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

Last week\'s constructor method code: public MyApp() { //Create and set up the w

ID: 3876742 • Letter: L

Question

Last week's constructor method code:

public MyApp() {

//Create and set up the window.
this.setTitle("Threads and Animation");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Display the window, centred on the screen

Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();

int x = screensize.width/2 - WindowSize.width/2;

int y = screensize.height/2 - WindowSize.height/2;

setBounds(x, y, WindowSize.width, WindowSize.height);

setVisible(true);

}

Week #2 Assignment Create a program which performs simple random animation of coloured squares Use two classes: Threads and Animation MovingSquaresApplication extends JFrame Implements Runnable has main) method Member data includes an array of GameObject instances Constructor method does similar setup as last week's code, plus instantiates the GameObjects in the array, and creates+starts a Thread Uses a Thread to perform animation of the GameObjects by calling their move() methods Paint) method draws the GameObjects by calling their paint(Graphics g) methods GameObject Member data includes x,y,color Constructor method randomises the object's position and color Public move() method is used to randomly alter x,y members Public paint(Graphics g) method draws the object as a square using g.fillRect()

Explanation / Answer

Game class

import javax.swing.*;
import java.awt.*;

public class Game extends JPanel {

//changing these values will change the size of the game, while still remaining functional
//within the size limit specified.
public static final int WINDOW_WIDTH = 600;
public static final int WINDOW_HEIGHT = 400;

//Creates a Square object Array
Square[] squareArray = new Square[20];


public Game() {

//initializes square objects
for (int i = 0; i < squareArray.length; i++)
squareArray[i] = new Square();
}

public void paint(Graphics graphics) {

//makes background black
graphics.setColor(Color.black);
graphics.fillRect(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);

//paints square objects to the screen
for (Square aSquareArray : squareArray) {
aSquareArray.paint(graphics);
}
}

public void update() {

//calls the Square class update method on the square objects
for (Square aSquareArray : squareArray) aSquareArray.update();
}

public static void main(String[] args) throws InterruptedException {

Game game = new Game();
JFrame frame = new JFrame();
frame.add(game);
frame.setVisible(true);
frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setTitle("Raining Multiple Squares");
frame.setResizable(false);
frame.setLocationRelativeTo(null);

while (true) {
game.update();
game.repaint();
Thread.sleep(10);
}
}
}

Square class

import javax.swing.*;
import java.awt.*;
import java.util.Random;

public class Square extends JPanel {

private int squareXLocation;
private int squareSize;
private int squareYLocation = -squareSize;
private int fallSpeed = 1;
Random rand = new Random();

/*
//creates a random value inside the window and stores it in squareXLocation
*/
public int generateRandomXLocation(){
return squareXLocation = rand.nextInt(Game.WINDOW_WIDTH - squareSize);
}

/*
//creates a random value between 1-50 and stores it in squareWidth
*/
public int generateRandomSquareSize(){
return squareSize = rand.nextInt(50);
}

/*
//creates a random value that is not zero and stores it in fallSpeed(so squares do not get stuck at 0 speed)
*/
public int generateRandomFallSpeed(){
return fallSpeed = rand.ints(1, 1, 10).findFirst().getAsInt();
}

/*
//paints the square with the variables generated in the random methods
*/
public void paint(Graphics g){
g.setColor(Color.CYAN);
g.fillRect(squareXLocation,squareYLocation,squareSize,squareSize);
}

/*
//sets the squareWidth and square fallSpeed to a random value for every square created
*/
public Square(){
generateRandomXLocation();
generateRandomSquareSize();
generateRandomFallSpeed();
}

public void update(){

//changes the squares xLocation and fallSpeed if the created square reaches the bottom of the screen
if(squareYLocation >= Game.WINDOW_HEIGHT){
generateRandomXLocation();
generateRandomFallSpeed();
generateRandomSquareSize();
squareYLocation = -squareSize;
}

//moves the square down if the square is inside the window
if(squareYLocation <= Game.WINDOW_HEIGHT){
squareYLocation += fallSpeed;
}
}
}