Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Animation exercise Extra Credit CS 210 import java awt. public class Car public

ID: 664915 • Letter: A

Question

Animation exercise Extra Credit CS 210 import java awt. public class Car public static void main (String args) Drawing Panel panel E new Drawing Panel (200 100) panel setBackground (Color.LIGHT GRAY) Graphics g panel. getGraphics car body g. set Color (Color BLACK) g. fill Rect (10, 30, 100 50) a Drawing pane x wheels g. set Color (Color RED File Help g. fill Oval (20, 70, 20, 20) g. filloval (80, 70, 20, 20) window g set Color (Color CYAN) g. fill Rect (80, 40, 30, 20) 7/13/2015 Portions Copyright 2008 by Pearson Education

Explanation / Answer

import aLibrary.*; import java.awt.Color; public class Car { /** whether this Car is selected */ private boolean isSelected; /** the graphics object displayed in the window */ private ARoundRectangle rrect; /** the window where graphics are displayed */ private A3ButtonWindow win; /** the court where cars are displayed */ private Court court; /** the car width */ public final int CAR_WIDTH = 60; /** the car height */ public final int CAR_HEIGHT = 40; /** the car movement increment each time the 'Move' button is pressed */ private final int CAR_INCREMENT = 10; /** stores the direction this Car is moving in */ private int direction; /** constant for right direction */ private static final int RIGHT = 0; /** constant for up direction */ private static final int UP = 1; /** constant for left direction */ private static final int LEFT = 2; /** constant for down direction */ private static final int DOWN = 3; /** * Constructor for objects of class Car *
  • initializes all instance variables of Car class

    *
  • instantiates the RoundRectangle graphics object

    *
  • initializes the color of the graphics object

    *
  • sets graphics object as Filled if this is the selected Car

    *
  • places the graphics object in the window container

* @param upperLeftX the x coordinate of the upper left corner of this Car * @param upperLeftY the y coordinate of the upper left corner of this Car * @param newColor the desired color for this Car * @param newSelect true if this Car is the selected Car, false otherwise * @param newWin a reference variable to the A3ButtonWindow */ public Car( int upperLeftX, int upperLeftY, Color newColor, boolean newSelect, A3ButtonWindow newWin, Court c ) { win = newWin; court = c; isSelected = newSelect; direction = RIGHT; rrect = new ARoundRectangle( upperLeftX, upperLeftY, CAR_WIDTH, CAR_HEIGHT ); // passes the values ( size ) of this car if( isSelected ) rrect.setToFill(); rrect.setColor( newColor ); rrect.place( win ); win.repaint(); } /** @return true if this is the selected Car, false otherwise */ public boolean isSelected(){ return isSelected; } /** sets this Car as the selected Car

* also sets fill and updates the mid button text

* repaints the window */ public void select(){ isSelected = true; rrect.setToFill(); switch ( direction ){ case RIGHT: win.setMidText("Right"); break; case UP: win.setMidText("Up"); break; case LEFT: win.setMidText("Left"); break; case DOWN: win.setMidText("Down"); } win.repaint(); } /** sets this Car as NOT selected

* also sets outline display and repaints the window */ public void deSelect(){ isSelected = false; rrect.setToOutline(); win.repaint(); } /** moves the selected Car in the direction * that is set within its direction variable */ public void moveCar(){ int stepX =0; int stepY=0; Car other = court.getOtherCar(this); boolean tooHighToCollide= ((this.rrect.getY()+CAR_HEIGHT)(other.rrect.getY()+CAR_HEIGHT)) ; // checks if this car is too low to collide if moving other than down boolean farLeftToCollide=(this.rrect.getX()>other.rrect.getX()+CAR_WIDTH); // checks if this car is too left to collide if moving other than left boolean farRightToCollide = ((this.rrect.getX()+CAR_WIDTH) ((this.rrect.getX() + CAR_WIDTH + CAR_INCREMENT))) // tests if the next move left will be beyond the right border of the window { if ( ( ((this.rrect.getX() + CAR_WIDTH + CAR_INCREMENT)< ((other.rrect.getX()))) ||(tooHighToCollide) ||tooLowToCollide ) ) stepX = CAR_INCREMENT; else if (farLeftToCollide) stepX = CAR_INCREMENT; } break; case UP: if ((0) < ((this.rrect.getY()- CAR_INCREMENT))) // tests if the next move up will be beyond the top border of the window if ((this.rrect.getY()-CAR_INCREMENT>(other.rrect.getY()+CAR_HEIGHT)) || (farLeftToCollide) || (farRightToCollide) ) stepY = -CAR_INCREMENT; else if (tooHighToCollide) stepY = -CAR_INCREMENT; break; case LEFT: if ((0) < ((this.rrect.getX()- CAR_INCREMENT))) // tests if the next move left will be beyond the Left border of the window { if(( ((this.rrect.getX() - CAR_INCREMENT)> ((other.rrect.getX()+CAR_WIDTH))) ||(tooHighToCollide) ||tooLowToCollide ) ) stepX =-CAR_INCREMENT; else if (this.rrect.getX() ((this.rrect.getY()+CAR_HEIGHT+ CAR_INCREMENT)- court.BOTTOM_LINE)) // tests if the next move down will be beyond the BottomLine if (((this.rrect.getY()+CAR_INCREMENT+CAR_HEIGHT)