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
;
14 implements Observer
, Runnable
{
16 final static long serialVersionUID
= 1L;
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
) {
37 (int)(Toolkit
.getDefaultToolkit().getScreenSize().height
* 0.9),
38 (int)(Toolkit
.getDefaultToolkit().getScreenSize().height
* 0.9));
42 AntViewObservable avo
,
44 this(avo
, wsize
, wsize
);
48 AntViewObservable 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];
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
);
76 createJFrame("Custom...", this, wXSize
, wYSize
);
78 Dimension d
= this.getSize();
80 scalePoints(d
, this.CityXPoints
, this.CityYPoints
);
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
);
96 jframe
.setSize(new Dimension(wxsize
, wysize
));
97 jframe
.setVisible(true);
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
;
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
++) {
114 (int)((double)xpoints
[c
] * xScale
) + borderSize
;
116 for (int c
= 0; c
< ypoints
.length
; c
++) {
118 d
.height
- (int)((double)ypoints
[c
] * yScale
) - borderSize
;
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
);
143 public void update(Graphics g
) {
145 Graphics2D g2
= (Graphics2D
)g
;
146 Dimension d
= this.getSize();
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: " +
176 avo
.getGlobalBestTourIteration(),
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
);
193 cpr
= (int)Math
.round(d
.getWidth()/wXSize
* cityPointRadius
);
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
,
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
) {
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());