add load menu and a status line

- don't print to console but add a status line
- add a load menu to load raw
This commit is contained in:
philippe lhardy
2020-10-14 22:21:46 +02:00
parent cc4b03ee4d
commit 6dfcea5647
3 changed files with 130 additions and 55 deletions

View File

@@ -16,11 +16,7 @@ import java.awt.event.ComponentEvent;
import java.awt.event.InputEvent; import java.awt.event.InputEvent;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener; import java.awt.event.MouseMotionListener;
import java.io.DataOutputStream; import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Locale; import java.util.Locale;
import java.util.ResourceBundle; import java.util.ResourceBundle;
@@ -53,10 +49,10 @@ public class Display extends JFrame {
MazeComponent maze; MazeComponent maze;
MazeControler controler; MazeControler controler;
LabyModel model; LabyModel model;
JPanel controlPanel;
boolean autoSize; boolean autoSize;
MazeParams params = null; MazeParams params = null;
JTextField statusField = null;
Maze3dSettings stlsettings; Maze3dSettings stlsettings;
@@ -92,7 +88,7 @@ public class Display extends JFrame {
setVisible(true); setVisible(true);
} }
private static MazeComponent createMazeComponent(LabyModel model, int W, int H) { private MazeComponent createMazeComponent(LabyModel model, int W, int H) {
MazeCellParameters cp = new MazeCellParameters(model.getWidth(), model.getHeight(), W, H, 3, 3); MazeCellParameters cp = new MazeCellParameters(model.getWidth(), model.getHeight(), W, H, 3, 3);
MazeComponent comp = new MazeComponent(model, cp); MazeComponent comp = new MazeComponent(model, cp);
return comp; return comp;
@@ -101,10 +97,8 @@ public class Display extends JFrame {
void recreateModel() { void recreateModel() {
// recreate labyrinth // recreate labyrinth
if (params != null) { if (params != null) {
params = controler.getSettings().resetParams();
// DON'T keep current model... // Keep current model to be able to complete a manualy edited one
// TODO control SEED.
model = new LabyModel(params);
maze.resetWallsProvider(model); maze.resetWallsProvider(model);
model.setMazeListener(maze); model.setMazeListener(maze);
@@ -126,10 +120,18 @@ public class Display extends JFrame {
if (params != null) { if (params != null) {
params = controler.getSettings().resetParams(); params = controler.getSettings().resetParams();
model = new LabyModel(params); model = new LabyModel(params);
refresh();
}
}
void refresh()
{
// reinit labyrinth view
if (params != null) {
maze.resetWallsProvider(model); maze.resetWallsProvider(model);
model.setMazeListener(maze); model.setMazeListener(maze);
// we are in GUI event thread, need to release it and do it outside. // we are in GUI event thread, need to release it and do it outside.
new Thread() { new Thread() {
public void run() { public void run() {
@@ -137,7 +139,6 @@ public class Display extends JFrame {
} }
}.start(); }.start();
} }
} }
@@ -243,7 +244,7 @@ public class Display extends JFrame {
// to allow to log / write somewher into screen... // to allow to log / write somewher into screen...
void writeSentence(String pSentence) { void writeSentence(String pSentence) {
// TODO // TODO
System.out.println(pSentence); addStatus(pSentence);
} }
void writeError(String pError) { void writeError(String pError) {
@@ -254,7 +255,7 @@ public class Display extends JFrame {
Drawing d = createDrawing(); Drawing d = createDrawing();
if (d != null) { if (d != null) {
File outfile = new File(params.getSaveDir(), params.getName() + ".imc"); File outfile = getFileForExtension("imc");
writeSentence("Saving to " + outfile + " ..."); writeSentence("Saving to " + outfile + " ...");
try { try {
DataOutputStream out = new DataOutputStream(new FileOutputStream(outfile)); DataOutputStream out = new DataOutputStream(new FileOutputStream(outfile));
@@ -268,11 +269,16 @@ public class Display extends JFrame {
} }
} }
File getFileForExtension(final String extension)
{
return new File(params.getSaveDir(), params.getName() + "." + extension);
}
void saveSvg() { void saveSvg() {
Drawing d = createDrawing(); Drawing d = createDrawing();
if (d != null) { if (d != null) {
File outfile = new File(params.getSaveDir(), params.getName() + ".svg"); File outfile = getFileForExtension("svg");
writeSentence("Saving to " + outfile + " ..."); writeSentence("Saving to " + outfile + " ...");
try { try {
DataOutputStream out = new DataOutputStream(new FileOutputStream(outfile)); DataOutputStream out = new DataOutputStream(new FileOutputStream(outfile));
@@ -291,7 +297,7 @@ public class Display extends JFrame {
void savePng() { void savePng() {
File file = new File("snapshot.png"); File file = getFileForExtension("png");
// BufferedImage bi = new BufferedImage(this.getSize().width, this.getSize().height, BufferedImage.TYPE_INT_ARGB); // BufferedImage bi = new BufferedImage(this.getSize().width, this.getSize().height, BufferedImage.TYPE_INT_ARGB);
BufferedImage bi = new BufferedImage(maze.getSize().width, maze.getSize().height, BufferedImage.TYPE_INT_ARGB); BufferedImage bi = new BufferedImage(maze.getSize().width, maze.getSize().height, BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics(); Graphics g = bi.createGraphics();
@@ -305,6 +311,11 @@ public class Display extends JFrame {
} }
} }
void addStatus(String pStatus)
{
statusField.setText(pStatus);
}
private class MazeControler extends JPanel { private class MazeControler extends JPanel {
/** /**
@@ -321,13 +332,42 @@ public class Display extends JFrame {
p.setName(pName); p.setName(pName);
} }
private JMenu createLoadingMenu() {
JMenu loadMenu = new JMenu(labels.getString("load") );
final JTextField loadName = new JTextField("newlaby");
loadMenu.add(loadName);
Action loadRawAction = new AbstractAction() {
public void actionPerformed(ActionEvent evt) {
//
addStatus("load raw");
String filename = loadName.getText();
if ((filename.length() > 0)) {
setMazeName(filename);
loadRaw();
refresh();
}
}
};
JButton loadRawButton = new JButton(labels.getString("load" ) + " raw");
loadRawButton.addActionListener(loadRawAction);
loadMenu.add(loadRawButton);
return loadMenu;
}
private JMenu createSavingMenu() { private JMenu createSavingMenu() {
final JTextField saveName = new JTextField("newlaby "); final JTextField saveName = new JTextField("newlaby");
final JButton savePngButton = new JButton(labels.getString("save") + " png"); final JButton savePngButton = new JButton(labels.getString("save") + " png");
Action savePngAction = new AbstractAction() { Action savePngAction = new AbstractAction() {
public void actionPerformed(ActionEvent evt) { public void actionPerformed(ActionEvent evt) {
// //
System.out.println("save png"); addStatus("save png");
setMazeName(saveName.getText()); setMazeName(saveName.getText());
savePng(); savePng();
} }
@@ -346,7 +386,7 @@ public class Display extends JFrame {
Action saveAction = new AbstractAction() { Action saveAction = new AbstractAction() {
public void actionPerformed(ActionEvent evt) { public void actionPerformed(ActionEvent evt) {
// //
System.out.println("save"); addStatus("save");
setMazeName(saveName.getText()); setMazeName(saveName.getText());
MazeParamsFixed p = (MazeParamsFixed) params; MazeParamsFixed p = (MazeParamsFixed) params;
save(p, model); save(p, model);
@@ -357,7 +397,7 @@ public class Display extends JFrame {
Action saveImcAction = new AbstractAction() { Action saveImcAction = new AbstractAction() {
public void actionPerformed(ActionEvent evt) { public void actionPerformed(ActionEvent evt) {
// //
System.out.println("save imc"); addStatus("save imc");
setMazeName(saveName.getText()); setMazeName(saveName.getText());
saveImc(); saveImc();
} }
@@ -367,7 +407,7 @@ public class Display extends JFrame {
Action saveStlAction = new AbstractAction() { Action saveStlAction = new AbstractAction() {
public void actionPerformed(ActionEvent evt) { public void actionPerformed(ActionEvent evt) {
// //
System.out.println("save stl"); addStatus("save stl");
setMazeName(saveName.getText()); setMazeName(saveName.getText());
MazeParamsFixed p = (MazeParamsFixed) params; MazeParamsFixed p = (MazeParamsFixed) params;
saveStl(p, model,stlsettings.createParams()); saveStl(p, model,stlsettings.createParams());
@@ -377,7 +417,7 @@ public class Display extends JFrame {
stlsettings = new Maze3dSettings(new Maze3dParams()); stlsettings = new Maze3dSettings(new Maze3dParams());
JMenu saveMenu = new JMenu("Save"); JMenu saveMenu = new JMenu(labels.getString("save") );
saveMenu.add(saveName); saveMenu.add(saveName);
saveMenu.add(saveSvgButton); saveMenu.add(saveSvgButton);
saveMenu.add(savePngButton); saveMenu.add(savePngButton);
@@ -405,7 +445,7 @@ public class Display extends JFrame {
resolveButton.addActionListener(new ActionListener() { resolveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) { public void actionPerformed(ActionEvent evt) {
// //
System.out.println("Resolving"); addStatus("Resolving");
resolve(); resolve();
} }
}); });
@@ -416,7 +456,7 @@ public class Display extends JFrame {
Action quitAction = new AbstractAction() { Action quitAction = new AbstractAction() {
public void actionPerformed(ActionEvent evt) { public void actionPerformed(ActionEvent evt) {
// //
System.out.println("quit"); addStatus("quit");
System.exit(0); System.exit(0);
} }
}; };
@@ -444,17 +484,18 @@ public class Display extends JFrame {
JMenuBar menuBar = new JMenuBar(); JMenuBar menuBar = new JMenuBar();
menuBar.add(createSavingMenu()); menuBar.add(createSavingMenu());
menuBar.add(createLoadingMenu());
add(menuBar); add(menuBar);
add(createResolveQuitBar()); add(createResolveQuitBar());
controlPanel = new JPanel(new BorderLayout()); JPanel controlMovesPanel = new JPanel(new BorderLayout());
settings = new MazeSettings(params); settings = new MazeSettings(params);
@SuppressWarnings("serial") @SuppressWarnings("serial")
Action goNorth = new AbstractAction() { Action goNorth = new AbstractAction() {
public void actionPerformed(ActionEvent evt) { public void actionPerformed(ActionEvent evt) {
// //
System.out.println("go North"); addStatus("go North");
goNorth(); goNorth();
} }
}; };
@@ -464,7 +505,7 @@ public class Display extends JFrame {
Action goEast = new AbstractAction() { Action goEast = new AbstractAction() {
public void actionPerformed(ActionEvent evt) { public void actionPerformed(ActionEvent evt) {
// //
System.out.println("go East"); addStatus("go East");
goEast(); goEast();
} }
}; };
@@ -475,7 +516,7 @@ public class Display extends JFrame {
Action goWest = new AbstractAction() { Action goWest = new AbstractAction() {
public void actionPerformed(ActionEvent evt) { public void actionPerformed(ActionEvent evt) {
// //
System.out.println("go West"); addStatus("go West");
goWest(); goWest();
} }
}; };
@@ -485,16 +526,22 @@ public class Display extends JFrame {
Action goSouth = new AbstractAction() { Action goSouth = new AbstractAction() {
public void actionPerformed(ActionEvent evt) { public void actionPerformed(ActionEvent evt) {
// //
System.out.println("go South"); addStatus("go South");
goSouth(); goSouth();
} }
}; };
JButton south = addDirection(this, labels.getString("south"), "DOWN", goSouth); JButton south = addDirection(this, labels.getString("south"), "DOWN", goSouth);
controlPanel.add(north, BorderLayout.NORTH); controlMovesPanel.add(north, BorderLayout.NORTH);
controlPanel.add(west, BorderLayout.WEST); controlMovesPanel.add(west, BorderLayout.WEST);
controlPanel.add(east, BorderLayout.EAST); controlMovesPanel.add(east, BorderLayout.EAST);
controlPanel.add(south, BorderLayout.SOUTH); controlMovesPanel.add(south, BorderLayout.SOUTH);
JPanel controlPanel = new JPanel();
controlPanel.add(controlMovesPanel,BorderLayout.LINE_START);
statusField = new JTextField("",20);
controlPanel.add(statusField, BorderLayout.CENTER);
add(controlPanel, BorderLayout.NORTH); add(controlPanel, BorderLayout.NORTH);
// control display panel contains controls for display // control display panel contains controls for display
@@ -549,6 +596,32 @@ public class Display extends JFrame {
} }
private void loadRaw() {
File infile = new File(params.getSaveDir(), params.getName() + ".raw");
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(infile);
model = new LabyModel("raw", inputStream);
} catch (IOException io) {
io.printStackTrace(System.err);
statusField.setText("[ERROR] Can't load " + infile.getAbsolutePath());
}
finally
{
if (inputStream != null )
{
// cleanup
try {
inputStream.close();
}
catch (Exception any)
{
// don't care really
}
}
}
}
private JButton addDirection(final JPanel panel, final String direction, String key, Action goAction) { private JButton addDirection(final JPanel panel, final String direction, String key, Action goAction) {
String actionName = "go" + direction; String actionName = "go" + direction;
JButton button = new JButton(direction); JButton button = new JButton(direction);
@@ -694,7 +767,7 @@ public class Display extends JFrame {
} }
private static class MazeComponent private class MazeComponent
extends JComponent extends JComponent
implements MazeCreationListener, implements MazeCreationListener,
MazeResolutionListener, MazeResolutionListener,
@@ -734,7 +807,7 @@ public class Display extends JFrame {
if (drawingPath == null) { if (drawingPath == null) {
drawingPath = new LinkedList<>(); drawingPath = new LinkedList<>();
last = new DirectionPosition((short) 0, newPosition); last = new DirectionPosition((short) 0, newPosition);
System.out.println("Mouse dragged Cell " + newPosition + " Button " + e.getModifiersEx() + " " + InputEvent.BUTTON1_DOWN_MASK); addStatus("Mouse dragged Cell " + newPosition + " Button " + e.getModifiersEx() + " " + InputEvent.BUTTON1_DOWN_MASK);
drawingPath.addLast(last); drawingPath.addLast(last);
} else { } else {
// setShowAll(true); // setShowAll(true);
@@ -753,14 +826,14 @@ public class Display extends JFrame {
last = last.moveToAdjacentDirection(); last = last.moveToAdjacentDirection();
drawingPath.addLast(last); drawingPath.addLast(last);
} }
System.out.println("Mouse dragged from Cell " + first.getPosition() + "To" + newPosition + " Button " + e.getModifiersEx() + " " + InputEvent.BUTTON1_DOWN_MASK); addStatus("Mouse dragged from Cell " + first.getPosition() + "To" + newPosition + " Button " + e.getModifiersEx() + " " + InputEvent.BUTTON1_DOWN_MASK);
} }
changed(null, null, map); changed(null, null, map);
} }
@Override @Override
public void mouseMoved(MouseEvent e) { public void mouseMoved(MouseEvent e) {
// System.out.println("Mouse moved (" + e.getX() + ',' + e.getY() + ')'); // addStatus("Mouse moved (" + e.getX() + ',' + e.getY() + ')');
Position newPosition = getPosition(e.getX(), e.getY()); Position newPosition = getPosition(e.getX(), e.getY());
if ((newPosition.getX() >= 0) && (newPosition.getY() >= 0)) { if ((newPosition.getX() >= 0) && (newPosition.getY() >= 0)) {
requestFocus(); requestFocus();
@@ -769,12 +842,12 @@ public class Display extends JFrame {
public void setShowAll(boolean showall) { public void setShowAll(boolean showall) {
this.showAll = showall; this.showAll = showall;
System.out.println(showAll ? "show all" : "X"); addStatus(showAll ? "show all" : "X");
} }
void checkExit() { void checkExit() {
if ((sX == gX) && (sY == gY)) { if ((sX == gX) && (sY == gY)) {
System.out.println("Exit found by human !"); addStatus("Exit found by human !");
} }
} }
@@ -958,7 +1031,7 @@ public class Display extends JFrame {
} }
if (this.showAll) { if (this.showAll) {
System.out.println("*"); addStatus("*");
pX = aX; pX = aX;
pY = aY; pY = aY;
@@ -998,7 +1071,7 @@ public class Display extends JFrame {
public void notifyCompletion(LinkedList<DirectionPosition> solvedPath) { public void notifyCompletion(LinkedList<DirectionPosition> solvedPath) {
LinkedList<DirectionPosition> newPath = new LinkedList<>(solvedPath); LinkedList<DirectionPosition> newPath = new LinkedList<>(solvedPath);
System.out.println("resolution completed"); addStatus("resolution completed");
synchronized (lockChange) { synchronized (lockChange) {
this.solvedPath = newPath; this.solvedPath = newPath;
} }
@@ -1016,39 +1089,39 @@ public class Display extends JFrame {
Display display = new Display(model, W, H, params); Display display = new Display(model, W, H, params);
} }
public static void saveStl(MazeParamsFixed params, LabyModel model, Maze3dParams wallparams) { public void saveStl(MazeParamsFixed params, LabyModel model, Maze3dParams wallparams) {
File outfile = new File(params.getSaveDir(), params.getName() + ".stl"); File outfile = getFileForExtension("stl");
if (!outfile.exists()) { if (!outfile.exists()) {
System.out.println("Saving to " + outfile + " ..."); addStatus("Saving to " + outfile + " ...");
try { try {
FileOutputStream out = new FileOutputStream(outfile); FileOutputStream out = new FileOutputStream(outfile);
new Wall3dStream(params.getName(), model, out, wallparams).stream(); new Wall3dStream(params.getName(), model, out, wallparams).stream();
out.close(); out.close();
System.out.println("... Done."); addStatus("... Done.");
} catch (IOException io) { } catch (IOException io) {
io.printStackTrace(System.err); io.printStackTrace(System.err);
} }
} else { } else {
System.out.println("" + outfile + " already exists"); addStatus("" + outfile + " already exists");
} }
} }
public static void save(MazeParamsFixed params, LabyModel model) { public void save(MazeParamsFixed params, LabyModel model) {
File outfile = new File(params.getSaveDir(), params.getName() + ".raw"); File outfile = getFileForExtension("raw");
if (!outfile.exists()) { if (!outfile.exists()) {
System.out.println("Saving to " + outfile + " ..."); addStatus("Saving to " + outfile + " ...");
try { try {
FileOutputStream out = new FileOutputStream(outfile); FileOutputStream out = new FileOutputStream(outfile);
model.streamOut("raw", out); model.streamOut("raw", out);
out.flush(); out.flush();
out.close(); out.close();
System.out.println("... Done."); addStatus("... Done.");
} catch (IOException io) { } catch (IOException io) {
io.printStackTrace(System.err); io.printStackTrace(System.err);
} }
} else { } else {
System.out.println("" + outfile + " already exists"); addStatus("" + outfile + " already exists");
} }
} }
@@ -1081,7 +1154,7 @@ public class Display extends JFrame {
model.addEntryOrExit(-1, 0); model.addEntryOrExit(-1, 0);
model.addEntryOrExit(params.getWidth(), params.getHeight() - 1); model.addEntryOrExit(params.getWidth(), params.getHeight() - 1);
System.out.println("Generation completed"); addStatus("Generation completed");
*/ */
/* /*
*/ */

View File

@@ -11,3 +11,4 @@ save = Save
quit = Quit quit = Quit
title = A Maz ing title = A Maz ing
seed = seed seed = seed
load = Load

View File

@@ -11,3 +11,4 @@ save = Sauver
quit = Quitter quit = Quitter
title = La Bireinte title = La Bireinte
seed = graine seed = graine
load = Charger