Surens Animation
<< Eni's and Erik's Hockey Game | OldProjectsTrailIndex | Kevin's Tower Defense Game >>
Suren's Disk racer animation.
Fr Chris' notes
Its getting better and better!
/**
* A Sample Animation with a Restart button * * @author Fr Chris Thiel, OFMCap * @version 9 May 2007 */
import java.awt.event.*; import java.applet.*; import java.applet.Applet; import java.awt.*; import javax.swing.*;
public class SampleAnimation extends Applet
implements ActionListener
{
Timer timer; Image virtualMem; Graphics gBuffer; int appletWidth; int appletHeight; int frame; int lastFrame; double rate1=.5; double rate2=.5; double rate3=.5; double rate4=.5; double rate5=.5; Button myButton; AudioClip racecars; /** * ActionListener is an interface and * requires the actionPerformed() method * to be defined..in this case we * look for a restart button being pressed */ public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == timer) { if (frame < lastFrame) { frame++; } else { frame=0; rate1=Math.random(); rate2=Math.random(); rate3=Math.random(); rate4=Math.random(); rate5=Math.random(); } } else { //restart button pressed frame=0; rate1=Math.random(); rate2=Math.random(); rate3=Math.random(); rate4=Math.random(); rate5=Math.random(); if (timer.isRunning()) { timer.stop(); myButton.setLabel("Start"); if(racecars!=null)racecars.stop(); } else { timer.start(); myButton.setLabel("Stop"); if(racecars!=null) racecars.loop(); } } repaint(); } // OUR CLASS IS A SUBCLASS OF APPLET /** * this init() method will be run at the beginning * and where we create the Listeners */ public void init() { appletWidth = getWidth(); appletHeight = getHeight(); frame=0; timer=new Timer(10,this); lastFrame=appletWidth; virtualMem = createImage(appletWidth,appletHeight); gBuffer = virtualMem.getGraphics(); gBuffer.setColor(Color.white); gBuffer.fillRect(0,0,appletWidth,appletHeight); myButton= new Button("5,4,3,2,1,0 Get Set Go!!!!!!!!"); //the class is its own button listener myButton.addActionListener (this); add(myButton); //Audio racecars = getAudioClip(getDocumentBase(), "racecars.wav"); } public int speed(int f, double p) { return (int)(f*p); } public void paint(Graphics g) { //We draw using the object methods of // the graphics buffer, not what is // currently on the screen gBuffer.setColor(Color.black); gBuffer.fillRect(0,0,appletWidth,appletHeight);gBuffer.setColor(Color.black); gBuffer.setColor(Color.black); gBuffer.drawString("Frame "+frame,20,20); gBuffer.setColor(Color.blue); gBuffer.fillOval(speed(frame,rate1), 200, 50,50); gBuffer.setColor(Color.orange); gBuffer.fillOval(speed(frame,rate2), 100, 50,50); gBuffer.setColor(Color.red); gBuffer.fillOval(speed(frame,rate3), 10, 50,50); gBuffer.setColor(Color.green); gBuffer.fillOval(speed(frame,rate4), 300, 50,50); gBuffer.setColor(Color.yellow); gBuffer.fillOval(speed(frame,rate5), 400, 50,50); //Now we send the result to the screen g.drawImage(virtualMem,0,0,this); } public void update(Graphics g) { paint(g); //get rid of flicker with this method }
}