Animation - UPDATE FOR JFRAME
2 Ways that might make sense; you dont need buffergraphics because JPanel has it built in.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class TimerBasedAnimation extends JPanel implements Runnable {
int x=0;
public TimerBasedAnimation() {
start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
//g.clearRect(0, 0, 500, 500);
x++;
g.drawRect(x,40,50,50);
g.setColor(Color.green);
g.fillOval(x, 20, 20, 20);
}
public void update()
{
//put your code for what should happen each animation
repaint();
}
/*********************************************************************************************/
/* BELOW IS FOR ANIMATION. THE ONLY THING THAT YOU NEED TO CHANGE IS DELAY */
int frame;
int delay=20; // this is the time of the delay in milliseconds.
Thread animator;
/**
* This method is called when the applet becomes visible on
* the screen. Create a thread and start it.
*/
public void start()
{
animator = new Thread(this);
animator.start();
}
/**
* This method is called by the thread that was created in
* the start method. It does the main animation.
*/
public void run()
{
// Remember the starting time
long tm = System.currentTimeMillis();
while (Thread.currentThread() == animator)
{
// Display the next frame of animation.
update();
try
{
tm += delay;
Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
}
catch (InterruptedException e)
{
break;
}
// Advance the frame
frame++;
}
}
/**
* This method is called when the applet is no longer
* visible. Set the animator variable to null so that the
* thread will exit before displaying the next frame.
*/
public void stop()
{
animator = null;
}
public static void main(String[] args) {
JFrame frame = new JFrame("TimerBasedAnimation");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TimerBasedAnimation());
frame.setSize(350, 250);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class CopyOfTimerBasedAnimation extends JPanel implements ActionListener {
int x=0;
Timer timer;
public CopyOfTimerBasedAnimation() {
timer = new Timer(20, taskPerformer);
timer.setInitialDelay(190);
timer.start();
}
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
repaint();
}
};
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.clearRect(0, 0, 500, 500);
x++;
g.drawRect(x,40,50,50);
}
public void actionPerformed(ActionEvent e) {
repaint();
}
public static void main(String[] args) {
JFrame frame = new JFrame("TimerBasedAnimation");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new CopyOfTimerBasedAnimation());
frame.setSize(350, 250);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
}
|