scale the image according to window heigth if width is larger than
[aco.git] / AntView.java
blob734c2d9f34fe4fbbc8b4e3a011054cf32d768520
1 import java.util.Observable;
2 import java.util.Observer;
3 import java.util.HashMap;
4 import java.awt.*;
5 import java.applet.*;
6 import javax.swing.*;
8 class AntView
9 extends Applet
10 implements Observer, Runnable {
12 final static long serialVersionUID = 1L;
14 protected int xMax;
15 protected int yMax;
16 protected int wXSize;
17 protected int wYSize;
18 protected double xScale;
19 protected double yScale;
20 protected double xMax2yMaxRatio;
21 protected int[] xPoints;
22 protected int[] yPoints;
23 protected final int cityPointRadius = 10;
24 protected final int borderSize = 20;
26 protected JFrame jframe;
27 protected Environment env;
28 protected Graph graph;
29 protected Coordinate coordinate;
31 public AntView(Environment env) {
32 this((int)(Toolkit.getDefaultToolkit().getScreenSize().height * 0.9),
33 (int)(Toolkit.getDefaultToolkit().getScreenSize().height * 0.9), env);
36 public AntView(
37 int wsize,
38 Environment env) {
39 this(wsize, wsize, env);
42 public AntView(
43 int wxsize,
44 int wysize,
45 Environment env) {
47 this.env = env;
48 this.graph = env.getGraph();
49 this.coordinate = graph.getCoordinate();
51 this.xPoints = new int[graph.getNumOfCities() + 1];
52 this.yPoints = new int[graph.getNumOfCities() + 1];
54 this.xMax = 0;
55 this.yMax = 0;
57 for (int c = 0; c < graph.getNumOfCities(); c++) {
58 xPoints[c] = coordinate.getCoordinates(c).getFirst();
59 yPoints[c] = coordinate.getCoordinates(c).getSecond();
60 if (xPoints[c] > xMax)
61 xMax = xPoints[c];
62 if (yPoints[c] > yMax)
63 yMax = yPoints[c];
66 this.xMax2yMaxRatio = (double)xMax/(double)yMax;
68 wXSize = (int)((double)xMax/(double)yMax * (double)wxsize);
69 wYSize = wysize;
71 createJFrame("Custom...", this, wXSize, wYSize);
73 System.gc();
76 protected void createJFrame(String Title, Component windowComponent, int wxsize, int wysize) {
77 jframe = new JFrame(Title);
79 jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
81 Container cp = jframe.getContentPane();
82 cp.setBackground(Color.white);
83 cp.add(BorderLayout.CENTER, windowComponent);
85 jframe.pack();
86 jframe.setSize(new Dimension(wxsize, wysize));
87 jframe.setVisible(true);
88 jframe.toFront();
91 public void run() {
92 this.update(this.getGraphics());
95 public void paint(Graphics g) {
96 Graphics2D g2 = (Graphics2D)g;
97 HashMap<RenderingHints.Key, Object> rhmap = new HashMap<RenderingHints.Key, Object>(8);
98 rhmap.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_DEFAULT);
99 rhmap.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
100 rhmap.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
101 rhmap.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
102 rhmap.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
103 rhmap.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
104 rhmap.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
105 rhmap.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
106 RenderingHints rh = new RenderingHints(rhmap);
107 g2.setRenderingHints(rh);
109 this.update(g);
112 public void update(Graphics g) {
114 Graphics2D g2 = (Graphics2D)g;
115 Dimension d = this.getSize();
117 if (d.getWidth() > (d.getHeight() * xMax2yMaxRatio)) {
118 xScale = ((d.getHeight() - 2 * borderSize) * xMax2yMaxRatio) / (double)xMax;
119 yScale = (d.getHeight() - 2 * borderSize) / (double)yMax;
120 } else {
121 xScale = (d.getWidth() - 2 * borderSize) / (double)xMax;
122 yScale = ((d.getWidth() - 2 * borderSize) * 1/xMax2yMaxRatio) / (double)yMax;
125 for (int c = 0; c < graph.getNumOfCities(); c++) {
126 xPoints[c] =
127 (int)((double)coordinate.getCoordinates(
128 env.getMinTour(c) ).getFirst() * xScale) + borderSize;
129 yPoints[c] =
130 d.height - (int)((double)coordinate.getCoordinates(
131 env.getMinTour(c) ).getSecond() * yScale) - borderSize;
134 xPoints[graph.getNumOfCities()] = xPoints[0];
135 yPoints[graph.getNumOfCities()] = yPoints[0];
137 g2.clearRect(0, 0, d.width, d.height);
139 drawTourLines(g2);
140 drawCities(g2);
142 g2.drawString("Minimum Length: " +
143 env.getMinTourLength() +
144 " at Iteration " +
145 env.getMinTourIteration(),
146 10, d.height - 10);
148 System.gc();
151 public void drawCities(Graphics2D g2) {
152 g2.setColor(new Color(0, 0, 128));
153 Dimension d = this.getSize();
155 int cpr = (int)cityPointRadius;
156 if (d.getWidth() > d.getHeight())
157 cpr = (int)Math.round(d.getHeight()/wYSize * cityPointRadius);
158 else
159 cpr = (int)Math.round(d.getWidth()/wXSize * cityPointRadius);
161 if (cpr <= 1)
162 cpr = 2;
164 for (int c = 0; c < graph.getNumOfCities(); c++) {
165 g2.fillOval(xPoints[c] - cpr/2,
166 yPoints[c] - cpr/2,
167 cpr,
168 cpr);
172 public void drawCityWeb(Graphics2D g2) {
173 g2.setColor(new Color(210, 210, 210));
174 g2.setStroke(new BasicStroke(0.25f));
176 for (int c1 = 0; c1 < graph.getNumOfCities(); c1++) {
177 for (int c2 = 0; c2 < graph.getNumOfCities(); c2++) {
178 g2.drawLine(xPoints[c1], yPoints[c1], xPoints[c2], yPoints[c2]);
183 public void drawTourLines(Graphics2D g2) {
184 g2.setColor(new Color(200, 20, 20));
185 g2.setStroke(new BasicStroke(1.75f));
186 g2.drawPolyline(xPoints, yPoints, graph.getNumOfCities() + 1);
189 public void update(Observable o, Object arg) {
190 this.update(this.getGraphics());