public TspWorldPanel(TspPanel tspPanel) {
this.tspPanel = tspPanel;
}
public void resetPanel() {
TravelingSalesmanTour travelingSalesmanTour = tspPanel.getTravelingSalesmanTour();
double srcMinimumX = Double.MAX_VALUE;
double srcMaximumX = -Double.MAX_VALUE;
double srcMinimumY = Double.MAX_VALUE;
double srcMaximumY = -Double.MAX_VALUE;
for (City city : travelingSalesmanTour.getCityList()) {
double x = city.getX();
if (x < srcMinimumX) {
srcMinimumX = x;
}
if (x > srcMaximumX) {
srcMaximumX = x;
}
double y = city.getY();
if (y < srcMinimumY) {
srcMinimumY = y;
}
if (y > srcMaximumY) {
srcMaximumY = y;
}
}
double srcWidth = srcMaximumX - srcMinimumX;
double srcHeight = srcMaximumY - srcMinimumY;
Dimension size = getSize();
double width = size.getWidth();
double height = size.getHeight();
int canvasWidth = (int) Math.ceil(width) + 1;
int canvasHeight = (int) Math.ceil(height) + 1;
canvas = new BufferedImage(canvasWidth, canvasHeight, BufferedImage.TYPE_INT_RGB);
Graphics g = canvas.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, canvasWidth, canvasHeight);
g.setColor(Color.RED);
for (City city : travelingSalesmanTour.getCityList()) {
int x = translateCoordinate(srcMinimumX, srcWidth, width, city.getX());
int y = translateCoordinate(srcMinimumY, srcHeight, height, city.getY());
g.fillRect(x - 1, y - 1, 3, 3);
}
g.setColor(Color.BLACK);
if (travelingSalesmanTour.isInitialized()) {
for (CityAssignment cityAssignment : travelingSalesmanTour.getCityAssignmentList()) {
City city = cityAssignment.getCity();
int x1 = translateCoordinate(srcMinimumX, srcWidth, width, city.getX());
int y1 = translateCoordinate(srcMinimumY, srcHeight, height, city.getY());
City nextCity = cityAssignment.getNextCityAssignment().getCity();
int x2 = translateCoordinate(srcMinimumX, srcWidth, width, nextCity.getX());