Clean code.

This commit is contained in:
Holt59 2018-03-06 10:30:43 +01:00
parent 825108d10e
commit e5d1b30cfd
3 changed files with 27 additions and 15 deletions

View File

@ -241,8 +241,17 @@ public class MainWindow extends JFrame {
launchThread(new Runnable() { launchThread(new Runnable() {
@Override @Override
public void run() { public void run() {
// Run the algorithm.
ShortestPathSolution solution = copyAlgorithm.run(); ShortestPathSolution solution = copyAlgorithm.run();
displayShortestPathSolution(solution); // Add the solution to the solution panel (but do not display
// overlay).
spPanel.solutionPanel.addSolution(solution, false);
// If the solution is feasible, add the path to the path panel.
if (solution.isFeasible()) {
pathPanel.addPath(solution.getPath());
}
// Show the solution panel and enable the shortest-path panel.
spPanel.solutionPanel.setVisible(true);
spPanel.setEnabled(true); spPanel.setEnabled(true);
} }
}); });
@ -372,15 +381,6 @@ public class MainWindow extends JFrame {
} }
} }
private void displayShortestPathSolution(ShortestPathSolution solution) {
spPanel.solutionPanel.addSolution(solution, false); // Do not add overlay in the solution
// panel.
if (solution.isFeasible()) {
pathPanel.addPath(solution.getPath());
}
spPanel.solutionPanel.setVisible(true);
}
/** /**
* Notify all listeners that a new graph has been loaded. * Notify all listeners that a new graph has been loaded.
*/ */
@ -456,10 +456,10 @@ public class MainWindow extends JFrame {
mainPanel.setLeftComponent(mapViewDrawing); mainPanel.setLeftComponent(mapViewDrawing);
mainPanel.setDividerLocation(oldLocation); mainPanel.setDividerLocation(oldLocation);
notifyDrawingLoaded(basicDrawing, mapViewDrawing); notifyDrawingLoaded(basicDrawing, mapViewDrawing);
drawing.clear();
((MapViewDrawing) drawing).drawGraph(mfile);
} }
else if (isNewGraph) {
// 2. We draw the graph.
if (isNewGraph) {
drawing.clear(); drawing.clear();
((MapViewDrawing) drawing).drawGraph(mfile); ((MapViewDrawing) drawing).drawGraph(mfile);
} }
@ -476,8 +476,11 @@ public class MainWindow extends JFrame {
mainPanel.setLeftComponent(basicDrawing); mainPanel.setLeftComponent(basicDrawing);
mainPanel.setDividerLocation(oldLocation); mainPanel.setDividerLocation(oldLocation);
notifyDrawingLoaded(mapViewDrawing, basicDrawing); notifyDrawingLoaded(mapViewDrawing, basicDrawing);
this.currentPalette = palette;
drawing.clear();
drawing.drawGraph(graph, palette);
} }
if (isNewGraph || palette != this.currentPalette) { else if (isNewGraph || palette != this.currentPalette) {
this.currentPalette = palette; this.currentPalette = palette;
drawing.clear(); drawing.clear();
drawing.drawGraph(graph, palette); drawing.drawGraph(graph, palette);

View File

@ -319,6 +319,7 @@ public class BasicDrawing extends JPanel implements Drawing {
* *
*/ */
public BasicDrawing() { public BasicDrawing() {
setLayout(null);
this.zoomAndPanListener = new ZoomAndPanListener(this, this.zoomAndPanListener = new ZoomAndPanListener(this,
ZoomAndPanListener.DEFAULT_MIN_ZOOM_LEVEL, 20, 1.2); ZoomAndPanListener.DEFAULT_MIN_ZOOM_LEVEL, 20, 1.2);
@ -421,6 +422,13 @@ public class BasicDrawing extends JPanel implements Drawing {
this.repaint(); this.repaint();
} }
/**
* @return The current ZoomAndPanListener associated with this drawing.
*/
public ZoomAndPanListener getZoomAndPanListener() {
return this.zoomAndPanListener;
}
/** /**
* @param lon * @param lon
* @return * @return

View File

@ -107,7 +107,8 @@ public class ZoomAndPanListener implements MouseListener, MouseMotionListener, M
if (zoomLevel > minZoomLevel) { if (zoomLevel > minZoomLevel) {
zoomLevel--; zoomLevel--;
Point2D p1 = transformPoint(p); Point2D p1 = transformPoint(p);
coordTransform.scale(1 / zoomMultiplicationFactor, 1 / zoomMultiplicationFactor); coordTransform.scale(1 / zoomMultiplicationFactor,
1 / zoomMultiplicationFactor);
Point2D p2 = transformPoint(p); Point2D p2 = transformPoint(p);
coordTransform.translate(p2.getX() - p1.getX(), p2.getY() - p1.getY()); coordTransform.translate(p2.getX() - p1.getX(), p2.getY() - p1.getY());
targetComponent.repaint(); targetComponent.repaint();