Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Given MoveableShape.java 01: import java.awt.*; 02: 03: /** 04: A shape that can be moved around. 05: */ 06: public interface MoveableShape 07: { 08:

Given

MoveableShape.java

01: import java.awt.*; 02: 03: /** 04:  A shape that can be moved around. 05: */ 06: public interface MoveableShape 07: { 08: /** 09:  Draws the shape. 10:  @param g2 the graphics context 11:  */ 12: void draw(Graphics2D g2); 13: /** 14:  Moves the shape by a given amount. 15:  @param dx the amount to translate in x-direction 16:  @param dy the amount to translate in y-direction 17:  */ 18: void translate(int dx, int dy); 19: }

ShapeIcon.java

01: import java.awt.*; 02: import java.util.*; 03: import javax.swing.*; 04: 05: /** 06:  An icon that contains a moveable shape. 07: */ 08: public class ShapeIcon implements Icon 09: { 10: public ShapeIcon(MoveableShape shape, 11: int width, int height) 12: { 13: this.shape = shape; 14: this.width = width; 15: this.height = height; 16: } 17: 18: public int getIconWidth() 19: { 20: return width; 21: } 22: 23: public int getIconHeight() 24: { 25: return height; 26: } 27: 28: public void paintIcon(Component c, Graphics g, int x, int y) 29: { 30: Graphics2D g2 = (Graphics2D) g; 31: shape.draw(g2); 32: } 33: 34: private int width; 35: private int height; 36: private MoveableShape shape; 37: }

AnimationTester.java

01: import java.awt.*; 02: import java.awt.event.*; 03: import javax.swing.*; 04: 05: /** 06:  This program implements an animation that moves 07:  a car shape. 08: */ 09: public class AnimationTester 10: { 11: public static void main(String[] args) 12: { 13: JFrame frame = new JFrame(); 14: 15: final MoveableShape shape 16: = new CarShape(0, 0, CAR_WIDTH); 17: 18: ShapeIcon icon = new ShapeIcon(shape, 19: ICON_WIDTH, ICON_HEIGHT); 20: 21: final JLabel label = new JLabel(icon); 22: frame.setLayout(new FlowLayout()); 23: frame.add(label); 24: 25: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 26: frame.pack(); 27: frame.setVisible(true); 28: 29: final int DELAY = 100; 30: // Milliseconds between timer ticks 31: Timer t = new Timer(DELAY, new 32: ActionListener() 33: { 34: public void actionPerformed(ActionEvent event) 35: { 36: shape.translate(1, 0); 37: label.repaint(); 38: } 39: }); 40: t.start(); 41: } 42: 43: private static final int ICON_WIDTH = 400; 44: private static final int ICON_HEIGHT = 100; 45: private static final int CAR_WIDTH = 100; 46: }

CarShape.java

01: import java.awt.*; 02: import java.awt.geom.*; 03: import java.util.*; 04: 05: /** 06:  A car that can be moved around. 07: */ 08: public class CarShape implements MoveableShape 09: { 10: /** 11:  Constructs a car item. 12:  @param x the left of the bounding rectangle 13:  @param y the top of the bounding rectangle 14:  @param width the width of the bounding rectangle 15:  */ 16: public CarShape(int x, int y, int width) 17: { 18: this.x = x; 19: this.y = y; 20: this.width = width; 21: } 22: 23: public void translate(int dx, int dy) 24: { 25: x += dx; 26: y += dy; 27: } 28: 29: public void draw(Graphics2D g2) 30: { 31: Rectangle2D.Double body 32: = new Rectangle2D.Double(x, y + width / 6, 33: width - 1, width / 6); 34: Ellipse2D.Double frontTire 35: = new Ellipse2D.Double(x + width / 6, y + width / 3, 36: width / 6, width / 6); 37: Ellipse2D.Double rearTire 38: = new Ellipse2D.Double(x + width * 2 / 3, y + width / 3, 39: width / 6, width / 6); 40: 41: // The bottom of the front windshield 42: Point2D.Double r1 43: = new Point2D.Double(x + width / 6, y + width / 6); 44: // The front of the roof 45: Point2D.Double r2 46: = new Point2D.Double(x + width / 3, y); 47: // The rear of the roof 48: Point2D.Double r3 49: = new Point2D.Double(x + width * 2 / 3, y); 50: // The bottom of the rear windshield 51: Point2D.Double r4 52: = new Point2D.Double(x + width * 5 / 6, y + width / 6); 53: Line2D.Double frontWindshield 54: = new Line2D.Double(r1, r2); 55: Line2D.Double roofTop 56: = new Line2D.Double(r2, r3); 57: Line2D.Double rearWindshield 58: = new Line2D.Double(r3, r4); 59: 60: g2.draw(body); 61: g2.draw(frontTire); 62: g2.draw(rearTire); 63: g2.draw(frontWindshield); 64: g2.draw(roofTop); 65: g2.draw(rearWindshield); 66: } 67: 68: private int x; 69: private int y; 70: private int width; 71: }

Consider the animation code from the end of Chapter 4. Modify the ShapeIcon class so that it displays a collection of objects implementing the MovableShape interface. Modify the animation program (main()) to display 4 moving cars, each starting from a different vertical coordinate

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Finance The Role Of Data Analytics In Manda Due Diligence

Authors: Ps Publishing

1st Edition

B0CR6SKTQG, 979-8873324675

More Books

Students also viewed these Databases questions

Question

Prove that cosine is a continuous function.

Answered: 1 week ago