Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6920025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:05:44+00:00 2026-05-27T10:05:44+00:00

This is just an exercise in mechanics. I am attempting to create three custom

  • 0

This is just an exercise in mechanics. I am attempting to create three custom panels that control their own progress bar. It’s part of a time management program I am writing for myself to learn more about Java. The larger program uses dates input by the user to create the framework for the min/max of the progress bar. Both this program and the larger one exhibit the same behavior with multiple bars racing the clock.

The issue I am having is that if I have just one bar everything seems to work just fine, but when I have more than one everything seems to go bust. So I wrote this little program to test some things out. It’s very simple, takes three custom panels, gives them a label and uses a timer event to change the label and the position of the progress bar. My question is If the math lines up (System output shows the calculation) and I’m counting events every second (1000 milliseconds) why is everything beating the count down.

Please forgive my lack of form with my code. I’m more concerned with the logic than the form.

(Most of the below is cut from my larger program, so if you see extraneous bits they really do have a home)
Thank you in advance.

import javax.swing.*;
import javax.swing.Timer;
import java.awt.*;
import java.awt.Color;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;

public class plaything extends JFrame implements ActionListener
{
myPanel[] mp = new myPanel[3];
JLabel[] jl = new JLabel[3];
short[] tim = new short[3];
short x = 0;
short t = 0; //used to stagger the clocks
short dateSaver; //holds temp dates

public plaything()
{
    setSize(400, 350);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new GridLayout(3, 1) ); 

    for(short x = 0; x < 3; x++)
    {
        mp[x] = new myPanel();

        //sets all three bars to different 'times'
        dateSaver = (short)(10 + t) ;
        tim[x] = dateSaver;
        mp[x].setMax( dateSaver );

        jl[x] = new JLabel("Expires: " + dateSaver);
        this.add(mp[x]);
        mp[x].add( jl[x] );
        t += 15; // 15 seconds
    }

    Timer time = new Timer(1000, this);
    time.start();
}
public void actionPerformed(ActionEvent e)
{
    if ( x < 60 )
    {
        x++;
    } 
    else
    {
        x = 1;
    }
    for(myPanel m : mp)
    {
        m.tock();
    }
    for(short x = 0; x < 3; x++ )
    {
        mp[x].tock();
        jl[x].setText( "" + --tim[x]  );
    }
}


private class myPanel extends JPanel
{
//Fields
    private boolean finished = false;
    //(x,y) Coords
    private short x = 15;
    private short y = 50;
    //Size and shape
    private short width = 200;
    private short height = 10;
    private short arcSize = 10;
    //Bar essentials
    private double max; //highest range of bar
    private double fill = width; //sets filled in portion
    private double tick; //calculates movement per event
    private Color urgent = Color.BLUE; // Changes the color depending on the   Urgency

//Constructors
    public myPanel()
    {
        this.setBackground( Color.WHITE );
        repaint();
    }

//Mutators ( tick manipulation )
    public void setMax( double maxIn )
    {
        this.max = maxIn;
        System.out.println("Max: " + this.max );
        this.tick = (double)width / this.max;
        System.out.println("tick: " + this.tick );
    }

//Methods
    //Tick Manipulation
    public void tock()
    {   
        //Ends when zero
        if( fill < 1 )
        {
            fill = width;
            finished = true;
            tick = 0;
            urgent = Color.BLUE;
            repaint();
        }
        else
        {   
            fill -= tick ;
            System.out.println("fill is " + fill );
            repaint();
        }

    }
    //Paint method
    public void paint( Graphics g)
    {
        super.paint(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setColor( urgent );
        g2.draw(new RoundRectangle2D.Double(x,y + 40, width, height, arcSize, arcSize) );
        g2.fill(new RoundRectangle2D.Double(x,y + 40, fill , height, arcSize, arcSize) );
    }
 }
public static void main(String[] args)
  {
    plaything pt = new plaything();
    pt.setVisible(true);
  }
}

My real only concern is where is my logic flawed concerning the progression of the bars and the labels. I hope to find how to make both reach zero together. (two days of research and work on just the bars alone)

Again thank you for your time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:05:45+00:00Added an answer on May 27, 2026 at 10:05 am

    You’re calling tock() twice every iteration of your Timer:

        for(myPanel m : mp)
        {
            m.tock(); // ONCE
        }
        for(short x = 0; x < 3; x++ )
        {
            mp[x].tock(); // TWICE
            jl[x].setText( "" + --tim[x]  );
        }
    

    You should remove one call, or the other.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey, This is just a simple exercise from class that I decided to have
This just won't work. The problem is that I do not know enough to
This just started happening three weeks or so ago. The content of my website
I don't think that this could be done in C#, but posting this just
How do I setup a class that represents an interface? Is this just an
I got this exercise, is not a homework , I just trying to solve:
I just completed an exercise for this text and was wondering if this could
This is one of those questions that you just know is going to make
This is one of those questions that you just know is going to make
I just finished this exercise for beginners on creating and importing modules in python.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.