0
import java.awt.Color;
import java.awt.Graphics;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;

public class guiMethod extends JFrame
{

    public static void main(String[] args)
    {
        guiMethod metronome = new guiMethod();
        metronome.setTitle("Example GUI");
        metronome.setSize(400, 120);
        metronome.setBackground(Color.BLACK);
        metronome.setDefaultCloseOperation(EXIT_ON_CLOSE);
        metronome.setVisible(true);

    }

    public void paint(final Graphics g)
    {

        Timer metronome = new Timer();

        TimerTask task = new TimerTask()
        {
            public void run()
            {
                int numSquared = 0;

                if (numSquared > 3)
                {
                    numSquared = 0;
                }
                else
                {
                    numSquared++;
                }

                int col;
                int row;
                int x;
                int y;

                for (row = 0; row < 1; row++)
                {
                    for (col = 0; col < 4; col++)
                    {
                        x = col * 100;
                        y = (row * 100) + 20;

                        if (col == numSquared)
                        {
                            g.setColor(Color.BLUE);
                        }
                        else
                        {
                            g.setColor(Color.CYAN);
                        }
                        g.fillRect(x, y, 100, 100);
                    }
                }
            }
        };
        metronome.schedule(task, 0, 1000);
    }

}

I'm attempting to create a metronome program, barebones, locked at 60BPM at the start. What I'm trying to figure out is when the timer goes through its task, the blue tile should move one space down. However, if a timer is implemented, the window is just blank. I was able to create a program for the first state of the metronome, here:

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;

public class guiMethod extends JFrame
{

    public static void main(String[] args)
    {
        guiMethod metronome = new guiMethod();
        metronome.setTitle("Example GUI");
        metronome.setSize(400, 120);
        metronome.setDefaultCloseOperation(EXIT_ON_CLOSE);
        metronome.setVisible(true);

    }

    public void paint(Graphics g)
    {
        int col; 
        int row;
        int x;
        int y;


        for (row = 0; row < 1; row++)
        {
            for (col = 0; col < 4; col++)
            {
                x = col * 100;
                y = (row * 100) + 20;

                if (col == 0)
                {
                    g.setColor(Color.BLUE);
                }
                else
                {
                    g.setColor(Color.CYAN);
                }
                g.fillRect(x, y, 100, 100);
            }
        }
    }


}

But when I implement the timer into the paint function, it doesn't work as intended. How do I implement the timer into the paint function without giving me a window without anything in it?

Devon Caron
  • 21
  • 1
  • 2
  • You shouldn't declare the timer inside of `paint`, as that won't work as you expect. Instead, the timer should be declared in the main method & it's `TimerTask` should call `metronome.repaint()` which will create a request to trigger `paint`. May also wanna look into `javax.swing.Timer`, opposed to `java.util.Timer`. – Vince Feb 14 '18 at 23:14
  • See http://www.java2s.com/Tutorials/Java/Swing_How_to/Timer/Refresh_with_swing_Timer.htm – PM 77-1 Feb 14 '18 at 23:17
  • @VinceEmigh: not "may also wanna" but definitely the OP *should* use a Swing Timer and not a util Timer. The former is much safer for Swing GUI's. – Hovercraft Full Of Eels Feb 14 '18 at 23:22

0 Answers0