Question
Everything works Using the code provided below, add/ make changes to the program so that the moving shape reappears on the left-hand side after it
Everything works
Using the code provided below, add/ make changes to the program so that the moving shape reappears on the left-hand side after it dissapears from the frame.
AnimationTester.java /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class AnimationTester { /** Construct a frame and animate a plane in it. */ public static void main(String[] args) { JFrame frame = new JFrame(); final MoveableShape shape = new PlaneShape(0, 0, CAR_WIDTH); final ShapeIcon icon = new ShapeIcon(shape, ICON_WIDTH, ICON_HEIGHT); final JLabel label = new JLabel(icon); frame.setLayout(new FlowLayout()); frame.add(label); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); final int DELAY = 30; // milliseconds between timer ticks Timer t = new Timer(DELAY, new ActionListener() { public void actionPerformed(ActionEvent event) { shape.translate(1, 0); label.repaint(); } }); t.start(); } private static final int ICON_WIDTH = 400; private static final int ICON_HEIGHT = 100; private static final int CAR_WIDTH = 100; }
MoveableShape.java ///////////////////////////////////////////////////////////////////////////////////////////////////
import java.awt.*; /** A shape that can be moved around. */ public interface MoveableShape { /** * Draws the shape. * * @param g2 the graphics context */ void draw(Graphics2D g2); /** * Moves the shape by a given amount. * * @param dx the amount to translate in x-direction * @param dy the amount to translate in y-direction */ void translate(double dx, double dy); }
PlaneShape.java ///////////////////////////////////////////////////////////////////////////////////////////////////
import java.awt.*; import java.awt.geom.*; import java.util.*; /** A plane that can be moved around. */ public class PlaneShape implements MoveableShape { /** Constructs a plane item. @param x the left of the bounding rectangle @param y the top of the bounding rectangle @param width the width of the bounding rectangle */ public PlaneShape(int x, int y, int width) { this.x = x; this.y = y; this.width = width; } public void translate(double dx, double dy) { x += dx; y += dy; } public void draw(Graphics2D g2) { Rectangle2D.Double body = new Rectangle2D.Double(x + width / 3, y + width / 3, width, width / 6); // the bottom left of left wing Point2D.Double l1 = new Point2D.Double(x + width / 2, y + width / 3); // the top of left wing Point2D.Double l2 = new Point2D.Double(x + width / 2, y + width / 6); // the bottom right of left wing Point2D.Double l3 = new Point2D.Double(x + width / 1, y + width / 3); // Left wing Line2D.Double backLW = new Line2D.Double(l1, l2); Line2D.Double frontLW = new Line2D.Double(l2, l3); // the top left of right wing Point2D.Double r1 = new Point2D.Double(x + width / 2, y + width / 3 + width/6); // the bottom of right wing Point2D.Double r2 = new Point2D.Double(x + width / 2, y + 3*width / 6 + width/6); // the top right of right wing Point2D.Double r3 = new Point2D.Double(x + width / 1, y + width / 3 + width/6); // Right wing Line2D.Double backRW = new Line2D.Double(r1, r2); Line2D.Double frontRW = new Line2D.Double(r2, r3); // the top left of front Point2D.Double f1 = new Point2D.Double(x + width / 3 + width, y + width/3); // the right of front Point2D.Double f2 = new Point2D.Double(x + width / 3 + width + width/6, y + width / 3 + width/12); // the bottom left of front Point2D.Double f3 = new Point2D.Double(x + width / 3 + width, y + width / 3 + width/6); // Bottom Line2D.Double topFront = new Line2D.Double(f1, f2); Line2D.Double bottomFront = new Line2D.Double(f2, f3); g2.draw(body); g2.draw(backLW); g2.draw(frontLW); g2.draw(backRW); g2.draw(frontRW); g2.draw(topFront); g2.draw(bottomFront); } private int x; private int y; private int width; }
ShapeIcon.java ///////////////////////////////////////////////////////////////////////////////////////////////////
import java.awt.*; import java.util.*; import javax.swing.*; /** An icon that contains a moveable shape. */ public class ShapeIcon implements Icon { public ShapeIcon(MoveableShape shape, int width, int height) { this.shape = shape; this.width = width; this.height = height; } public int getIconWidth() { return width; } public int getIconHeight() { return height; } public void paintIcon(Component c, Graphics g, int x, int y) { Graphics2D g2 = (Graphics2D)g; shape.draw(g2); } private int width; private int height; private MoveableShape shape; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started