data for drawing the tsp solutions
[aco.git] / AntView.java
blob41b11027b4082e58a0d261265291a1d0e2571eb9
1 import java.util.Observable;
2 import java.util.Observer;
3 import java.awt.*;
4 import java.applet.*;
5 import javax.swing.*;
7 class AntView
8 extends Applet
9 implements Observer {
11 final static long serialVersionUID = 1L;
13 protected int xMax;
14 protected int yMax;
15 protected double xScale;
16 protected double yScale;
17 protected int[] xPoints;
18 protected int[] yPoints;
19 protected int[] xScaledPoints;
20 protected int[] yScaledPoints;
21 protected final int border = 20;
23 protected Environment env;
24 protected Graph graph;
25 protected Coordinate coordinate;
27 public AntView() {
28 this(0, 0);
31 public AntView(int rect_dim) {
32 this(rect_dim, rect_dim, 10*rect_dim, 10*rect_dim);
35 public AntView(
36 int rect_dim,
37 int wsize) {
38 this(rect_dim, rect_dim, wsize, wsize);
41 public AntView(
42 int xdim,
43 int ydim,
44 int wxsize,
45 int wysize) {
46 XDIM = xdim;
47 YDIM = ydim;
48 POINTS = new Color[xdim][ydim];
49 DATA = new Double[xdim][ydim];
50 for (int x = 0; x < xdim; x++) {
51 for (int y = 0; y < ydim; y++) {
52 POINTS[x][y] = new Color(255,255,255);
53 DATA[x][y] = 0.0;
57 JFrame f = new JFrame("Calendar");
59 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
61 Container cp = f.getContentPane();
62 cp.setBackground(Color.white);
63 cp.add("Center", this);
65 f.pack();
66 f.setSize(new Dimension(wxsize,wysize));
67 f.setVisible(true);
68 f.toFront();
71 public Color getPOINT(int x, int y) {
72 if (x < XDIM && y < YDIM && x >= 0 && y >= 0)
73 return this.POINTS[x][y];
74 else
75 return null;
78 public void setPOINT(int x, int y, Color color) {
79 if (x < XDIM && y < YDIM && x >= 0 && y >= 0)
80 this.POINTS[x][y] = color;
81 else
82 return;
85 public Double getDATA(int x, int y) {
86 if (x < XDIM && y < YDIM && x >= 0 && y >= 0)
87 return this.DATA[x][y];
88 else
89 return null;
92 public void setDATA(int x, int y, Double val) {
93 if (x < XDIM && y < YDIM && x >= 0 && y >= 0)
94 this.DATA[x][y] = val;
95 else
96 return;
99 public void paint(Graphics g) {
100 this.setBackground(Color.getColor("OPAQUE"));
101 this.update(g);
104 public void update() {
105 this.update(this.getGraphics());
108 public void update(Graphics g) {
109 Graphics2D g2 = (Graphics2D)g;
111 Dimension d = getSize();
113 XPSIZE = d.width / XDIM;
114 YPSIZE = d.height / YDIM;
116 XBORDER = XPSIZE/10 + 1;
117 YBORDER = YPSIZE/10 + 1;
118 XPSIZE -= XBORDER;
119 YPSIZE -= YBORDER;
121 for (int x = 0; x < XDIM; x++) {
122 for (int y = 0; y < YDIM; y++) {
123 g2.setColor(POINTS[x][y]);
124 g2.fillRect(x * (XPSIZE + YBORDER),
125 y * (YPSIZE + YBORDER), XPSIZE, YPSIZE);
130 public void calibrate() {
132 double min = Double.MAX_VALUE, max = Double.MIN_VALUE;
134 for (int x = 0; x < XDIM; x++) {
135 for (int y = 0; y < YDIM; y++) {
136 double dval = getDATA(x, y);
137 if (dval < min)
138 min = dval;
139 if (dval > max)
140 max = dval;
144 this.dataRange = max - min;
148 public void calibrate(double max, double min) {
149 this.dataRange = max - min;
152 /* no @Override or x10c will complain! */
153 public void update(Observable o, Object arg) {
155 int[] Tour = (int[])arg;
156 for (int i : Tour) {
157 this.setDATA(i, i, 1.0);
158 this.setPOINT(i, i, new Color( 0 ) );
160 this.update();
165 public void drawCities(Graphics2D g2) {
167 g2.setColor(new Color(0, 0, 128));
169 for (int c = 0; c < graph.getNumOfCities(); c++) {
170 g2.fillOval(xScaledPoints[c] - 8, yScaledPoints[c] - 8, 16, 16);
174 public void drawCityWeb(Graphics2D g2) {
175 g2.setColor(new Color(210, 210, 210));
176 g2.setStroke(new BasicStroke(0.25f));
178 for (int c1 = 0; c1 < graph.getNumOfCities(); c1++) {
179 for (int c2 = 0; c2 < graph.getNumOfCities(); c2++) {
180 g2.drawLine(xScaledPoints[c1], yScaledPoints[c1], xScaledPoints[c2], yScaledPoints[c2]);
185 public void drawTourLines(Graphics2D g2, int[] minTourXpoints, int[] minTourYpoints) {
186 g2.setColor(new Color(200, 20, 20));
187 g2.setStroke(new BasicStroke(1.75f));
188 g2.drawPolyline(minTourXpoints, minTourYpoints, graph.getNumOfCities() + 1);