








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of creating animations using java graphics, focusing on the rotator and pulsar classes. It covers the instance variables, constructors, and methods of these classes, as well as casting and updating the sprites. The code examples demonstrate how to draw and manipulate the sprites in different frames.
Typology: Study notes
1 / 14
This page cannot be seen from the preview
Don't miss anything!









Tuesday, December 5, 2006 Animation 23- 2
(0,0) y x JApplet window If I only had a brain import java.awt.; import javax.swing.; // for JApplet public class PacmanEatsTinman extends JApplet { public void paint (Graphics g) // Draw Tinman g.setColor(Color.red); g.drawString("If I only had a brain", 80 , 60 ); g.drawOval( 100 , 100 , 50 , 50 ); // Head Polygon hat = new Polygon(); hat.addPoint( 100 , 100 ); hat.addPoint( 125 , 70 ); hat.addPoint( 150 , 100 ); g.fillPolygon(hat); g.fillRect( 100 , 150 , 50 , 80 ); // Body g.fillRect( 105 , 230 , 15 , 50 ); // Leg g.fillRect( 130 , 230 , 15 , 50 ); // Leg g.fillRect( 75 , 175 , 100 , 20 ); // Arms // Draw Pacman g.setColor(Color.yellow); g.fillArc( 0 , 150 , 80 ,80,45,270); // Body } }
Animation 23- 3 Review: Graphics contract (partial) public void setColor (Color c) public void drawString (String str, int x, int y) public void drawLine (int x1, int y1, int x2, int y2) public void drawRect (int x, int y, int width, int height) public void fillRect (int x, int y, int width, int height) public void drawOval (int x, int y, int width, int height) public void fillOval (int x, int y, int width, int height) public void drawPolygon (Polygon p); public void fillPolygon (Polygon p); public void drawArc (int x, int y, int width, int height, int startAngle, int arcAngle); public void fillArc (int x, int y, int width, int height, int startAngle, int arcAngle); Animation 23- 4 A New Microworld: AnimationWorld o Created by Elaine Yang (CS111 lab instructor, 1999-2000) o Animation World uses Java Graphics to create animations. There are 2 main parts to AnimationWorld:
Animation 23- 7 public class Rotator extends Sprite { // Instance variables describing the state of a rotator. // All of these remain constant during animation except angle. private int x; // x-coord of arc center private int y; // y-coord of arc center private int radius; // radius of arc private int arcAngle; // angle of arc private Color color; // color of arc private int angle; // current startAngle of arc private int deltaAngle; // change in angle on update // Constructor method to create a rotator public Rotator(int x, int y, int radius, int arcAngle, int deltaAngle, Color color) { this.x = x; this.y = y; this.radius = radius; this.arcAngle = arcAngle; this.deltaAngle = deltaAngle; this.color = color; this.angle = 0; // initial startAngle of arc // … Instance methods go here … } } The Rotator Class radius color (x, y) (^) angle arcAngle Animation 23- 8 Rotator Instance Methods public class Rotator extends Sprite { // Instance variables and constructors ... public void drawState (Graphics g) { g.setColor(color); g.fillArc(x-radius,yradius, 2 radius, 2 radius, angle,arcAngle); } public void updateState () { angle = angle + deltaAngle; } public void resetState () { angle = 0; } } (0,0) (x, y) y x 2radius 2radius (x-radius, y-radius)
Animation 23- 9 Casting public class RotatorAnimation extends Animation { // Constructor method public RotatorAnimation () { this.addSprite(new Rotator( 400 , 200 , 100 , 30 , 5 ,Color.pink)); this.addSprite(new Pulsar( 300 , 275 , 75 , 270 ,-2,Color.gray)); this.setNumberFrames(Animation.NO_MAX_FRAMES); } } // Recall the Rotator constructor contract: public Rotator(int x, int y, int radius, int arcAngle, int deltaAngle, Color color); Animation 23- 10 Casting public class RotatorAnimation extends Animation { // Constructor method public RotatorAnimation () { Sprite r1 = new Rotator( 400 , 200 , 100 , 30 , 5 ,Color.pink)); Sprite r2 = new Rotator( 300 , 275 , 75 , 270 ,-2,Color.gray)); this.addSprite(r1); this.addSprite(r2); // What happens if sprites are added in opposite order? this.setNumberFrames(Animation.NO_MAX_FRAMES); } } // Recall the Rotator constructor contract: public Rotator(int x, int y, int radius, int arcAngle, int deltaAngle, Color color);
Animation 23- 13 Pulsars Two Sprites ready to perform Animation 23- 14 The Pulsar Class (x, y) radius minRadius (^) maxRadius color public class Pulsar extends Sprite { // instance variables private int x; private int y; private int radius; private int minRadius; private int maxRadius; private int changeRadius; private Color color; // constructors public Pulsar(int x, int y, int minRadius, int maxRadius, int changeRadius, Color color) { this.x = x; this.y = y; this.radius = minRadius; this.minRadius = minRadius; this.maxRadius = maxRadius; this.changeRadius = changeRadius; this.color = color; } }
Animation 23- 15 Pulsar instance methods public class Pulsar extends Sprite { // instance variables and constructors ... public void drawState(Graphics g) { g.setColor(color); g.fillOval(x-radius,y-radius, 2 radius, 2 radius); } … } Applet window (0,0) (x, y) y x 2radius 2radius (x-radius, y-radius) Animation 23- 16 Updating the Pulsar public class Pulsar extends Sprite { // instance variables and constructors ... public void drawState(Graphics g) { g.setColor(color); g.fillOval(x-radius,y-radius, 2 *radius, 2 *radius); } public void updateState() { radius = radius + changeRadius; if (radius > maxRadius) { radius = maxRadius; changeRadius = - changeRadius; } else if (radius < minRadius) { radius = minRadius; changeRadius = - changeRadius; } } … }
Animation 23- 19 Bouncing balls Animation 23- 20 Mobile sprites public class BouncingBall extends Sprite { // Instance variables private int radius; private int initX; private int initY; private int x; private int y; private int changeX; private int changeY; private Color color; // Constructor method public BouncingBall(int ix, int iy, int radius, int changeX, int changeY, Color color) { this.initX = ix; this.initY = iy; this.radius = radius; this.changeX = changeX; this.changeY = changeY; this.color = color; this.resetState(); } }
Animation 23- 21 Rendering & resetting the Sprite public class BouncingBall extends Sprite { // Instance variables and constructor method ... // Instance methods public void drawState(Graphics g) { g.setColor(color); g.fillOval(x-radius, y-radius, 2 radius, 2 radius); } public void resetState() { this.x = this.initX; this.y = this.initY; } } (0,0) (x, y) y x 2radius 2radius (x-radius, y-radius) Animation 23- 22 The ball moves by (changeX, changeY) public class BouncingBall extends Sprite { ... public void updateState() { x = x + changeX; y = y + changeY; if (x < radius) { x = radius; changeX = - changeX; } if (x + radius >= width) { x = width - radius; changeX = - changeX; } if (y < radius) { y = radius; changeY = - changeY; } if (y + radius >= height) { y = height - radius; changeY = - changeY; } } } (0,0) (x, y) y x (x+changeX, y+changeY) Move ball
Animation 23- 25 Hitting the ceiling public class BouncingBall extends Sprite { ... public void updateState() { x = x + changeX; y = y + changeY; if (x < radius) { x = radius; changeX = - changeX; } if (x + radius >= width) { x = width - radius; changeX = - changeX; } if (y < radius) { y = radius; changeY = - changeY; } if (y + radius >= height) { y = height - radius; changeY = - changeY; } } } (0,0) (x, y) y (x-radius, y-radius) x Bumped top, reverse directions Animation 23- 26 Bottoming out public class BouncingBall extends Sprite { ... public void updateState() { x = x + changeX; y = y + changeY; if (x < radius) { x = radius; changeX = - changeX; } if (x + radius >= width) { x = width - radius; changeX = - changeX; } if (y < radius) { y = radius; changeY = - changeY; } if (y + radius >= height) { y = height - radius; changeY = - changeY; } } } (0,0) (x, y) y x (x-radius, y-radius) Bumped bottom, reverse directions h e i g h t
Animation 23- 27 The show must go on public class BouncingBallAnimation extends Animation { // Constructor method for BouncingBallAnimation public BouncingBallAnimation() { this.addSprite(new BouncingBall( 50 , 50 , 30 , 2 , 2 ,Color.magenta)); this.addSprite(new BouncingBall( 150 , 150 , 25 , 3 ,-3,Color.cyan)); this.addSprite(new BouncingBall( 250 , 250 , 40 , 1 , 4 ,Color.green)); this.setFps( 12 ); } } // Constructor method for BouncingBall public BouncingBall(int ix, int iy, int radius, int changeX, int changeY, Color color) {…}