I have a two classes, one that contains the main method and calls the paint method which is called CityScape and another called Building.
I'm trying to create a cityscape with 5 buildings whose windows are randomly off or on (gray or yellow). The problem is that every time the repaint method is called, the windows randomly change.
How can I make java remember the state of the buildings (i.e. the lights) and make them not change when the repaint method is called (e.g. when the window is resized).
Here is the CityScape class
import javax.swing.*;
import java.awt.*;
public class CityScape extends JPanel {
private Building building1 = new Building(this, new Rectangle(25, 300, 50, 250));
private Building building2 = new Building(this, new Rectangle(125, 350, 100, 200));
private Building building3 = new Building(this, new Rectangle(275, 250, 100, 300));
private Building building4 = new Building(this, new Rectangle(425, 350, 50, 200));
private Building building5 = new Building(this, new Rectangle(525, 400, 100, 150));
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("CITYSCAPE");
frame.setSize(675, 600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CityScape c = new CityScape();
frame.add(c);
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
setBackground(g2d);
paintBuildings(g2d);
}
public void paintBuildings(Graphics2D g2d) {
building1.paint(g2d);
building2.paint(g2d);
building3.paint(g2d);
building4.paint(g2d);
building5.paint(g2d);
}
public void setBackground(Graphics2D g2d) {
g2d.setColor(new Color(255, 102, 25));
g2d.fillRect(0, 0, 1000, 1000);
g2d.setColor(Color.DARK_GRAY);
g2d.fillRect(0, 550, 700, 700);
}
}
and here is the building class:
import java.awt.*;
public class Building {
private CityScape cityScape;
private Rectangle r;
public Building(CityScape cityScape, Rectangle r) {
this.cityScape = cityScape;
this.r = r;
}
public void paint(Graphics2D g2d) { //Draws the windows on the building (either with lights off or on)
int x = 0;
int y = 0;
for (x = 0; x < r.getWidth(); x = x + 25) {
for (y = 0; y < r.getHeight(); y = y + 25) {
if (randBool()) {
g2d.setColor(Color.YELLOW);
g2d.fillRect((int) r.getX() + x, (int) r.getY() + y, 25, 25);
g2d.setColor(Color.WHITE);
g2d.fillRect((int) r.getX(), (int) r.getY() + y, (int) r.getWidth(), 1);
} else {
g2d.setColor(Color.GRAY);
g2d.fillRect((int) r.getX() + x, (int) r.getY() + y, 25, 25);
g2d.setColor(Color.WHITE);
g2d.fillRect((int) r.getX(), (int) r.getY() + y, (int) r.getWidth(), 1);
}
g2d.fillRect((int) r.getX() + x, (int) r.getY(), 1, (int) r.getHeight());
}
}
g2d.fillRect((int) r.getX() + x, (int) r.getY(), 1, (int) r.getHeight());
}
public static boolean randBool() {
return Math.random() < 0.5;
}
}
Thank you