Test3.java

This Java application demonstrates animation.

Download: java1.zip.

This example illustrates the use of a Timer to do animation. The Timer constructor takes two arguments: the delay in milliseconds and the ActionListener that will respond to the Timer's ActionEvents. For the second argument, an object of the inner class TimerHandler is created. This object calls JPanel's repaint function to redraw the screen.

The example here modifies Test2 by rotating one of the ball objects about a point on the screen.


Test3.java

// Test3.java
import java.awt.Graphics;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.Timer;
import java.util.ArrayList;


 class Panel3 extends JPanel
{
    ArrayList<Ball> balls = new ArrayList<Ball>();
    private Timer myTimer;
    Panel3()
    {
	balls.add(new Ball(80,100,Color.RED));
	balls.add(new Ball(160,70,Color.BLUE));
	balls.add(new Ball(100,70,Color.YELLOW));
	for (Ball b: balls) b.set_radius(6);
    }

    public void rotate(Ball b, double angle)
    {
	double x = b.px - 100;
	double y = b.py - 75;
	double phi = angle*Math.PI/180.0;
	double ct = Math.cos(phi);
	double st = Math.sin(phi);
	b.set_position(ct*x-st*y+100,st*x+ct*y+75);
    }
    
    public void startAnimation()
    {
	if (myTimer == null)
	{
	    myTimer = new Timer(50,new TimerHandler() );
	    myTimer.start();
	}
	else if (!myTimer.isRunning()) myTimer.restart();
    }
    
    public void stopAnimation()
    {
	myTimer.stop();
    }
	
    private class TimerHandler implements ActionListener
    {
	public void actionPerformed(ActionEvent actionevent)
	{
	    Ball b = balls.get(1);
	    rotate(b,3);
	    repaint();
	}
    }
    public void paintComponent(Graphics g)
    {
	super.paintComponent( g );
	for (Ball b: balls) b.draw(g);
    }
}

public class Test3 
{
    Panel3 panel;

    public static void main(String args[] )
    {
	Panel3 panel = new Panel3();
	JFrame application = new JFrame("Test3");
	application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	application.add(panel);
	application.setSize(200,200);
	application.setVisible(true);
	panel.startAnimation();
    }
}


Ball.java

import java.awt.*;

public class Ball
{
    double px, py;
    int radius;
    double mass;
    Color color;
    Ball()
    {
	color = new Color(255,0,0);
	set_position(50,50);
	set_radius(17);
    }
    Ball(double px, double py, Color color)
    {
	this.color = color;
	set_position(px,py);
	set_radius(17);
    }

    void set_position(double px, double py)
    {
	this.px = px;
	this.py = py;
    }
    
    void set_radius(int r)
    {
	radius = r;
	mass = r*r;
    }

    public String toString()
    {
	String str = "("+px+", "+py+") " + color;
	return str;
    }
    
    
    public void draw(Graphics g) 
    {
	g.setColor(color);
	g.fillOval((int)(px-radius), (int) (py-radius), (int)2*radius, (int)2*radius);
    }

    
}


Maintained by John Loomis, updated Thu Jun 02 19:11:11 2016