-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainPanel.java
More file actions
83 lines (55 loc) · 2.14 KB
/
MainPanel.java
File metadata and controls
83 lines (55 loc) · 2.14 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
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.Timer;
@SuppressWarnings("serial")
public class MainPanel extends Component implements ActionListener {
Timer timer = new Timer(10, this);
public MainPanel() {
super();
this.setPreferredSize(new Dimension(800, 800));
timer.start();
}
@Override
public void paint(Graphics graphics) {
Graphics2D g = (Graphics2D)graphics;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
AffineTransform transform = g.getTransform();
transform.scale(this.getWidth() / (Model.MAX_SIZE * 2) , this.getHeight() / (Model.MAX_SIZE * 2));
transform.translate(Model.MAX_SIZE, Model.MAX_SIZE);
transform.scale(.9, .9);
g.setTransform(transform);
g.setColor(Color.WHITE);
g.fillRect(-Model.MAX_SIZE, -Model.MAX_SIZE, Model.MAX_SIZE * 2, Model.MAX_SIZE * 2);
for(Agent agent : Main.model.agents)
{
g.setColor(Color.RED);
Shape circle = new Ellipse2D.Double(agent.location.getX() - Agent.RADIUS, agent.location.getY() - Agent.RADIUS, Agent.RADIUS * 2, Agent.RADIUS * 2);
g.fill(circle);
Line2D line = new Line2D.Double(agent.location.getX(), agent.location.getY(), agent.location.getX() + Math.cos(agent.heading) * Agent.RADIUS * 1.5, agent.location.getY() + Math.sin(agent.heading) * Agent.RADIUS * 1.5);
g.setStroke(new BasicStroke(.1f));
g.setColor(Color.BLACK);
g.draw(line);
Line2D lineDestination = new Line2D.Double(agent.location.getX(), agent.location.getY(), agent.destination.getX(), agent.destination.getY());
g.setStroke(new BasicStroke(.05f));
g.setColor(Color.GRAY);
g.draw(lineDestination);
}
super.paint(g);
}
@Override
public void actionPerformed(ActionEvent arg0) {
repaint();
Main.model.update();
}
}