I'm fairly sure I understand how a swing timer works, I just cannot figure out how to apply it in my code. The way I apply it in my code doesn't allow it to draw because Graphics g is outside of its scope.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JayFrame extends JFrame
{
public JayFrame()
{
super("My Frame");
setContentPane(new DrawPane());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1200, 675);
setResizable(false);
setVisible(true);
}
class DrawPane extends JPanel
{
Timer timer = new Timer(1000, new MyTimer());
public void paintComponent(Graphics g)
{
//Paint stuff
super.paintComponent(g);
timer.start();
for(int i = 0; i < 1000; i += 110)
{
g.fillRect(i, 10, 100, 100);
try{Thread.sleep(100);}
catch(InterruptedException ie){}
}
timer.stop();
}
}
class MyTimer implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//Loop stuff
repaint();
}
}
public static void main(String[] args)
{
new JayFrame();
}
}
EDIT: I have updated the code to show what I think should work, but does not. So I probably do have a flawed understanding of swing timers.