Add second year

This commit is contained in:
2023-12-07 01:19:12 +00:00
parent 3291e5c79e
commit 3d12031ab8
1168 changed files with 431409 additions and 0 deletions

View File

@ -0,0 +1,41 @@
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class GameObject {
// member data
private int x, y;
private int height = 50, width = 50;
private Color c;
private int stepSize = 10; // how many pixels the object will move at a time
// constructor
public GameObject() {
// randomly generating colour
c = new Color((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255));
// randomly generating initial x y co-ordinates
x = (int)(Math.random()*600);
y = (int)(Math.random()*600);
}
// public interface
public void move() {
// generating either a 1 or a 0 to determine if the square should move left or right and up or down - 1 = left/up, 0 = right/down
int xDirection = (int)(Math.random()*2);
int yDirection = (int)(Math.random()*2);
// changing the x & y co-ordinates by either plus or minus the stepSize, depending on the direction
x += (xDirection == 1) ? -stepSize : + stepSize;
y += (yDirection == 1) ? -stepSize : + stepSize;
}
public void paint(Graphics g) {
// setting colour
g.setColor(c);
// make a filled rectangle with the specified dimensions at the specified x, y co-ordinates
g.fillRect(x, y, height, width);
}
}

View File

@ -0,0 +1,76 @@
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class MovingSquaresApplication extends JFrame implements Runnable {
// window dimensions
private static final Dimension WindowSize = new Dimension(600,600);
// array of gameobjects (squares)
private GameObject gameobjects[] = new GameObject[100];
// constructor
public MovingSquaresApplication() {
// create & set up the window
this.setTitle("Moving Squares Apllication");
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);
// create array of game objects.
//Arrays.fill(gameobjects, 100);
for (int i = 0; i < 100; i++) {
gameobjects[i] = new GameObject();
}
// creating a new thread & starting it
Thread t = new Thread(this);
t.start();
}
// run method called by thread
public void run() {
while (true) {
// iterating over array of gameobjects & calling move() on each object
for (GameObject go : gameobjects) {
go.move();
}
try {
// sleeping
Thread.sleep(100);
// catching exception
} catch (InterruptedException e) {
e.printStackTrace();
}
// repainting now that every object has been moved
this.repaint();
}
}
// paint method
public void paint(Graphics g) {
// filling the screen with a white square between "frames" of animation to get rid of trails caused by moving objects
Color c = new Color(255, 255, 255);
g.setColor(c);
g.fillRect(0, 0, 1000, 1000);
// iterating over each GameObject and calling paint on it
for (GameObject go : gameobjects) {
go.paint(g);
}
}
public static void main(String[] args) {
// instantiating class in main method to begin application
MovingSquaresApplication w = new MovingSquaresApplication();
}
}