Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The following code is from Ghost 2 Class: import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics2D; import java.io.PrintStream; import imagePackage.RasterImage; public class Ghost2 { // CLASS FIELDS

image text in transcribedimage text in transcribed

The following code is from Ghost 2 Class:

import java.awt.BasicStroke;

import java.awt.Color;

import java.awt.Graphics2D;

import java.io.PrintStream;

import imagePackage.RasterImage;

public class Ghost2 {

// CLASS FIELDS =======================================

private int x;

private int y;

private int size;

private int eyeRad;

private int pupilRad;

private Color normalCol;

private boolean frightened;

private boolean eaten;

private int direction;

// CLASS CONSTRUCTORS =================================

/**

* Default Constructor

*/

public Ghost2() {

this.x=0;

this.y=0;

this.size=100;

this.eyeRad=size/8;

this.pupilRad=this.eyeRad/2;

this.normalCol=Color.CYAN;

this.frightened=false;

this.eaten=false;

this.direction=Game.EAST;

}

/**

* Custom Constructor #1

*/

public Ghost2(int x, int y, int size) {

this.x=x;

this.y=y;

this.size=size;

this.eyeRad=size/8;

this.pupilRad=this.eyeRad/2;

this.normalCol=Color.CYAN;

this.frightened=false;

this.eaten=false;

this.direction=Game.EAST;

}

/**

* Custom Constructor #2

*

*/

public Ghost2(int x, int y, int size, Color body, int direction) {

this.x=x;

this.y=y;

this.size=size;

this.eyeRad=size/8;

this.pupilRad=this.eyeRad/2;

this.normalCol=body;

this.frightened=false;

this.eaten=false;

this.direction=direction;

}

// CLASS METHODS ======================================

/**

* A method that outputs the state of a Ghost1 object (as per the lab3 pdf output examples)

*

*/

public String toString() {

String state;

if(frightened) {

if(eaten) {

state="eaten";

}else {

state="frightened";

}

}else {

state="normal";

}

return String.format("Ghost @ (%d, %d): \t[ size = %d * %d ] \t[ colour = %s] ] \t[ state = %s ]",

this.x,this.y,this.size,this.size,this.normalCol.toString(),state);

}

// GETTERS =========

public int getX() {

return x;

}

public int getY() {

return y;

}

public Color getBodyCol() {

return normalCol;

}

public int getSize() {

return size;

}

public int getDirection() {

return direction;

}

public boolean isFrightened() {

return frightened;

}

public boolean isEaten() {

return eaten;

}

// SETTERS =========

public void setX(int x) {

this.x= x;

}

public void setY(int y) {

this.y= y;

}

public void setBodyCol(Color c) {

this.normalCol=c;

}

public void setDirection(int d) {

this.direction=d;

}

public void setFrightened(boolean f) {

this.frightened=f;

}

public void setEaten(boolean e) {

this.eaten=e;

}

// MAIN METHOD === (not needed, use Ghost2Client.java instead) ============

public static void main(String[] args) {

/*

*

* In this task, you will copy over the code from Q1, rename constructors, then

* modify the class:

*

* first, by locking off the fields of the ghost so they are not public,

*

* then by adding getters/setters to support public access and mutation of some of these

* fields (as specified by the instructions for Q2 in the lab3 pdf, and UML diagram)

*/

}

}

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Question 03 (render \& move methods): Ghost3.java In this task, add a render() method to your Ghost 2 class to create the Ghost 3 class below. Make sure you copy over the code from part2, and rename the constructors to Ghost 3(..). The render() method will likely take the most time for this lab (please do not do this at the last minute). Note that to render a Ghost 3 object, the size parameters and body colour must be according to the properties of that object. The proportions are roughly set out in the first figure of this document (you do not have to adhere to this exactly, but the proportions should help you figure out relative coordinates for the Ghost). Note also, that the direction property is indicated by the location of the pupil in the eye - i.e. NORTH would have the ghost looking UP (pupils at top centre), EAST would have the pupils looking to the right, WEST pupils would be looking left, and SOUTH pupils would look down. If the ghost is frightened (but not eaten), the ghost would appear to have a dark gray body, pupils centred, and a scared mouth (which is not present in normal state). If the ghost is frightened and eaten, then only the eyes of the frightened ghost would be rendered (see figure above). You have some creative license here, the look does not have to be EXACT. - In the main method of the Game.java file (Game class), create a Raster Image object with a size of 800 (h) x 800 (w) pixels. - Include a calls to create four Ghost 3 objects (for each of the 4 ghosts in the game: i.e. "shadow", "pinky", "inky" and "clyde"). Position each in the middle of each of the 4 quadrants of the RasterImage window (e.g. "shadow" is in the top left), and ensure set their sizes to 1/4 of the size of the RasterImage window. Note, if you change their size (they should scale accordingly, but still be positioned in the centre of each quadrant). - Store these objects in a single array (i.e. has 4 elements). - Now make calls to render() on every element in the array. It is useful to clear the screen inside your draw method before drawing, that way if you change a property and call render(), any previously drawn versions will be removed from the screen The result should look something like this (for a Raster Image of size 800800 ) assuming the ghosts are all initially in a non-frightened, and non-eaten state. You can experiment with the frightened/eaten states by using the mutator for each to set and redraw. Optionally (if time permits), you may also add a move method (to mutate the position of the Ghost 3 object left/right/up/down on the screen by a certain amount of pixels, where the char "dir" is either ' L ', 'R', 'U', or ' D ' indicating left, right, up or down respectively); and "step" represents the number of pixels to increment in the move in that direction. The method should return a boolean to indicate if the Ghost 3 object's position (x,y) is on the screen (true) or off the screen (false). [Hint: you can use RasterImage class methods to test this]. Test your move method by initializing the positions of the elements as above, and then invoking the move method on public boolean isEaten() return eaten; // SETTERS ======== public void setX(int x){ this.x= x; public void setY(int y)\{ this.y= y; public void setBodycol(Color c) this.normalCol=c; \} this public void setDirection(int d) \{ this.direction=d; public void setFrightened(boolean f){ \} this.frightened=f; public void setEaten(boolean e) \{ \} this.eaten=e; // MAIN METHOD === (not needed, use Ghost2Client.java instead) ============ public static void main(String[] args) \{ / * In this task, you will copy over the code from Q1, rename constructors, then * modify the class: * first, by locking off the fields of the ghost so they are not public, * then by adding getters/setters to support public access and mutation of some of these * fields (as specified by the instructions for Q2 in the lab3 pdf, and UML diagram) / \} Question 03 (render \& move methods): Ghost3.java In this task, add a render() method to your Ghost 2 class to create the Ghost 3 class below. Make sure you copy over the code from part2, and rename the constructors to Ghost 3(..). The render() method will likely take the most time for this lab (please do not do this at the last minute). Note that to render a Ghost 3 object, the size parameters and body colour must be according to the properties of that object. The proportions are roughly set out in the first figure of this document (you do not have to adhere to this exactly, but the proportions should help you figure out relative coordinates for the Ghost). Note also, that the direction property is indicated by the location of the pupil in the eye - i.e. NORTH would have the ghost looking UP (pupils at top centre), EAST would have the pupils looking to the right, WEST pupils would be looking left, and SOUTH pupils would look down. If the ghost is frightened (but not eaten), the ghost would appear to have a dark gray body, pupils centred, and a scared mouth (which is not present in normal state). If the ghost is frightened and eaten, then only the eyes of the frightened ghost would be rendered (see figure above). You have some creative license here, the look does not have to be EXACT. - In the main method of the Game.java file (Game class), create a Raster Image object with a size of 800 (h) x 800 (w) pixels. - Include a calls to create four Ghost 3 objects (for each of the 4 ghosts in the game: i.e. "shadow", "pinky", "inky" and "clyde"). Position each in the middle of each of the 4 quadrants of the RasterImage window (e.g. "shadow" is in the top left), and ensure set their sizes to 1/4 of the size of the RasterImage window. Note, if you change their size (they should scale accordingly, but still be positioned in the centre of each quadrant). - Store these objects in a single array (i.e. has 4 elements). - Now make calls to render() on every element in the array. It is useful to clear the screen inside your draw method before drawing, that way if you change a property and call render(), any previously drawn versions will be removed from the screen The result should look something like this (for a Raster Image of size 800800 ) assuming the ghosts are all initially in a non-frightened, and non-eaten state. You can experiment with the frightened/eaten states by using the mutator for each to set and redraw. Optionally (if time permits), you may also add a move method (to mutate the position of the Ghost 3 object left/right/up/down on the screen by a certain amount of pixels, where the char "dir" is either ' L ', 'R', 'U', or ' D ' indicating left, right, up or down respectively); and "step" represents the number of pixels to increment in the move in that direction. The method should return a boolean to indicate if the Ghost 3 object's position (x,y) is on the screen (true) or off the screen (false). [Hint: you can use RasterImage class methods to test this]. Test your move method by initializing the positions of the elements as above, and then invoking the move method on public boolean isEaten() return eaten; // SETTERS ======== public void setX(int x){ this.x= x; public void setY(int y)\{ this.y= y; public void setBodycol(Color c) this.normalCol=c; \} this public void setDirection(int d) \{ this.direction=d; public void setFrightened(boolean f){ \} this.frightened=f; public void setEaten(boolean e) \{ \} this.eaten=e; // MAIN METHOD === (not needed, use Ghost2Client.java instead) ============ public static void main(String[] args) \{ / * In this task, you will copy over the code from Q1, rename constructors, then * modify the class: * first, by locking off the fields of the ghost so they are not public, * then by adding getters/setters to support public access and mutation of some of these * fields (as specified by the instructions for Q2 in the lab3 pdf, and UML diagram) / \}

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_2

Step: 3

blur-text-image_3

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

Graph Database Modeling With Neo4j

Authors: Ajit Singh

2nd Edition

B0BDWT2XLR, 979-8351798783

More Books

Students also viewed these Databases questions