2
2
import java .awt .*;
3
3
4
4
class Dot {
5
+ private JFrame frame ;
5
6
private int positionX , positionY , velocityX , velocityY , accelerationX , accelerationY ;
6
7
private Color dotColor ;
7
- int canvasWidth = 800 , canvasHeight = 800 ;
8
+ private Painter painter ;
8
9
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 ;
12
14
this .velocityX = 0 ;
13
15
this .velocityY = 0 ;
14
16
this .accelerationX = 0 ;
15
17
this .accelerationY = 0 ;
16
18
this .dotColor = Color .black ;
19
+ painter = new Painter ();
17
20
}
18
21
19
- public int getPositionX (){
22
+ // Begin Getters
23
+ public int getPositionX () {
20
24
return positionX ;
21
25
}
22
26
@@ -39,16 +43,40 @@ public int getAccelerationX() {
39
43
public int getAccelerationY () {
40
44
return accelerationY ;
41
45
}
46
+ // End Getters
42
47
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 ;
45
51
}
46
52
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 ) {
49
68
g .setColor (dotColor );
50
69
g .fillOval (getPositionX (), getPositionY (), 5 , 5 );
51
70
g .drawOval (getPositionX (), getPositionY (), 5 , 5 );
52
71
}
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
+ }
53
81
}
54
82
}
0 commit comments