Skip to content

Commit bbe766a

Browse files
author
Joseph Kerkhof
committed
obstacles now draw on canvas
1 parent acfb767 commit bbe766a

File tree

3 files changed

+119
-33
lines changed

3 files changed

+119
-33
lines changed

.idea/workspace.xml

Lines changed: 81 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Obstacle.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.awt.*;
2+
3+
public class Obstacle {
4+
private int obstacleX, obstacleY, width, height;
5+
6+
Obstacle(int obstacleX, int obstacleY, int width, int height) {
7+
this.obstacleX = obstacleX;
8+
this.obstacleY = obstacleY;
9+
this.width = width;
10+
this.height = height;
11+
}
12+
13+
// Method for drawing the dot
14+
public void drawObstacles(Graphics g) {
15+
g.setColor(Color.blue);
16+
g.drawRect(obstacleX, obstacleY, width, height);
17+
g.fillRect(obstacleX, obstacleY, width, height);
18+
}
19+
}

src/Worker.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
import javax.swing.*;
44

55
class Worker extends JFrame {
6-
static Dot[] dots;
7-
static Goal goal;
6+
Dot[] dots;
7+
Goal goal;
8+
private Obstacle[] obstacles;
89
private static int windowWidth, windowHeight, numberOfDots, numberOfDirections;
910
private int generation;
1011

@@ -26,6 +27,9 @@ class Worker extends JFrame {
2627
// Make the goal
2728
this.goal = new Goal(windowWidth, windowHeight);
2829

30+
// Make the obstacles
31+
this.obstacles = makeObstacles();
32+
2933
// Create and show the window
3034
this.setPreferredSize(new Dimension(windowWidth, windowHeight));
3135
this.pack();
@@ -49,6 +53,11 @@ public void paint(Graphics g) {
4953
for (Dot dot : dots) {
5054
dot.drawDot(g);
5155
}
56+
57+
// Draws all the obstacles
58+
for (Obstacle obstacle : obstacles) {
59+
obstacle.drawObstacles(g);
60+
}
5261
}
5362

5463
public static Dot[] makeDots(int numberOfDots, int numberOfDirections) {
@@ -169,4 +178,12 @@ public void mutateDemBabies() {
169178
dot.brain.mutate();
170179
}
171180
}
181+
182+
private Obstacle[] makeObstacles() {
183+
Obstacle[] obstacles = new Obstacle[1];
184+
185+
obstacles[0] = new Obstacle(0, 400, 400, 50);
186+
187+
return obstacles;
188+
}
172189
}

0 commit comments

Comments
 (0)