Skip to content

Commit 43acf3c

Browse files
author
Joseph Kerkhof
committed
The dot MOVES
1 parent 37044a1 commit 43acf3c

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

src/Dot.java

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,25 @@
22
import java.awt.*;
33

44
class Dot {
5+
private JFrame frame;
56
private int positionX, positionY, velocityX, velocityY, accelerationX, accelerationY;
67
private Color dotColor;
7-
int canvasWidth = 800, canvasHeight = 800;
8+
private Painter painter;
89

9-
Dot() {
10-
this.positionX = canvasWidth / 2;
11-
this.positionY = canvasHeight / 2;
10+
Dot(JFrame frame) {
11+
this.frame = frame;
12+
this.positionX = frame.getWidth() / 2;
13+
this.positionY = frame.getHeight() / 2;
1214
this.velocityX = 0;
1315
this.velocityY = 0;
1416
this.accelerationX = 0;
1517
this.accelerationY = 0;
1618
this.dotColor = Color.black;
19+
painter = new Painter();
1720
}
1821

19-
public int getPositionX(){
22+
// Begin Getters
23+
public int getPositionX() {
2024
return positionX;
2125
}
2226

@@ -39,16 +43,40 @@ public int getAccelerationX() {
3943
public int getAccelerationY() {
4044
return accelerationY;
4145
}
46+
// End Getters
4247

43-
public void drawDot(JFrame frame) {
44-
frame.getContentPane().add(new Component());
48+
// Begin Setters
49+
public void setPositionX(int position) {
50+
this.positionX = position;
4551
}
4652

47-
private class Component extends JComponent{
48-
public void paint(Graphics g){
53+
public void setPositionY(int position) {
54+
this.positionY = position;
55+
}
56+
// End Setters
57+
58+
public void drawDot() {
59+
frame.getContentPane().add(painter);
60+
}
61+
62+
public void moveDot(){
63+
painter.move();
64+
}
65+
66+
private class Painter extends JPanel {
67+
public void paint(Graphics g) {
4968
g.setColor(dotColor);
5069
g.fillOval(getPositionX(), getPositionY(), 5, 5);
5170
g.drawOval(getPositionX(), getPositionY(), 5, 5);
5271
}
72+
73+
public void move() {
74+
int newX = getPositionX() + 1;
75+
int newY = getPositionY() + 1;
76+
setPositionX(newX);
77+
setPositionY(newY);
78+
79+
repaint();
80+
}
5381
}
5482
}

src/PathFinder.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import java.awt.Dimension;
2-
2+
import java.awt.Point;
33
import javax.swing.*;
44

55
class PathFinder {
66
static JFrame frame = new JFrame("PathFindingAI");
77

8-
public static void main(String[] args) {
8+
public static void main(String[] args) throws InterruptedException {
99
displayWindow();
10-
Dot dot = new Dot();
11-
dot.drawDot(frame);
10+
Dot dot = new Dot(frame);
11+
dot.drawDot();
12+
while(true){
13+
dot.moveDot();
14+
Thread.sleep(100);
15+
}
1216
}
1317

1418
private static void displayWindow() {

0 commit comments

Comments
 (0)