1

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.

6
  • You say "everything goes bust," what exactly happens, can you be more specific? Does one of them finish properly, but the rest of them finish immediately? Or do they all finish immediately? What is the output of your System.out.println() method calls?
    – Craig Otis
    Dec 12, 2011 at 0:35
  • This program posted, all three lines finish fast, the first is 5 seconds fast, the second is 12 seconds fast and the last is almost 23 seconds fast. All the output from the System.out is what you would expect. The math lines up but bars still finish early. In my larger program which this is slightly modeled off of. One (the longest one) finishes correctly and all the rest tend to be fast.
    – Powe8525
    Dec 12, 2011 at 0:41
  • 1
    You state, "Please forgive my lack of form with my code. I’m more concerned with the logic than the form.". This is fine when you're just writing code for yourself, but when you're writing code and ask that others try to evaluate and understand it, especially volunteers, then this is wrong. Use of proper form when posting code here is a sign of respect for our time and effort, and shows that you're willing to put in effort so as to make our efforts for your benefit easier. Dec 12, 2011 at 0:43
  • That's a very good point. I put this together not really to post but to play around with the concept. to try to find my flaws. Please don't take my lack of form for anything other than lack of training. I will gain better habits as time goes on I'm sure.
    – Powe8525
    Dec 12, 2011 at 0:47
  • 1
    Your code itself (not the formatting) actually looks pretty good. Some suggestions if you don't mind: draw in the JPanel's paintComponent method, not its paint method since you're not wishing to override the painting of any children or borders. Also, consider setting the Graphics2D object's anti-aliasing RenderingHints to RenderingHints.VALUE_ANTIALIAS_ON, to smooth out your drawing. If any of this is unclear, please let me know, and I'll show you what I mean. Dec 12, 2011 at 1:15

1 Answer 1

4

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.

13
  • Haha thanks. Kind of tough to weed through a bunch of one- and two-letter variable names to figure out what was going on.
    – Craig Otis
    Dec 12, 2011 at 0:50
  • Wow i was so tired i missed that entirely! Now this model works just fine. Which begs the question why does this work and not the larger code. The only real difference is tab panels. Thank you very much Craig! The first line was how i was going to iterate until i realized i couldn't use m in any way to iterate my labels. Still have to watch my cleanup.
    – Powe8525
    Dec 12, 2011 at 0:51
  • @Powe8525 No problem. Feel free to start a new question for your other code if interested.
    – Craig Otis
    Dec 12, 2011 at 0:52
  • my other code is over 700 lines of code and every bit as ugly as this one. I couldn't do that to you guys. Thanks for your help though!
    – Powe8525
    Dec 12, 2011 at 0:54
  • 1
    I ran your larger project. If the issue with your code is that the progress bar seems to finish at a slightly different time than the countdown itself, consider this: Don't calculate your progress bar decrement amount right off the bat. It's very bad practice. For example, if you have a progress bar of width 100 and you want to decrease it in 60 seconds, do not calculate ahead of time that for each tick, you need to decrease its width by 1.4. Rather, update your view every time, based completely on the data from the model. Eg. at 37 seconds, set your bar's width to: (100 / 37)
    – Craig Otis
    Dec 12, 2011 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.