Cat Snake - Graphics 2D Animation Write an applet program that will create an an
ID: 3795010 • Letter: C
Question
Cat Snake - Graphics 2D Animation
Write an applet program that will create an animation of a shape (a cat? or a snake?) moving across the screen using Graphics2D (see below). Create an applet similar to the image below using setSize() to size your applet and applet viewer. It will have Ellipse2D shape for the yellow sun, Rectangle2D green grass, and the Rectangle2D cyan and white sky. Create a GradientPaint cyclic object for the shift in colors for the sky. You will also draw a little dark gray Line2D tree trunk with a green Arc2D for the leaves.
You will draw a red GeneralPath for the face with 2 blue eyes. Use a BasicStroke to make the lines around the face thicker. The face (and eyes) will slowly move across the screen from left to right using the static sleep method. Override the update method to lesson flicker. Each time in paint(), you will draw over the previous shape in the green grass color, then make the move and redraw the face a short distance to the right (keep track of an x variable that keeps incrementing in paint() and a y that stays constant). You can leave all other shapes drawn on the screen.
So the paint() method will:
1. If it is the first time run, draw everything with a Graphics2D object.
2. If it is not the first time:
a. Use a GeneralPath (with an x coordinate) with a BasicStroke and Ellipse to draw either just the cat snake face or all the grass in green to cover the previous face.
b. Move the x coordinate to the right a short distance.
c. Recreate the GeneralPath and redraw the cat snake face in blue and red.
d. Go to sleep for a few milliseconds.
e. Continue your cat snake moving until it is to the right edge of the applet. If your cat snake is not there yet, call repaint() to repeat. Stop your cat snake while it is visible on the right edge of the applet.
Add a full paragraph of documentation (at least 4 lines each) to your main program. Supply extra documentation (at least 5 comments) throughout for all of the not easily understandable lines of code for your main program. Run your applet.
can someone help me with this
Explanation / Answer
import java.awt.geom.*; //Needed for Graphics 2D
import javax.swing.JApplet; //Needed for applet
import java.awt.*;
public class CatSnake extends JApplet {
private static final long serialVersionUID = 1L; //Needed to avoid Eclipse warning
boolean firstTime = true; //Flag to indicate if static object need to be painted
int catHeadLeft = 30; //Starting x position of vertical cat head lines
int catHeadRight = 80;
int catEarLeft = 45; //Starting x position of diagonal cat ear lines
int catEarRight = 65;
int catEyeLeftX = 37; //Starting x position of cat eyes
int catEyeRightX = 63;
int appletHeight; //Applet width and height
int appletWidth;
//Method to initialize applet, and get dimensions
public void init() {
this.setSize(600, 400); // Sets size of applet viewer
Dimension appletSize = this.getSize(); // Gets applet size
appletHeight = appletSize.height;
appletWidth = appletSize.width;
}
//Method to override paint, does not clear the screen to reduce flicker
public void update(Graphics g) {
paint(g);
}
//Method to paint the applet, calls repaint to move cat head after sleep and increment
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g; //Instantiates new Graphics2D object, casts a Graphics object (g)
//as Graphics2D
if (firstTime) { //Paints static objects if first call to paint
//Paints solid green grass rectangle
Rectangle2D.Float grassRectangle = new Rectangle2D.Float(0, appletHeight / 2, appletWidth,
appletHeight / 2); //Spans entire applet width and half the height from bottom
g2.setColor(Color.green); //Sets color
g2.fill(grassRectangle); //Paints grass
//Paints gradient sky rectangle
GradientPaint gradientPainter = new GradientPaint(0, 0, Color.cyan, appletWidth - 100, appletHeight - 100,
Color.white, false); //Instantiates new GradientPaint object using cyan and white
//Rectangle spans entire applet width, half the height from top
Rectangle2D.Float skyRectangle = new Rectangle2D.Float(0, 0, appletWidth, appletHeight / 2);
g2.setPaint(gradientPainter); //Sets gradient color
g2.fill(skyRectangle); //Paints sky
//Paints Basic Stroke tree trunk
BasicStroke trunkStroke = new BasicStroke(5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND); //Defines stroke
//Positions endpoints to create a vertical tree trunk 50 pixels to the right of center, with base at horizon
Point2D.Float trunkTop = new Point2D.Float(appletWidth / 2 + 50, appletHeight / 2);
Point2D.Float trunkBottom = new Point2D.Float(appletWidth / 2 + 50, (appletHeight / 2) - 60);
Line2D.Float trunkLine = new Line2D.Float(trunkTop, trunkBottom); //Creates Line2D object
g2.setColor(Color.darkGray); //Sets color
g2.setStroke(trunkStroke); //Sets stroke
g2.draw(trunkLine); //Paints trunk
//Paints solid green arc tree top
Arc2D.Float treeArc = new Arc2D.Float(appletWidth / 2 + 10, appletHeight / 2 - 80, 80, 50, 10f, 160f,
Arc2D.CHORD); //Creates new Arc2D centered on top of trunk
g2.setColor(Color.green); //Sets color
g2.fill(treeArc); //Paints tree top
//Paints solid yellow ellipse sun
//Creates new Ellipse2D circle in top left corner of applet
Ellipse2D.Float sunEllipse = new Ellipse2D.Float(appletWidth / 10, appletHeight / 10, 60f, 60f);
g2.setColor(Color.yellow); //Sets color
g2.fill(sunEllipse); //Paints sun
firstTime = false; //Changes flag so static shapes will not be repainted to reduce flicker
}
try{ //Try-catch block to handle animation and sleep
// Sets cat head stroke
BasicStroke catStroke = new BasicStroke(3f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
//Paints green rectangle over cat head
Rectangle2D catCover = new Rectangle2D.Float(catHeadLeft - 5, 225, 65, 70);
g2.setColor(Color.GREEN);
g2.fill(catCover);
//Increments x position of cat head and eyes, to move to the right
catHeadLeft += 20;
catHeadRight += 20;
catEarLeft += 20;
catEarRight += 20;
catEyeLeftX += 20;
catEyeRightX += 20;
//Paints GeneralPath cat head in red, which will be visible
GeneralPath catHead = new GeneralPath();
catHead.moveTo(catHeadLeft, 230f);
catHead.lineTo(catHeadLeft, 290f);
catHead.lineTo(catHeadRight, 290f);
catHead.lineTo(catHeadRight, 230f);
catHead.lineTo(catEarRight, 245f);
catHead.lineTo(catEarLeft, 245f);
catHead.closePath();
g2.setStroke(catStroke);
g2.setColor(Color.red);
g2.draw(catHead);
//Paints Ellipse2D cat eyes in blue, which will be visible
Ellipse2D.Float catEyeLeft = new Ellipse2D.Float(catEyeLeftX, 250, 10f, 10f);
Ellipse2D.Float catEyeRight = new Ellipse2D.Float(catEyeRightX, 250, 10f, 10f);
g2.setColor(Color.blue);
g2.fill(catEyeLeft);
g2.fill(catEyeRight);
//Sleeps for quarter second
Thread.sleep(250);
} catch (InterruptedException e) { //Empty catch to handle interrupted exception
}
if (catHeadRight < appletWidth - 20) { //Tests if right edge of cat head is on the applet.
repaint(); //Calls repaint if head is on the applet. Static shapes are not redrawn,
//cat head is covered in green, incremented, and repainted
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.