Doubt about this Java code: In the third source code (race stage) we have this l
ID: 3703932 • Letter: D
Question
Doubt about this Java code:
In the third source code (race stage) we have this line //randomCar = new Car(rand.nextInt(this.getWidth()-60), rand.nextInt(this.getHeight()-30));
Why do we substract 60 from the width of the car? What does this have to do with the frame? Also in this line from the same source code: return(c.getxPos()+60 >= this.getWidth());
why do we sum 60 to the current position of the car? Why 60? Whats its use?
MAIN
public class Main {
public static void main(String[] args) throws InterruptedException {
JFrame mainFrame = new JFrame();
mainFrame.setSize(800, 600);
mainFrame.setTitle("My First GUI!!!");
RaceStage raceStage = new RaceStage();
mainFrame.add(raceStage);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
raceStage.initStage();
while(!raceStage.someCarWon()) {
mainFrame.repaint();
Thread.sleep(25);
}
CAR CODE
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.Random;
public class Car {
private int xPos;
private int yPos;
private int height;
private int width;
private Color color;
private int direction;
public Car(int xPos, int yPos) {
super();
this.xPos = xPos;
this.yPos = yPos;
this.direction = 1; // Cars initially move right
this.height = 30;
this.width = 60;
this.color = Color.MAGENTA;
}
public int getxPos() {
return xPos;
}
public void setxPos(int xPos) {
this.xPos = xPos;
}
public int getyPos() {
return yPos;
}
public void setyPos(int yPos) {
this.yPos = yPos;
}
public int getDirection() {
return direction;
}
public void setDirection(int direction) {
this.direction = direction;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public static int getInitialWidth() {
return 60;
}
public static int getInitialHeight() {
return 30;
}
public void translate(int deltax, int deltay) {
xPos += deltax;
yPos += deltay;
}
public void draw(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Line2D.Double rearWindow = new Line2D.Double(getxPos()+10,getyPos()+10,getxPos()+20,getyPos()+0);
Line2D.Double frontWindow = new Line2D.Double(getxPos()+40,getyPos()+0,getxPos()+50,getyPos()+10);
Line2D.Double roof = new Line2D.Double(getxPos()+20,getyPos()+0,getxPos()+40,getyPos()+0);
Rectangle body = new Rectangle(getxPos()+0,getyPos()+10,60,10);
Ellipse2D.Double rearTire= new Ellipse2D.Double(getxPos()+10, getyPos()+20, 10, 10);
Ellipse2D.Double frontTire= new Ellipse2D.Double(getxPos()+40, getyPos()+20, 10, 10);
g2.draw(rearWindow);
g2.draw(roof);
g2.draw(frontWindow);
g2.draw(frontTire);
g2.draw(rearTire);
g2.setColor(this.color);
g2.draw(body);
}
}
}
}
RACE CODE:
public class RaceStage extends JComponent {
private Random rand;
//private Car topLeftCar;
private Car[] cars;
// private Car bottomRightCar;
// private Car randomCar;
private int numCars;
public RaceStage() {
//randomCar = new Car(rand.nextInt(this.getWidth()-60), rand.nextInt(this.getHeight()-30));
}
public void initStage() {
numCars = this.getHeight() / (Car.getInitialHeight() + 10);
//numCars = 8;
rand = new Random();
//topLeftCar = new Car(0,0);
cars = new Car[numCars];
int nextY = 0;
for(int i=0; i<numCars; i++) {
cars[i] = new Car(0, nextY);
nextY += 40;
}
}
public boolean reachedRightEdge(Car c) {
return(c.getxPos()+60 >= this.getWidth());
}
public boolean reachedLeftEdge(Car c) {
return(c.getxPos()<=0);
}
public boolean someCarWon() {
for(int i=0; i<numCars; i++) {
if (this.reachedRightEdge(cars[i])) {
return true;
}
}
return false;
}
public int checkWinner() {
int maxIndex = 0;
for (int i=1; i < numCars; i++) {
if (cars[i].getxPos() > cars[maxIndex].getxPos()) {
maxIndex = i;
}
}
return maxIndex;
}
public void paintComponent(Graphics g) {
for (int i=0; i<numCars; i++) {
cars[i].draw(g);
cars[i].translate(rand.nextInt(10), 0);
}
int leadCarIndex = checkWinner();
cars[leadCarIndex].setColor(Color.GREEN);
}
}
Explanation / Answer
Doubt about this Java code: In the third source code (race stage) we have this line //randomCar = new Car(rand.nextInt(this.getWidth()-60), rand.nextInt(this.getHeight()-30)); Answer : In this line we are creating the random int value for the car dimensions. and this line rand.nextInt(this.getWidth()-60) means we are creating the a random int value which is existing in 0 to (this.getwidth() - 60) this is java syntax. But why we take (this.getWidth() - 60) this is because we have our car with width 60 and we want to create x cordinate for car so we have to minus the car width from the getWidth value then we can create a random value between 0 to x cordinate. Why do we substract 60 from the width of the car? What does this have to do with the frame? Also in this line from the same source code: return(c.getxPos()+60 >= this.getWidth()); Answer : and when we return this.getwidth() + 60 That means we return the y cordinate of car where this.getWidth have x cordinate value. why do we sum 60 to the current position of the car? Why 60? Whats its use? Answer : So hope you understand why this program use 60. You can also use any value like 50 or any. But use always a same value. Thanks Hope u understand if not then feel free to comment.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.