revert to calling static methods again
[aco.git] / aco / antview / AntView.java
blob1a32c2777c1c9ebf5c3b4c41c2ed7b0b0230b16c
1 package aco.antview;
3 import java.util.Observable;
4 import java.util.Observer;
5 import java.util.TreeSet;
6 import java.util.HashMap;
7 import java.util.Collections;
8 import java.awt.*;
9 import java.applet.*;
10 import javax.swing.*;
12 public class AntView
13 extends Applet
14 implements Observer, Runnable {
16 final static long serialVersionUID = 1L;
18 protected int xMax;
19 protected int yMax;
20 protected int wXSize;
21 protected int wYSize;
22 protected double xScale;
23 protected double yScale;
24 protected double xMax2yMaxRatio;
25 protected int[] TourXPoints;
26 protected int[] TourYPoints;
27 protected int[] CityXPoints;
28 protected int[] CityYPoints;
29 protected final int cityPointRadius = 10;
30 protected final int borderSize = 20;
32 protected JFrame jframe;
33 protected AntViewObservable avo;
35 public AntView(AntViewObservable avo) {
36 this(avo,
37 (int)(Toolkit.getDefaultToolkit().getScreenSize().height * 0.9),
38 (int)(Toolkit.getDefaultToolkit().getScreenSize().height * 0.9));
41 public AntView(
42 AntViewObservable avo,
43 int wsize) {
44 this(avo, wsize, wsize);
47 public AntView(
48 AntViewObservable avo,
49 int wxsize,
50 int wysize) {
52 this.avo = avo;
54 this.CityXPoints = new int[avo.getNumOfCities()];
55 this.CityYPoints = new int[avo.getNumOfCities()];
56 this.TourXPoints = new int[avo.getNumOfCities() + 1];
57 this.TourYPoints = new int[avo.getNumOfCities() + 1];
59 this.xMax = 0;
60 this.yMax = 0;
62 for (int c = 0; c < avo.getNumOfCities(); c++) {
63 CityXPoints[c] = avo.getCoordinates(c).getFirst();
64 CityYPoints[c] = avo.getCoordinates(c).getSecond();
65 if (CityXPoints[c] > xMax)
66 this.xMax = CityXPoints[c];
67 if (CityYPoints[c] > yMax)
68 this.yMax = CityYPoints[c];
71 this.xMax2yMaxRatio = (double)this.xMax/(double)this.yMax;
73 this.wXSize = (int)((double)this.xMax/(double)this.yMax * (double)wxsize);
74 this.wYSize = wysize;
76 createJFrame("Custom...", this, wXSize, wYSize);
78 Dimension d = this.getSize();
79 computeScale(d);
80 scalePoints(d, this.CityXPoints, this.CityYPoints);
82 System.gc();
85 protected void createJFrame(String Title, Component windowComponent, int wxsize, int wysize) {
86 jframe = new JFrame(Title);
88 //jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
89 jframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
91 Container cp = jframe.getContentPane();
92 cp.setBackground(Color.white);
93 cp.add(BorderLayout.CENTER, windowComponent);
95 jframe.pack();
96 jframe.setSize(new Dimension(wxsize, wysize));
97 jframe.setVisible(true);
98 jframe.toFront();
101 protected void computeScale(Dimension d) {
102 if (d.getWidth() > (d.getHeight() * xMax2yMaxRatio)) {
103 xScale = ((d.getHeight() - 2 * borderSize) * xMax2yMaxRatio) / (double)xMax;
104 yScale = (d.getHeight() - 2 * borderSize) / (double)yMax;
105 } else {
106 xScale = (d.getWidth() - 2 * borderSize) / (double)xMax;
107 yScale = ((d.getWidth() - 2 * borderSize) * 1/xMax2yMaxRatio) / (double)yMax;
111 protected void scalePoints(Dimension d, int[] xpoints, int[] ypoints) {
112 for (int c = 0; c < xpoints.length; c++) {
113 xpoints[c] =
114 (int)((double)xpoints[c] * xScale) + borderSize;
116 for (int c = 0; c < ypoints.length; c++) {
117 ypoints[c] =
118 d.height - (int)((double)ypoints[c] * yScale) - borderSize;
122 public void run() {
123 this.update(this.getGraphics());
126 public void paint(Graphics g) {
127 Graphics2D g2 = (Graphics2D)g;
128 HashMap<RenderingHints.Key, Object> rhmap = new HashMap<RenderingHints.Key, Object>(8);
129 rhmap.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_DEFAULT);
130 rhmap.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
131 rhmap.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
132 rhmap.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
133 rhmap.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
134 rhmap.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
135 rhmap.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
136 rhmap.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
137 RenderingHints rh = new RenderingHints(rhmap);
138 g2.setRenderingHints(rh);
140 this.update(g);
143 public void update(Graphics g) {
145 Graphics2D g2 = (Graphics2D)g;
146 Dimension d = this.getSize();
148 computeScale(d);
150 TreeSet<Integer> TourLengths = new TreeSet<Integer>(Collections.reverseOrder());
151 TourLengths.addAll(avo.getGlobalBestTourMap().keySet());
153 if (TourLengths.size() > 0) {
155 g2.clearRect(0, 0, d.width, d.height);
157 int x = TourLengths.size();
158 for (int TourLength : TourLengths) {
160 int[] GlobalBestTour = avo.getGlobalBestTourMap().get(TourLength);
161 for (int c = 0; c < avo.getNumOfCities() + 1; c++) {
162 TourXPoints[c] = avo.getCoordinates(
163 GlobalBestTour[c] ).getFirst();
164 TourYPoints[c] = avo.getCoordinates(
165 GlobalBestTour[c] ).getSecond();
168 scalePoints(d, TourXPoints, TourYPoints);
169 drawTourLines(g2, new Color(200/x, 20/x, 20/(x--)), 1.75f);
172 g2.setColor(new Color(0, 0, 0));
173 g2.drawString("Minimum Length: " +
174 TourLengths.last() +
175 " at Iteration " +
176 avo.getGlobalBestTourIteration(),
177 10, d.height - 10);
181 drawCities(g2);
183 System.gc();
186 public void drawCities(Graphics2D g2) {
187 Dimension d = this.getSize();
189 int cpr = (int)cityPointRadius;
190 if (d.getWidth() > d.getHeight())
191 cpr = (int)Math.round(d.getHeight()/wYSize * cityPointRadius);
192 else
193 cpr = (int)Math.round(d.getWidth()/wXSize * cityPointRadius);
195 if (cpr < 2)
196 cpr = 2;
198 int halfcpr = cpr >> 1;
199 for (int c = 0; c < avo.getNumOfCities(); c++) {
200 g2.setColor(new Color(0, 0, 128));
201 g2.fillOval(CityXPoints[c] - halfcpr,
202 CityYPoints[c] - halfcpr,
203 cpr,
204 cpr);
205 g2.setColor(new Color(0, 128, 0));
206 g2.drawString(Integer.toString(c),
207 CityXPoints[c] + halfcpr,
208 CityYPoints[c] - halfcpr);
213 protected void drawCityWeb(Graphics2D g2) {
214 g2.setColor(new Color(210, 210, 210));
215 g2.setStroke(new BasicStroke(0.25f));
217 for (int c1 = 0; c1 < avo.getNumOfCities(); c1++) {
218 for (int c2 = 0; c2 < avo.getNumOfCities(); c2++) {
219 g2.drawLine(CityXPoints[c1], CityYPoints[c1], CityXPoints[c2], CityYPoints[c2]);
224 protected void drawTourLines(Graphics2D g2, Color color, float strokestrength) {
225 g2.setColor(color);
226 //g2.setColor(new Color(200, 20, 20));
227 g2.setStroke(new BasicStroke(strokestrength));
228 g2.drawPolyline(TourXPoints, TourYPoints, avo.getNumOfCities() + 1);
231 public void update(Observable o, Object arg) {
232 this.update(this.getGraphics());