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;
16 protected double xScale
;
17 protected double yScale
;
18 protected int[] xPoints
;
19 protected int[] yPoints
;
20 protected int[] xScaledPoints
;
21 protected int[] yScaledPoints
;
22 protected final int border
= 20;
24 protected Environment env
;
25 protected Graph graph
;
26 protected Coordinate coordinate
;
28 public AntView(Environment env
) {
29 this((int)(Toolkit
.getDefaultToolkit().getScreenSize().height
* 0.9),
30 (int)(Toolkit
.getDefaultToolkit().getScreenSize().height
* 0.9), env
);
36 this(wsize
, wsize
, env
);
44 JFrame f
= new JFrame("Calendar");
46 f
.setDefaultCloseOperation(JFrame
.EXIT_ON_CLOSE
);
48 Container cp
= f
.getContentPane();
49 cp
.setBackground(Color
.white
);
50 cp
.add("Center", this);
53 f
.setSize(new Dimension(wxsize
,wysize
));
58 this.graph
= env
.getGraph();
59 this.coordinate
= graph
.getCoordinate();
61 this.xPoints
= new int[graph
.getNumOfCities()];
62 this.yPoints
= new int[graph
.getNumOfCities()];
63 this.xScaledPoints
= new int[graph
.getNumOfCities()];
64 this.yScaledPoints
= new int[graph
.getNumOfCities()];
68 for (int c
= 0; c
< graph
.getNumOfCities(); c
++) {
69 xPoints
[c
] = coordinate
.getCoordinates(c
).getFirst();
70 yPoints
[c
] = coordinate
.getCoordinates(c
).getSecond();
71 if (xPoints
[c
] > xMax
)
73 if (yPoints
[c
] > yMax
)
77 Dimension d
= this.getSize();
80 xScale
= (double)(d
.width
-(border
*2))/(double)xMax
;
81 yScale
= (double)(d
.height
-(border
*2))/(double)yMax
;
83 /* apply scale factor to points */
84 for (int c
= 0; c
< graph
.getNumOfCities(); c
++) {
85 xScaledPoints
[c
] = (int)(xPoints
[c
] * xScale
);
86 xScaledPoints
[c
] += border
;
87 yScaledPoints
[c
] = (int)(yPoints
[c
] * yScale
);
88 yScaledPoints
[c
] += border
;
95 this.update(this.getGraphics());
98 public void paint(Graphics g
) {
99 Graphics2D g2
= (Graphics2D
)g
;
100 HashMap
<RenderingHints
.Key
, Object
> rhmap
= new HashMap
<RenderingHints
.Key
, Object
>(8);
101 rhmap
.put(RenderingHints
.KEY_ANTIALIASING
, RenderingHints
.VALUE_ANTIALIAS_DEFAULT
);
102 rhmap
.put(RenderingHints
.KEY_ALPHA_INTERPOLATION
, RenderingHints
.VALUE_ALPHA_INTERPOLATION_QUALITY
);
103 rhmap
.put(RenderingHints
.KEY_COLOR_RENDERING
, RenderingHints
.VALUE_COLOR_RENDER_QUALITY
);
104 rhmap
.put(RenderingHints
.KEY_DITHERING
, RenderingHints
.VALUE_DITHER_ENABLE
);
105 rhmap
.put(RenderingHints
.KEY_FRACTIONALMETRICS
, RenderingHints
.VALUE_FRACTIONALMETRICS_ON
);
106 rhmap
.put(RenderingHints
.KEY_INTERPOLATION
, RenderingHints
.VALUE_INTERPOLATION_BICUBIC
);
107 rhmap
.put(RenderingHints
.KEY_RENDERING
, RenderingHints
.VALUE_RENDER_QUALITY
);
108 rhmap
.put(RenderingHints
.KEY_TEXT_ANTIALIASING
, RenderingHints
.VALUE_TEXT_ANTIALIAS_ON
);
109 RenderingHints rh
= new RenderingHints(rhmap
);
110 g2
.setRenderingHints(rh
);
115 public void update(Graphics g
) {
117 Graphics2D g2
= (Graphics2D
)g
;
118 Dimension d
= this.getSize();
121 xScale
= (double)(d
.width
-(border
*2))/(double)xMax
;
122 yScale
= (double)(d
.height
-(border
*2))/(double)yMax
;
124 /* apply scale factor to points */
125 for (int c
= 0; c
< graph
.getNumOfCities(); c
++) {
126 xScaledPoints
[c
] = (int)(xPoints
[c
] * xScale
);
127 xScaledPoints
[c
] += border
;
128 yScaledPoints
[c
] = (int)(yPoints
[c
] * yScale
);
129 yScaledPoints
[c
] += border
;
132 int[] minTourXpoints
= new int[graph
.getNumOfCities()+1];
133 int[] minTourYpoints
= new int[graph
.getNumOfCities()+1];
135 for (int c
= 0; c
< graph
.getNumOfCities(); c
++) {
137 (int)((double)coordinate
.getCoordinates( env
.getMinTour(c
) ).getFirst() *
140 (int)((double)coordinate
.getCoordinates( env
.getMinTour(c
) ).getSecond() *
144 minTourXpoints
[graph
.getNumOfCities()] = minTourXpoints
[0];
145 minTourYpoints
[graph
.getNumOfCities()] = minTourYpoints
[0];
147 g2
.clearRect(0, 0, d
.width
, d
.height
);
150 drawTourLines(g2
, minTourXpoints
, minTourYpoints
);
153 g2
.drawString("Minimum Length: " +
154 env
.getMinTourLength() +
156 env
.getMinTourIteration(),
162 public void drawCities(Graphics2D g2
) {
164 g2
.setColor(new Color(0, 0, 128));
166 for (int c
= 0; c
< graph
.getNumOfCities(); c
++) {
167 g2
.fillOval(xScaledPoints
[c
] - 8, yScaledPoints
[c
] - 8, 16, 16);
171 public void drawCityWeb(Graphics2D g2
) {
172 g2
.setColor(new Color(210, 210, 210));
173 g2
.setStroke(new BasicStroke(0.25f
));
175 for (int c1
= 0; c1
< graph
.getNumOfCities(); c1
++) {
176 for (int c2
= 0; c2
< graph
.getNumOfCities(); c2
++) {
177 g2
.drawLine(xScaledPoints
[c1
], yScaledPoints
[c1
], xScaledPoints
[c2
], yScaledPoints
[c2
]);
182 public void drawTourLines(Graphics2D g2
, int[] minTourXpoints
, int[] minTourYpoints
) {
183 g2
.setColor(new Color(200, 20, 20));
184 g2
.setStroke(new BasicStroke(1.75f
));
185 g2
.drawPolyline(minTourXpoints
, minTourYpoints
, graph
.getNumOfCities() + 1);
188 public void update(Observable o
, Object arg
) {
189 this.update(this.getGraphics());