Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: repaint in a JButton while loop.

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Location
    Bedroom
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default repaint in a JButton while loop.

    Hey, so we have an assignment in Java. We are asked to make a GUI program that imitates the functions and interface of a microwave oven. I got some parts of the program working now. But I encountered a problem in the code of the JButton action listener.

    This error is about repaint in the loop that's in JButton code for the action listener. The function of the JButton which is named "Start" is supposed to start the countdown timer of the microwave oven. I got the countdown working really nice. But the problem is when It comes to repainting the remaining time which is a Graphics(a drawString). It seems that the repaint() gets invoked in the loop but since the loop for the JButton hasn't finished yet all the repaint() calls get invoked but are not invoked immediatley. It waits for the loop to terminate then invokes all the repaint() call all at the same time, and obviously, the only repaint() that wil be seen will be the last. I'm looking for a way to have it immediately repainted everytime it loops without having to wait for the loop to terminate. And am also using Thread.sleep(1000) so that it decrements every second.

    I'm really sorry I still don't know how to post a code in a proper way.
    Here is the code:

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import java.awt.Graphics;
    import java.awt.BorderLayout;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    public class MicrowaveOven extends JFrame implements ActionListener
    {
    private static JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bStart, bStopReset, bOpenClose;
    private static JLabel time;
    private static int x = 0, y = 0, z = 0, test = 0;
    public static void main(String[] args)
    {
    MicrowaveOven groupTwo = new MicrowaveOven();
    groupTwo.setTitle("Computer Programming");
    groupTwo.setSize(800, 500);
    groupTwo.setDefaultCloseOperation(JFrame.EXIT_ON_C LOSE);
    groupTwo.setLayout(null);
    groupTwo.setVisible(true);

    groupTwo.add(b7);
    groupTwo.add(b8);
    groupTwo.add(b9);
    groupTwo.add(b4);
    groupTwo.add(b5);
    groupTwo.add(b6);
    groupTwo.add(b1);
    groupTwo.add(b2);
    groupTwo.add(b3);
    groupTwo.add(b0);
    groupTwo.add(bStopReset);
    groupTwo.add(bStart);
    groupTwo.add(bOpenClose);
    //groupTwo.add(time);
    }

    public MicrowaveOven()
    {
    b7 = new JButton("7"); b7.setSize(45, 30); b7.setLocation(584, 110);
    b8 = new JButton("8"); b8.setSize(45, 30); b8.setLocation(637, 110);
    b9 = new JButton("9"); b9.setSize(45, 30); b9.setLocation(690, 110);
    b4 = new JButton("4"); b4.setSize(45, 30); b4.setLocation(584, 150);
    b5 = new JButton("5"); b5.setSize(45, 30); b5.setLocation(637, 150);
    b6 = new JButton("6"); b6.setSize(45, 30); b6.setLocation(690, 150);
    b1 = new JButton("1"); b1.setSize(45, 30); b1.setLocation(584, 190);
    b2 = new JButton("2"); b2.setSize(45, 30); b2.setLocation(637, 190);
    b3 = new JButton("3"); b3.setSize(45, 30); b3.setLocation(690, 190);
    b0 = new JButton("0"); b0.setSize(45, 30); b0.setLocation(584, 230);
    bStopReset = new JButton("Stop/Reset"); bStopReset.setSize(98, 30); bStopReset.setLocation(637, 230);
    bStart = new JButton("Start"); bStart.setSize(150, 30); bStart.setLocation(584, 270);
    bOpenClose = new JButton("Open/Close"); bOpenClose.setSize(150, 80); bOpenClose.setLocation(584, 320);

    //time = new JLabel(" " + x + ":" + y + z); time.setFont(new Font("TimesRoman", Font.BOLD, 40)); time.setLocation(615, 55); time.setSize(100, 30);
    b7.addActionListener(this);
    b8.addActionListener(this);
    b9.addActionListener(this);
    b4.addActionListener(this);
    b5.addActionListener(this);
    b6.addActionListener(this);
    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    b0.addActionListener(this);
    bStopReset.addActionListener(this);
    bStart.addActionListener(this);
    bOpenClose.addActionListener(this);
    }

    public static String timeInput(String number)
    {
    if (z == 0)
    {
    if (y != 0)
    {
    x = y;
    y = z;
    z = Integer.parseInt(number);
    }
    else if (x != 0)
    {
    x = x;
    y = y;
    z = z;
    }
    else
    z = Integer.parseInt(number);
    }
    else if (y == 0)
    {
    if (x != 0)
    {
    x = x;
    y = y;
    z = z;
    }
    else
    {
    y = z;
    z = Integer.parseInt(number);
    }
    }
    else if (x == 0)
    {
    x = y;
    y = z;
    z = Integer.parseInt(number);
    }

    return number;
    }

    public void actionPerformed(ActionEvent click)
    {
    boolean countdown = true;


    switch (click.getActionCommand())
    {
    case "0":
    {
    timeInput("0");
    repaint();
    break;
    }
    case "1":
    {
    timeInput("1");
    repaint();
    break;
    }
    case "2":
    {
    timeInput("2");
    repaint();
    break;
    }
    case "3":
    {
    timeInput("3");
    repaint();
    break;
    }
    case "4":
    {
    timeInput("4");
    repaint();
    break;
    }
    case "5":
    {
    timeInput("5");
    repaint();
    break;
    }
    case "6":
    {
    timeInput("6");
    repaint();
    break;
    }
    case "7":
    {
    timeInput("7");
    repaint();
    break;
    }
    case "8":
    {
    timeInput("8");
    repaint();
    break;
    }
    case "9":
    {
    timeInput("9");
    repaint();
    break;
    }
    case "Stop/Reset":
    {
    x = 0;
    y = 0;
    z = 0;
    repaint();
    break;
    }
    case "Start":
    {
    while (countdown)
    {
    if (z != 0)
    z--;
    else if (z == 0)
    {
    if (y != 0)
    {
    y--;
    z = 9;
    }
    }
    if (y == 0)
    {
    if ((x != 0) && (z == 0) && (test == 0))
    {
    y = 0;
    z = 0;
    test = 1;
    }
    else if ((x != 0) && (z == 0) && (test == 1))
    {
    x--;
    y = 5;
    z = 9;
    }
    }
    if ((x == 0) && (y == 0) && (z == 0))
    countdown = false;

    System.out.println(x+ ":" + y + z);
    repaint();

    try
    {
    Thread.sleep(1000);
    }
    catch (InterruptedException iE){}
    }
    break;
    }
    default:
    }
    }


    public void paint(Graphics mWave)
    {
    super.paint(mWave);

    mWave.drawRect(20, 40, 760, 440);
    mWave.drawRect(50, 70, 500, 370);
    mWave.drawRect(565, 80, 200, 40);
    Font timerFont = new Font("TimesRoman", Font.BOLD, 40);
    mWave.setFont(timerFont);
    mWave.drawString(" " + x + ":" + y + z, 625, 115);
    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,370
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: repaint in a JButton while loop.

    When posting code, please use highlight tags. Otherwise your code is almost impossible to read. Also, code should be in the form of an SSCCE. Make it easier for people to help you.

    Until then, the best I can tell you is to not call Thread.sleep() while on the EDT (you're on the EDT in any Listeners or Swing event such as painting). This blocks the EDT and causes the program to become unresponsive. Use a Swing Timer instead.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Help with repaint
    By AraHabs in forum AWT / Java Swing
    Replies: 5
    Last Post: November 5th, 2011, 04:40 PM
  2. Drawing a line and repaint()
    By rogerbacon in forum AWT / Java Swing
    Replies: 3
    Last Post: October 19th, 2011, 07:08 PM
  3. My JFrame won't repaint correctly please help me out!
    By ocolegrove in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 10th, 2011, 10:04 PM
  4. Repaint,
    By Time in forum AWT / Java Swing
    Replies: 3
    Last Post: May 21st, 2010, 11:23 PM
  5. Repaint doesn't repaint?
    By PotataChipz in forum AWT / Java Swing
    Replies: 6
    Last Post: January 18th, 2010, 08:56 PM