1 import java
.util
.Observable
;
2 import java
.util
.Observer
;
3 import java
.util
.HashMap
;
10 implements Observer
, Runnable
{
12 final static long serialVersionUID
= 1L;
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
);
39 this(wsize
, wsize
, 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];
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
)
62 if (yPoints
[c
] > yMax
)
66 this.xMax2yMaxRatio
= (double)xMax
/(double)yMax
;
68 wXSize
= (int)((double)xMax
/(double)yMax
* (double)wxsize
);
71 createJFrame("Custom...", this, wXSize
, wYSize
);
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
);
86 jframe
.setSize(new Dimension(wxsize
, wysize
));
87 jframe
.setVisible(true);
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
);
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
;
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
++) {
127 (int)((double)coordinate
.getCoordinates(
128 env
.getMinTour(c
) ).getFirst() * xScale
) + borderSize
;
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
);
142 g2
.drawString("Minimum Length: " +
143 env
.getMinTourLength() +
145 env
.getMinTourIteration(),
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
);
159 cpr
= (int)Math
.round(d
.getWidth()/wXSize
* cityPointRadius
);
164 for (int c
= 0; c
< graph
.getNumOfCities(); c
++) {
165 g2
.fillOval(xPoints
[c
] - cpr
/2,
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());