import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
class Board extends JPanel {
Point currentPosition, temp;
Point topVertex, bottomLeftVertex, bottomRightVertex;
int random;
public Board(int h, int w) {
// setPreferredSize(new Dimension(w, h));
setSize(w, h);
// Math.random() returns a double between 0 and 1, cast it as an int
// Needs to be a pixel location within your JPanel hence * w and * h. Add 1
// It always rounds down when casting to an int, so need to add 1
currentPosition = new Point((int) (Math.random() * w) + 1,
(int) (Math.random() * h) + 1);
// set the Vertex Points
topVertex = new Point(w / 2, 0);
bottomLeftVertex = new Point(0, h);
bottomRightVertex = new Point(w, h);
}
public void paintComponent(Graphics g){
super.paintComponents(g);
for (int i = 0; i < 100000; i++){
// For each iteration get a random number 1>= and >=3
random = (int) (Math.random() * 3 + 1);
// Random assignment of Vertex points to the random number generated
if (random == 1){
// Cause I can
g.setColor(Color.BLACK);
// Get the Point to be Plotted, Notice the currentPosition is calling the method
temp = currentPosition.midPoint(topVertex);
// To act as a point
g.fillRect(temp.getX(), temp.getY(), 1, 1);
// Make sure to reassign the currentPosition
currentPosition = temp;
}
if (random == 2){
g.setColor(Color.RED);
temp = currentPosition.midPoint(bottomLeftVertex);
g.fillRect(temp.getX(), temp.getY(), 1, 1);
currentPosition = temp;
}
if (random == 3){
g.setColor(Color.GREEN);
temp = currentPosition.midPoint(bottomRightVertex);
g.fillRect(temp.getX(), temp.getY(), 1, 1);
currentPosition = temp;
}
}
}
/*
* public void addPoint(Point p) { ArrayList<Point> point = new ArrayList();
* points.add(p); }
*
* public void paint(Graphics g) { super.paint(g);
*
* //int i = 0;
*
* /* while (i < 10000) { // ?? board.add(); i++; }
*
* }
*/
}