A simple Java animation example, v3

By: Nicholas Duchon
Developed in class on July 16, 2014



// File: AnimatedGUIv3.java
// Date: July, 16, 2014
// Author: Nicholas Duchon
// Purpose: demonstrate an animated GUI

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

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.JButton;

public class AnimatedGUIv3 extends JFrame {
public static final long serialVersionUID = 123;
static int frameCount = 0;

String value = "One day at a time";
JButton jbStop = new JButton ("Stop");
JButton jbFast = new JButton ("Faster");
JButton jbSlow = new JButton ("Slower");

public AnimatedGUIv3 (String pv, Color pc) {
setVisible (true);
setSize (400, 500);
value = pv + pv;
setTitle (value);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
AnimatingPanel ap = new AnimatingPanel(pc, this);
add (ap, BorderLayout.CENTER);
setLocationRelativeTo (null);

JPanel buttons = new JPanel ();
jbStop.addActionListener(ap);
jbFast.addActionListener(ap);
jbSlow.addActionListener(ap);
buttons.add (jbStop);
buttons.add (jbFast);
buttons.add (jbSlow);
add (buttons, BorderLayout.PAGE_START);

validate ();
frameCount ++;
} // end int constructor

public static void main(String[] args) {
AnimatedGUIv3 ag1 = new AnimatedGUIv3 ("qwerty", Color.yellow);
System.out.println ("bye: " + ag1);
AnimatedGUIv3 ag2 = new AnimatedGUIv3 ("Porthos ", Color.cyan);
System.out.println ("bye: " + ag2);
} // end main

public String toString () {
return super.toString() + " You have encountered my toString " + value;
} // end toString method

} // end class AnimatedGUIv1

class AnimatingPanel extends JPanel implements ActionListener {
public static final long serialVersionUID = 9837498;
Color myColor;
int boxX = 10, boxY = 40, boxW = 250, boxH = 200;
int cx = boxX, cy = boxY, rad = 5;
int sx = cx, sy = cy;
double t = 0, step = 1.0/100;
int direction = 0;
Timer timer;
boolean goFlag = true;
AnimatedGUIv3 parent = null;

public AnimatingPanel (Color x, AnimatedGUIv3 a) {
myColor = x;
parent = a;
setBackground (myColor);
int delay = 10; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//...Perform a task...
// System.out.println ("Doing it");
moveMe ();
}
};
timer = new Timer(delay, taskPerformer);
timer.start();
} // end color constructor

public void actionPerformed (ActionEvent e) {
if (e.getSource() == parent.jbStop) {
goFlag = ! goFlag;
if (goFlag) {timer.start(); parent.jbStop.setText ("Stop");}
else {timer.stop(); parent.jbStop.setText("Start");}
} // end if source = jbStop
else if (e.getSource() == parent.jbFast) step *= 1.5;
else if (e.getSource() == parent.jbSlow) step /= 1.5;
} // end action Performed

void moveMe () {
t += step;
switch (direction) {
case 0:
cx = (int) (sx + t * boxW + 0.5);
cy = (int) (sy + t * boxH + 0.5);
break;
case 1:
cx = (int) (sx - t * boxW + 0.5);
break;
case 2:
cy = (int) (sy - t * boxH + 0.5);
break;
case 3:
cx = (int) (sx + t * boxW + 0.5);
cy = (int) (sy + t * boxH + 0.5);
break;
case 4:
cy = (int) (sy - t * boxH + 0.5);
break;
case 5:
cx = (int) (sx - t * boxW + 0.5);
break;
} // end switching on line segment index = direction
repaint ();
if (t >= 1) {
t = 0;
sx = cx;
sy = cy;
direction = (direction + 1) % 6;
if (direction % 3 == 0) {sx = boxX; sy = boxY;}
} // end if done with this line segment
} // end moveMe

public void paint (Graphics g) {
super.paint (g);
g.setColor (Color.black);
g.drawRect (boxX, boxY, boxW, boxH);
g.drawLine(boxX, boxY, boxX + boxW, boxY + boxH);
g.drawString ("Direction " + direction, cx + 20, cy + 20);
g.setColor(Color.red);
g.fillOval(cx - rad, cy - rad, 2*rad, 2*rad);
} // end method paint

} // end class AnimatingPanel