Add shortest path panel.
This commit is contained in:
271
src/main/org/insa/graphics/ShortestPathPanel.java
Normal file
271
src/main/org/insa/graphics/ShortestPathPanel.java
Normal file
@@ -0,0 +1,271 @@
|
||||
package org.insa.graphics;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Color;
|
||||
import java.awt.Component;
|
||||
import java.awt.Font;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.swing.Box;
|
||||
import javax.swing.BoxLayout;
|
||||
import javax.swing.ButtonGroup;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JComboBox;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JRadioButton;
|
||||
import javax.swing.JSplitPane;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
|
||||
import org.insa.algo.shortestpath.AStarAlgorithm;
|
||||
import org.insa.algo.shortestpath.BellmanFordAlgorithm;
|
||||
import org.insa.algo.shortestpath.DijkstraAlgorithm;
|
||||
import org.insa.algo.shortestpath.ShortestPathAlgorithm;
|
||||
import org.insa.algo.shortestpath.ShortestPathData.Mode;
|
||||
import org.insa.graph.Graph;
|
||||
import org.insa.graph.Node;
|
||||
import org.insa.graph.io.BinaryGraphReaderV2;
|
||||
import org.insa.graph.io.GraphReader;
|
||||
import org.insa.graph.io.Openfile;
|
||||
import org.insa.graphics.NodesInputPanel.InputChangedEvent;
|
||||
import org.insa.graphics.drawing.BasicDrawing;
|
||||
import org.insa.graphics.drawing.Drawing;
|
||||
|
||||
public class ShortestPathPanel extends JPanel {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 406148710808045035L;
|
||||
|
||||
public class StartActionEvent extends ActionEvent {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 4090710269781229078L;
|
||||
|
||||
protected static final String START_EVENT_COMMAND = "allInputFilled";
|
||||
|
||||
protected static final int START_EVENT_ID = 0x1;
|
||||
|
||||
private final Node origin, destination;
|
||||
private final Mode mode;
|
||||
private final Class<? extends ShortestPathAlgorithm> algoClass;
|
||||
|
||||
public StartActionEvent(Class<? extends ShortestPathAlgorithm> algoClass, Node origin, Node destination,
|
||||
Mode mode) {
|
||||
super(ShortestPathPanel.this, START_EVENT_ID, START_EVENT_COMMAND);
|
||||
this.origin = origin;
|
||||
this.destination = destination;
|
||||
this.mode = mode;
|
||||
this.algoClass = algoClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Origin node associated with this event.
|
||||
*/
|
||||
public Node getOrigin() {
|
||||
return this.origin;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Destination node associated with this event.
|
||||
*/
|
||||
public Node getDestination() {
|
||||
return this.destination;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Mode associated with this event.
|
||||
*/
|
||||
public Mode getMode() {
|
||||
return this.mode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Algorithm class associated with this event.
|
||||
*/
|
||||
public Class<? extends ShortestPathAlgorithm> getAlgorithmClass() {
|
||||
return this.algoClass;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// Map between algorithm names and class, see end of this class for
|
||||
// initialization.
|
||||
private static Map<String, Class<? extends ShortestPathAlgorithm>> SHORTEST_PATH_ALGORITHMS = new HashMap<>();
|
||||
|
||||
// Input panels for node.
|
||||
private NodesInputPanel nodesInputPanel;
|
||||
|
||||
// Component that can be enabled/disabled.
|
||||
private ArrayList<JComponent> components = new ArrayList<>();
|
||||
|
||||
// Start listeners
|
||||
List<ActionListener> startActionListeners = new ArrayList<>();
|
||||
|
||||
public ShortestPathPanel(Drawing drawing, Graph graph) {
|
||||
super();
|
||||
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
|
||||
|
||||
setBorder(new EmptyBorder(15, 15, 15, 15));
|
||||
|
||||
// Set title.
|
||||
JLabel titleLabel = new JLabel("Shortest-Path");
|
||||
titleLabel.setBackground(Color.RED);
|
||||
titleLabel.setHorizontalAlignment(JLabel.LEFT);
|
||||
titleLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||||
Font font = titleLabel.getFont();
|
||||
font = font.deriveFont(Font.BOLD, 18);
|
||||
titleLabel.setFont(font);
|
||||
add(titleLabel);
|
||||
|
||||
add(Box.createVerticalStrut(8));
|
||||
|
||||
// Add algorithm selection
|
||||
JComboBox<String> algoSelect = new JComboBox<>(SHORTEST_PATH_ALGORITHMS.keySet().toArray(new String[0]));
|
||||
algoSelect.setBackground(Color.WHITE);
|
||||
algoSelect.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||||
add(algoSelect);
|
||||
components.add(algoSelect);
|
||||
|
||||
// Add inputs for node.
|
||||
this.nodesInputPanel = new NodesInputPanel(drawing, graph);
|
||||
this.nodesInputPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||||
nodesInputPanel.addTextField("Origin: ", new Color(57, 172, 115));
|
||||
nodesInputPanel.addTextField("Destination: ", new Color(255, 77, 77));
|
||||
|
||||
add(this.nodesInputPanel);
|
||||
components.add(this.nodesInputPanel);
|
||||
|
||||
// Add mode selection
|
||||
JPanel modePanel = new JPanel();
|
||||
modePanel.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||||
modePanel.setLayout(new BoxLayout(modePanel, BoxLayout.LINE_AXIS));
|
||||
JRadioButton lengthModeButton = new JRadioButton("Length");
|
||||
lengthModeButton.setSelected(true);
|
||||
JRadioButton timeModeButton = new JRadioButton("Time");
|
||||
ButtonGroup group = new ButtonGroup();
|
||||
group.add(lengthModeButton);
|
||||
group.add(timeModeButton);
|
||||
modePanel.add(Box.createHorizontalGlue());
|
||||
modePanel.add(lengthModeButton);
|
||||
modePanel.add(Box.createHorizontalGlue());
|
||||
modePanel.add(timeModeButton);
|
||||
modePanel.add(Box.createHorizontalGlue());
|
||||
|
||||
add(modePanel);
|
||||
components.add(timeModeButton);
|
||||
components.add(lengthModeButton);
|
||||
|
||||
// Bottom panel
|
||||
JPanel bottomPanel = new JPanel();
|
||||
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS));
|
||||
JButton startAlgoButton = new JButton("Start");
|
||||
startAlgoButton.setEnabled(false);
|
||||
startAlgoButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
List<Node> nodes = nodesInputPanel.getNodeForInputs();
|
||||
Node origin = nodes.get(0), destination = nodes.get(1);
|
||||
Mode mode = lengthModeButton.isSelected() ? Mode.LENGTH : Mode.TIME;
|
||||
|
||||
for (ActionListener lis: startActionListeners) {
|
||||
lis.actionPerformed(new StartActionEvent(SHORTEST_PATH_ALGORITHMS.get(algoSelect.getSelectedItem()),
|
||||
origin, destination, mode));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
JButton hideButton = new JButton("Hide");
|
||||
hideButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
nodesInputPanel.setEnabled(false);
|
||||
setVisible(false);
|
||||
}
|
||||
});
|
||||
|
||||
bottomPanel.add(startAlgoButton);
|
||||
bottomPanel.add(Box.createHorizontalGlue());
|
||||
bottomPanel.add(hideButton);
|
||||
|
||||
components.add(startAlgoButton);
|
||||
components.add(hideButton);
|
||||
|
||||
bottomPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||||
|
||||
add(Box.createVerticalStrut(8));
|
||||
add(bottomPanel);
|
||||
|
||||
nodesInputPanel.addInputChangedListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
InputChangedEvent evt = (InputChangedEvent) e;
|
||||
boolean allNotNull = true;
|
||||
for (Node node: evt.getNodes()) {
|
||||
if (node == null) {
|
||||
allNotNull = false;
|
||||
}
|
||||
}
|
||||
startAlgoButton.setEnabled(allNotNull);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEnabled(boolean enabled) {
|
||||
super.setEnabled(enabled);
|
||||
nodesInputPanel.setEnabled(enabled);
|
||||
for (JComponent component: components) {
|
||||
component.setEnabled(enabled);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new start action listener to this class.
|
||||
*
|
||||
* @param listener
|
||||
*/
|
||||
public void addStartActionListener(ActionListener listener) {
|
||||
this.startActionListeners.add(listener);
|
||||
}
|
||||
|
||||
static {
|
||||
SHORTEST_PATH_ALGORITHMS.put("Bellman-Ford", BellmanFordAlgorithm.class);
|
||||
SHORTEST_PATH_ALGORITHMS.put("Dijkstra", DijkstraAlgorithm.class);
|
||||
SHORTEST_PATH_ALGORITHMS.put("A*", AStarAlgorithm.class);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
|
||||
String nomcarte = "../BE_Graphe_Maps/morbihan3.mapgr";
|
||||
GraphReader reader = new BinaryGraphReaderV2(Openfile.open(nomcarte));
|
||||
Graph graph = reader.read();
|
||||
|
||||
JFrame frame = new JFrame();
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setLayout(new BorderLayout());
|
||||
JSplitPane p = new JSplitPane();
|
||||
BasicDrawing drawing = new BasicDrawing();
|
||||
JPanel pane = new ShortestPathPanel(drawing, graph);
|
||||
p.setLeftComponent(drawing);
|
||||
p.setRightComponent(pane);
|
||||
p.setResizeWeight(0.8);
|
||||
frame.add(p, BorderLayout.CENTER);
|
||||
frame.show();
|
||||
frame.setSize(800, 600);
|
||||
// pane.setSize(new Dimension(400, 0));
|
||||
|
||||
drawing.drawGraph(graph);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user