A simple Java animation example, v2

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

Moving a dot on a rectangle with a diagonal

// File: AnimatedGUIv1.java
// Date: July, 9, 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 AnimatedGUIv1 extends JFrame { public static final long serialVersionUID = 123; static int frameCount = 0; String value = "One day at a time"; public AnimatedGUIv1 (String pv, Color pc) { setVisible (true); setSize (400, 500); value = pv + pv; setTitle (value); setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); AnimatingPanel ap = new AnimatingPanel(pc); add (ap, BorderLayout.CENTER); setLocationRelativeTo (null); JPanel buttons = new JPanel (); JButton jbStop = new JButton ("Stop"); JButton jbFast = new JButton ("Faster"); JButton jbSlow = new JButton ("Slower"); jbStop.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) { AnimatedGUIv1 ag1 = new AnimatedGUIv1 ("qwerty", Color.yellow); System.out.println ("bye: " + ag1); AnimatedGUIv1 ag2 = new AnimatedGUIv1 ("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; public AnimatingPanel (Color x) { myColor = x; 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) { goFlag = ! goFlag; if (goFlag) timer.start(); else timer.stop(); } // 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); // cy = (int) (sy + t * boxH); break; case 2: // cx = (int) (sx + t * boxW); 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: // cx = (int) (sx + t * boxW); cy = (int) (sy - t * boxH + 0.5); break; case 5: cx = (int) (sx - t * boxW + 0.5); // cy = (int) (sy + t * boxH); break; } // end switch on direction repaint (); if (t >= 1) { t = 0; sx = cx; sy = cy; direction = (direction + 1) % 6; // if (direction % 3 == 0) {sx = boxX; sy = boxY;} // don't wander, but rounding using 1/2 works better } // end one line, start on next line } // 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