This commit is contained in:
Mikael Capelle
2018-02-16 15:29:11 +01:00
parent 65c81b9921
commit cfb59ac0f1
37 changed files with 1511 additions and 473 deletions

View File

@@ -1,58 +0,0 @@
package org.insa.base ;
/**
* Choix des couleurs pour l'affichage.
*/
import java.awt.* ;
import org.insa.drawing.Drawing;
public class Couleur {
static final Color autoroute = Color.red ;
static final Color bigroute = new Color(255, 105, 0) ;
static final Color tiroute = new Color(255, 234, 0) ;
static final Color cote = Color.blue ;
public static void set(Drawing d, char type) {
// Voir le fichier Descripteur.java pour le type des routes.
switch (type) {
case 'a':
d.setWidth(2) ;
d.setColor(Color.red) ;
break ;
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
d.setWidth(1) ;
d.setColor(bigroute) ;
break ;
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
d.setWidth(1) ;
d.setColor(tiroute) ;
break ;
case 'z':
d.setWidth(4) ;
d.setColor(cote) ;
break ;
default:
d.setWidth(1) ;
d.setColor(Color.black) ;
}
}
}

View File

@@ -0,0 +1,340 @@
package org.insa.base;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.insa.algo.weakconnectivity.WeaklyConnectedComponentGraphicObserver;
import org.insa.algo.weakconnectivity.WeaklyConnectedComponentTextObserver;
import org.insa.algo.weakconnectivity.WeaklyConnectedComponentsAlgorithm;
import org.insa.algo.weakconnectivity.WeaklyConnectedComponentsInstance;
import org.insa.drawing.DrawingVisible;
import org.insa.drawing.graph.BlackAndWhiteGraphPalette;
import org.insa.drawing.graph.GraphDrawing;
import org.insa.drawing.graph.PathDrawing;
import org.insa.graph.Graph;
import org.insa.graph.Path;
import org.insa.graph.io.BinaryGraphReader;
import org.insa.graph.io.BinaryPathReader;
import org.insa.graph.io.MapMismatchException;
import org.insa.graph.io.Openfile;
import com.sun.glass.events.KeyEvent;
public class MainWindow extends JFrame {
public class JOutputStream extends OutputStream {
private JTextArea textArea;
public JOutputStream(JTextArea textArea) {
this.textArea = textArea;
}
@Override
public void write(int b) throws IOException {
// redirects data to the text area
textArea.setText(textArea.getText() + String.valueOf((char)b));
// scrolls the text area to the end of data
textArea.setCaretPosition(textArea.getDocument().getLength());
// keeps the textArea up to date
textArea.update(textArea.getGraphics());
}
}
/**
*
*/
private static final long serialVersionUID = -527660583705140687L;
/**
*
*/
private static final String WINDOW_TITLE = "BE Graphes INSA";
/**
*
*/
private static final Dimension DEFAULT_DIMENSION = new Dimension(800, 600);
// Current graph.
private Graph graph;
// Current loaded path.
private Path currentPath;
// List of item for the top menus.
private JMenuItem openMapItem;
// List of items that cannot be used without a graph
private ArrayList<JMenuItem> graphItems = new ArrayList<JMenuItem>();
// Label containing the map ID of the current graph.
private JLabel mapIdPanel;
// Log stream and print stream
private JOutputStream logStream;
private PrintStream printStream;
/**
*
*/
private DrawingVisible drawing;
public MainWindow() {
super(WINDOW_TITLE);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setLayout(new BorderLayout());
setSize(DEFAULT_DIMENSION);
setJMenuBar(createMenuBar());
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int confirmed = JOptionPane.showConfirmDialog(null,
"Are you sure you want to close the application?", "Exit Confirmation",
JOptionPane.YES_NO_OPTION);
if (confirmed == JOptionPane.YES_OPTION) {
dispose();
System.exit(0);
}
}
});
// Create graph area
JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
drawing = new DrawingVisible();
drawing.setBackground(Color.WHITE);
JTextArea infoPanel = new JTextArea();
infoPanel.setMinimumSize(new Dimension(200, 50));
// infoPanel.setBorder(BorderFactory.createMatteBorder(0, 1, 0, 0, Color.GRAY));
infoPanel.setBackground(Color.WHITE);
infoPanel.setLineWrap(true);
infoPanel.setEditable(false);
this.logStream = new JOutputStream(infoPanel);
this.printStream = new PrintStream(this.logStream);
sp.setResizeWeight(0.8);
// sp.setEnabled(false);
sp.setDividerSize(5);
sp.setBackground(Color.WHITE);
sp.add(drawing);
sp.add(new JScrollPane(infoPanel));
this.add(sp, BorderLayout.CENTER);
this.add(createStatusBar(), BorderLayout.SOUTH);
}
private JMenuBar createMenuBar() {
// Open Map item...
openMapItem = new JMenuItem("Open Map... ",
KeyEvent.VK_O);
openMapItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_O, ActionEvent.ALT_MASK));
openMapItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"Map & compressed map files", "map", "map.gz");
chooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
chooser.setFileFilter(filter);
if (chooser.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION) {
BinaryGraphReader reader;
try {
reader = new BinaryGraphReader(
Openfile.open(chooser.getSelectedFile().getAbsolutePath()));
} catch (IOException e1) {
JOptionPane.showMessageDialog(MainWindow.this, "Cannot open the selected file.");
return ;
}
try {
graph = reader.read();
}
catch (Exception exception) {
JOptionPane.showMessageDialog(MainWindow.this, "Unable to read graph from the selected file.");
return ;
}
drawing.clear();
new GraphDrawing(drawing).drawGraph(graph);
for (JMenuItem item: graphItems) {
item.setEnabled(true);
}
mapIdPanel.setText("Map ID: 0x" + Integer.toHexString(graph.getMapId()));
}
}
});
// Open Path item...
JMenuItem openPathItem = new JMenuItem("Open Path... ", KeyEvent.VK_P);
openPathItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_P, ActionEvent.ALT_MASK));
openPathItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"Path & compressed path files", "path", "path.gz");
chooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
chooser.setFileFilter(filter);
if (chooser.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION) {
BinaryPathReader reader;
try {
reader = new BinaryPathReader(
Openfile.open(chooser.getSelectedFile().getAbsolutePath()));
} catch (IOException e1) {
JOptionPane.showMessageDialog(MainWindow.this, "Cannot open the selected file.");
return ;
}
try {
currentPath = reader.readPath(graph);
}
catch (MapMismatchException exception) {
JOptionPane.showMessageDialog(MainWindow.this, "The selected file does not contain a path for the current graph.");
return;
}
catch (Exception exception) {
JOptionPane.showMessageDialog(MainWindow.this, "Unable to read path from the selected file.");
return ;
}
new PathDrawing(drawing).drawPath(currentPath);
}
}
});
graphItems.add(openPathItem);
// Close item
JMenuItem closeItem = new JMenuItem("Quit", KeyEvent.VK_Q);
closeItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_Q, ActionEvent.ALT_MASK));
closeItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
MainWindow.this.dispatchEvent(new WindowEvent(MainWindow.this, WindowEvent.WINDOW_CLOSING));
}
});
//Build the first menu.
JMenu fileMenu = new JMenu("File");
fileMenu.add(openMapItem);
fileMenu.add(openPathItem);
fileMenu.addSeparator();
fileMenu.add(closeItem);
// Second menu
JMenuItem drawGraphItem = new JMenuItem("Redraw", KeyEvent.VK_R);
drawGraphItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_R, ActionEvent.ALT_MASK));
drawGraphItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
drawing.clear();
drawing.setAutoRepaint(true);
new GraphDrawing(drawing).drawGraph(graph);
drawing.setAutoRepaint(false);
}
});
graphItems.add(drawGraphItem);
JMenuItem drawGraphBWItem = new JMenuItem("Redraw (B&W)", KeyEvent.VK_B);
drawGraphBWItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_B, ActionEvent.ALT_MASK));
drawGraphBWItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
drawing.clear();
drawing.setAutoRepaint(true);
new GraphDrawing(drawing, new BlackAndWhiteGraphPalette()).drawGraph(graph);
drawing.setAutoRepaint(false);
}
});
graphItems.add(drawGraphBWItem);
JMenu graphMenu = new JMenu("Graph");
graphMenu.add(drawGraphItem);
graphMenu.add(drawGraphBWItem);
// Algo menu
JMenu algoMenu = new JMenu("Algorithms");
JMenuItem wccItem = new JMenuItem("Weakly Connected Components");
wccItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
WeaklyConnectedComponentsInstance instance = new WeaklyConnectedComponentsInstance(graph);
WeaklyConnectedComponentsAlgorithm algo = new WeaklyConnectedComponentsAlgorithm(instance);
algo.addObserver(new WeaklyConnectedComponentGraphicObserver(drawing));
(new Thread(algo)).start();
}
});
graphItems.add(wccItem);
algoMenu.add(wccItem);
// Create the menu bar.
JMenuBar menuBar = new JMenuBar();
menuBar.add(fileMenu);
menuBar.add(graphMenu);
menuBar.add(algoMenu);
for (JMenuItem item: graphItems) {
item.setEnabled(false);
}
return menuBar;
}
private JPanel createStatusBar() {
// create the status bar panel and shove it down the bottom of the frame
JPanel statusPanel = new JPanel();
statusPanel.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.GRAY));
statusPanel.setPreferredSize(new Dimension(getWidth(), 20));
statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS));
mapIdPanel = new JLabel();
mapIdPanel.setHorizontalAlignment(SwingConstants.LEFT);
statusPanel.add(mapIdPanel);
return statusPanel;
}
public static void main(final String[] args) {
MainWindow w = new MainWindow();
w.setExtendedState(JFrame.MAXIMIZED_BOTH);
w.setVisible(true);
}
}

View File

@@ -1,115 +0,0 @@
package org.insa.base ;
import java.io.* ;
import java.util.zip.* ;
/* Ne lisez pas cette classe. Lancez javadoc et lisez la doc generee plutot. */
/**
* La classe Openfile permet de lire les fichiers contenant les cartes :
* <ul>
* <li> en trouvant le bon dossier parmi les dossiers pre-configures </li>
* <li> en dezippant automatiquement si besoin </li>
* </ul>
*
*/
public class Openfile {
// Le programme examine chaque dossier dans l'ordre jusqu'a trouver celui qui contient la carte voulue
private static final String[] datadirs =
{ // NE MODIFIEZ PAS CELUI-CI
// car il permet de tester en etant a l'INSA.
"/home/commetud/3eme Annee MIC/Graphes-et-Algorithmes/Maps",
// Celui-ci pour les chemins
"/home/commetud/3eme Annee MIC/Graphes-et-Algorithmes/",
// On cherche aussi dans le sous-repertoire local "Maps" (s'il existe)
"Maps",
// et dans le repertoire courant (Unix uniquement)
".",
// Si vous utilisez votre propre dossier pour les donnees, mettez-le ici.
"/home/votrepropredossier/a/vous",
} ;
// Extension testees. Garder l'extension vide dans la liste.
private static final String[] extensions = { ".map", ".gz", ".map.gz", ".path", ".path.gz", "" } ;
/**
* Ouvre le fichier indiqué et renvoie un DataInputStream sur ce fichier.
* Le fichier ne sera pas ferme avant la fin de l'application.
* @param filename Nom du fichier a ouvrir (sans chemin)
*/
public static DataInputStream open (String filename) {
if (!filename.equals (new File(filename).getName())) {
System.out.println("Le nom du fichier ne doit pas contenir un chemin (ni absolu, ni relatif).") ;
System.out.println("Il doit juste contenir le nom du fichier contenant la carte.") ;
System.out.println("Si vous voulez utiliser un dossier specifique, configurez base/Openfile.java") ;
System.exit(1) ;
}
boolean trouve = false ;
InputStream fileinput = null ;
String fname = null ;
String fullpath = null ;
for (int extn = 0 ; !trouve && extn < extensions.length ; extn++) {
fname = filename + extensions[extn] ;
for (int index = 0 ; !trouve && index < datadirs.length ; index++) {
fullpath = datadirs[index] + File.separator + fname ;
File file = new File(fullpath) ;
if (file.canRead()) {
trouve = true ;
try {
fileinput = new FileInputStream(file) ;
} catch (IOException e) {
e.printStackTrace() ;
System.exit(1) ;
}
}
}
}
if (!trouve) {
// Pas trouve
System.out.println("Impossible de trouver le fichier " + filename) ;
System.out.println(" pourtant j'ai cherche dans les dossiers : ") ;
int existepas = 0 ;
for (int i = 0 ; i < datadirs.length ; i++) {
System.out.println(" - " + datadirs[i]) ;
if (!new File(datadirs[i]).isDirectory()) {
switch (existepas) {
case 0: System.out.println(" (Ce dossier n'existe pas d'ailleurs)") ; break;
case 1: System.out.println(" (Ce dossier n'existe pas non plus)") ; break;
default: System.out.println(" (Celui-la non plus)") ; break;
}
existepas++ ;
}
System.out.println() ;
}
System.exit(1) ;
}
System.out.println("Fichier utilisee : " + fullpath) ;
System.out.println() ;
if (fname.endsWith(".gz")) {
// The file is gzipped.
try {
fileinput = new GZIPInputStream(fileinput) ;
} catch (IOException e) {
e.printStackTrace() ;
System.exit(1) ;
}
}
else {
fileinput = new BufferedInputStream(fileinput) ;
}
return new DataInputStream(fileinput) ;
}
}

View File

@@ -1,86 +0,0 @@
package org.insa.base ;
import java.io.* ;
/* Ne lisez pas cette classe. Lancez javadoc et lisez la doc generee plutot. */
/**
* La classe Readarg facilite la lecture de donnees depuis le clavier ou depuis la ligne de commande.
*
*/
public class Readarg {
private final String[] args ;
private int next ;
// Le Java est le langage prefere des Shadoks.
private final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public Readarg(String[] argz) {
this.args = argz ;
this.next = 0 ;
}
/**
* Obtient une chaine, ou bien depuis la ligne de commande, ou depuis l'entree standard.
* @param msg Message affiche avant de demander la chaine
*/
public String lireString (String msg) {
String resultat = "" ;
System.out.print(msg) ;
if (this.next >= this.args.length) {
try {
resultat = br.readLine () ;
} catch (Exception e) {
System.err.println ("Erreur de lecture de l'entree standard.") ;
System.exit(1) ;
}
}
else {
resultat = this.args[this.next] ;
this.next++ ;
System.out.println (resultat) ;
}
return resultat ;
}
/**
* Obtient un entier, ou bien depuis la ligne de commande, ou depuis l'entree standard.
* @param msg Message affiche avant de demander l'entier
*/
public int lireInt (String msg) {
String lu = lireString (msg) ;
int result = 0 ;
try {
result = Integer.parseInt(lu) ;
}
catch (Exception e) {
System.err.println ("Un entier est attendu mais je lis " + lu) ;
System.exit(1) ;
}
return result ;
}
/**
* Obtient un float, ou bien depuis la ligne de commande, ou depuis l'entree standard.
* @param msg Message affiche avant de demander le float.
*/
public float lireFloat (String msg) {
String lu = lireString (msg) ;
float result = 0 ;
try {
result = Float.parseFloat(lu) ;
}
catch (Exception e) {
System.err.println ("Un reel est attendu mais je lis " + lu) ;
System.exit(1) ;
}
return result ;
}
}

View File

@@ -1,57 +0,0 @@
package org.insa.base ;
import java.io.* ;
import org.insa.drawing.Drawing;
/**
* Fonctions accessoires dont vous n'avez pas a vous servir directement.
*/
public class Utils {
// Calibrer la sortie graphique en fonction de la carte
// Vous pouvez modifier les coordonnees pour ameliorer le rendu.
public static void calibrer(String nomCarte, Drawing dessin) {
if (nomCarte.startsWith("insa")) {
// L'INSA
dessin.setBB (1.462, 1.473, 43.567, 43.5744) ;
}
else if (nomCarte.startsWith("paris")) {
// Ile de la Cité, Paris
dessin.setBB (2.329, 2.372, 48.839, 48.867) ;
}
else if (nomCarte.startsWith("mayot")) {
// Mayotte
dessin.setBB (44.5, 45.5, -13.25, -12.25) ;
}
else if (nomCarte.startsWith("reuni")) {
// La Réunion
dessin.setBB (55.0, 56.0, -21.5, -20.5) ;
}
else if (nomCarte.startsWith("midip")) {
dessin.setBB (-0.6, 3.8, 42.2, 45.3) ;
}
else if (nomCarte.startsWith("franc")) {
dessin.setBB (-5.2, 10.0, 41.0, 51.5) ;
}
else if (nomCarte.startsWith("pfranc")) {
dessin.setBB (-5.2, 10.0, 41.0, 51.5) ;
}
else if (nomCarte.startsWith("morbihan")) {
dessin.setBB (-3.53, -2.452, 47.27, 47.665) ;
}
else if (nomCarte.startsWith("newzealand")) {
dessin.setBB (153.415, 179.912, -47.931, -33.980) ;
}
else if (nomCarte.startsWith("fract") || nomCarte.startsWith("carr")) {
dessin.setBB (-0.05, 1.05, -0.05, 1.05) ;
}
else {
dessin.setBB (-20.0, 50.0, 20.0, 70.0) ;
}
}
}