-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathGameVisualizer.java
More file actions
102 lines (96 loc) · 3.18 KB
/
GameVisualizer.java
File metadata and controls
102 lines (96 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package gui;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.*;
import static gui.RobotModel.round;
public class GameVisualizer extends JPanel
{
public RobotModel robotModel = new RobotModel();
public RobotCoordinatesWindow robotCoordinatesWindow = new RobotCoordinatesWindow(robotModel);
private final Timer m_timer = initTimer();
private static Timer initTimer()
{
Timer timer = new Timer("events generator", true);
return timer;
}
public GameVisualizer()
{
m_timer.schedule(new TimerTask()
{
@Override
public void run()
{
onRedrawEvent();
}
}, 0, 50);
m_timer.schedule(new TimerTask()
{
@Override
public void run()
{
robotModel.onModelUpdateEvent();
}
}, 0, 10);
addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e)
{
robotModel.setTargetPosition(e.getPoint());
repaint();
}
});
setDoubleBuffered(true);
}
protected void onRedrawEvent()
{
EventQueue.invokeLater(this::repaint);
}
@Override
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
drawRobot(g2d, round(robotModel.m_robotPositionX), round(robotModel.m_robotPositionY), robotModel.m_robotDirection);
drawTarget(g2d, robotModel.m_targetPositionX, robotModel.m_targetPositionY);
}
private static void fillOval(Graphics g, int centerX, int centerY, int diam1, int diam2)
{
g.fillOval(centerX - diam1 / 2, centerY - diam2 / 2, diam1, diam2);
}
private static void drawOval(Graphics g, int centerX, int centerY, int diam1, int diam2)
{
g.drawOval(centerX - diam1 / 2, centerY - diam2 / 2, diam1, diam2);
}
private void drawRobot(Graphics2D g, int x, int y, double direction)
{
int robotCenterX = round(robotModel.m_robotPositionX);
int robotCenterY = round(robotModel.m_robotPositionY);
AffineTransform t = AffineTransform.getRotateInstance(robotModel.m_robotDirection, robotCenterX, robotCenterY);
g.setTransform(t);
g.setColor(Color.MAGENTA);
fillOval(g, robotCenterX, robotCenterY, 30, 10);
g.setColor(Color.BLACK);
drawOval(g, robotCenterX, robotCenterY, 30, 10);
g.setColor(Color.WHITE);
fillOval(g, robotCenterX + 10, robotCenterY, 5, 5);
g.setColor(Color.BLACK);
drawOval(g, robotCenterX + 10, robotCenterY, 5, 5);
}
private void drawTarget(Graphics2D g, int x, int y)
{
AffineTransform t = AffineTransform.getRotateInstance(0, 0, 0);
g.setTransform(t);
g.setColor(Color.GREEN);
fillOval(g, x, y, 5, 5);
g.setColor(Color.BLACK);
drawOval(g, x, y, 5, 5);
}
}