From c26db0e3cb6555e12dea462f25630e8b42c302d5 Mon Sep 17 00:00:00 2001 From: philippe lhardy Date: Sun, 11 Oct 2020 11:40:49 +0200 Subject: [PATCH] GUI change Save Menu and Create Button - Save is now a menu and Create/Resolve/Quit buttons --- java/org/artisanlogiciel/games/Display.java | 316 ++++++++++---------- 1 file changed, 153 insertions(+), 163 deletions(-) diff --git a/java/org/artisanlogiciel/games/Display.java b/java/org/artisanlogiciel/games/Display.java index c87b40b..3e19c58 100644 --- a/java/org/artisanlogiciel/games/Display.java +++ b/java/org/artisanlogiciel/games/Display.java @@ -23,24 +23,11 @@ import java.io.FileOutputStream; import java.io.IOException; import java.util.LinkedList; import java.util.Locale; -import java.util.Scanner; import java.util.ResourceBundle; import javax.imageio.ImageIO; -import javax.swing.AbstractAction; -import javax.swing.Action; -import javax.swing.BoxLayout; -import javax.swing.JButton; -import javax.swing.JComponent; -import javax.swing.JCheckBox; -import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.JPanel; -import javax.swing.JScrollPane; -import javax.swing.JSlider; -import javax.swing.JTextField; -import javax.swing.KeyStroke; +import javax.swing.*; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; @@ -71,40 +58,40 @@ public class Display extends JFrame Drawing drawing = new Drawing(); - Display(LabyModel model,int W, int H, MazeParams params) - { - super(labels.getString("title")); - if (params != null) - { - // fixedParams = new MazeParamsFixed(params.getSaveDir(),params.getWidth(),params.getHeight(),params.getMaxDepth()); - this.params = params; - } - this.model = model; - maze = createMazeComponent(model,W,H); - - Container con = this.getContentPane(); - JScrollPane scrollableMaze = new JScrollPane(maze); - scrollableMaze.addMouseMotionListener(maze); - con.add(scrollableMaze, BorderLayout.CENTER); - controler = new MazeControler(params); - con.add(controler.getMoveControl(), BorderLayout.NORTH); - con.add(controler.getGenerationControl(), BorderLayout.SOUTH); - - addComponentListener(new ComponentAdapter() { - @Override - public void componentResized(ComponentEvent e) { - if ( autoSize ) - { - maze.getAutoSize(); - } + Display(LabyModel model,int W, int H, MazeParams params) + { + super(labels.getString("title")); + if (params != null) + { + // fixedParams = new MazeParamsFixed(params.getSaveDir(),params.getWidth(),params.getHeight(),params.getMaxDepth()); + this.params = params; } - }); + this.model = model; + maze = createMazeComponent(model,W,H); - model.setMazeListener(maze); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - setBounds(W, H, W, H); - setVisible(true); - } + Container con = this.getContentPane(); + JScrollPane scrollableMaze = new JScrollPane(maze); + scrollableMaze.addMouseMotionListener(maze); + con.add(scrollableMaze, BorderLayout.CENTER); + controler = new MazeControler(params); + con.add(controler.getMoveControl(), BorderLayout.NORTH); + con.add(controler.getGenerationControl(), BorderLayout.SOUTH); + + addComponentListener(new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + if ( autoSize ) + { + maze.getAutoSize(); + } + } + }); + + model.setMazeListener(maze); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setBounds(W, H, W, H); + setVisible(true); + } private static MazeComponent createMazeComponent(LabyModel model, int W, int H) { @@ -119,8 +106,8 @@ public class Display extends JFrame if (params != null) { params = controler.getSettings().getParams(); - // keep current model... - // model = new LabyModel(params, new java.util.Random()); + // DON'T keep current model... + model = new LabyModel(params, new java.util.Random()); maze.resetWallsProvider(model); model.setMazeListener(maze); @@ -361,71 +348,61 @@ public class Display extends JFrame } } - private class MazeSettings extends JPanel - { - MazeParams params; - - JTextField textWidth = null; - JTextField textHeight = null; - JTextField textDepth = null; - - // TODO set width and height and depth of maze with gui - public MazeSettings(MazeParams params) + private class MazeSettings extends JPanel { - super(); - this.params = params; - createSettingsGui(); - } + MazeParams params; - void createSettingsGui() - { - if ( params != null ) + JTextField textWidth = null; + JTextField textHeight = null; + JTextField textDepth = null; + + // TODO set width and height and depth of maze with gui + public MazeSettings(MazeParams params) { - JButton buttonCreate = new JButton(labels.getString("create")); - buttonCreate.addActionListener(new ActionListener() - { - public void actionPerformed(ActionEvent evt) - { - recreateModel(); - } - }); - add(buttonCreate); - - JButton buttonReset = new JButton("reset");//labels.getString("reset")); - buttonReset.addActionListener(new ActionListener() - { - public void actionPerformed(ActionEvent evt) - { - resetModel(); - } - }); - add(buttonReset); - + super(); + this.params = params; + createSettingsGui(); } - if ( params != null ) + + void createSettingsGui() { - JLabel widthLabel = new JLabel(labels.getString("width")); - textWidth = new JTextField("0" + params.getWidth()); - add(widthLabel); - add(textWidth); - JLabel heightLabel = new JLabel(labels.getString("height")); - textHeight = new JTextField("0" + params.getHeight()); - add(heightLabel); - add(textHeight); - JLabel depthLabel = new JLabel(labels.getString("depth")); - textDepth = new JTextField("0" + params.getMaxDepth()); - add(depthLabel); - add(textDepth); - } - } + if ( params != null ) + { + JButton buttonReset = new JButton("reset");//labels.getString("reset")); + buttonReset.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent evt) + { + resetModel(); + } + }); + add(buttonReset); - public MazeParams getParams() - { - return new MazeParamsFixed(params.getSaveDir(), - Integer.parseInt(textWidth.getText()), - Integer.parseInt(textHeight.getText()), - Integer.parseInt(textDepth.getText())); - } + } + if ( params != null ) + { + JLabel widthLabel = new JLabel(labels.getString("width")); + textWidth = new JTextField("0" + params.getWidth()); + add(widthLabel); + add(textWidth); + JLabel heightLabel = new JLabel(labels.getString("height")); + textHeight = new JTextField("0" + params.getHeight()); + add(heightLabel); + add(textHeight); + JLabel depthLabel = new JLabel(labels.getString("depth")); + textDepth = new JTextField("0" + params.getMaxDepth()); + add(depthLabel); + add(textDepth); + } + } + + public MazeParams getParams() + { + return new MazeParamsFixed(params.getSaveDir(), + Integer.parseInt(textWidth.getText()), + Integer.parseInt(textHeight.getText()), + Integer.parseInt(textDepth.getText())); + } } @@ -448,69 +425,80 @@ public class Display extends JFrame p.setName(pName); } - private JPanel createSavingBar() + private JMenu createSavingMenu() { - final JTextField saveName = new JTextField("newlaby "); - final JButton savePngButton = new JButton(labels.getString("save") +" png"); - Action savePngAction = new AbstractAction() { - public void actionPerformed(ActionEvent evt) - { - // - System.out.println("save png"); - setMazeName(saveName.getText()); - savePng(); - } - }; - savePngButton.addActionListener(savePngAction); - final JButton saveSvgButton = new JButton(labels.getString("save") +" svg"); - Action saveSvgAction = new AbstractAction() { - public void actionPerformed(ActionEvent evt) - { - writeSentence("save png"); - setMazeName(saveName.getText()); - saveSvg(); - } - }; - saveSvgButton.addActionListener(saveSvgAction); - final JButton saveButton = new JButton(labels.getString("save") +" raw"); - Action saveAction = new AbstractAction() { - public void actionPerformed(ActionEvent evt) - { - // - System.out.println("save"); - setMazeName(saveName.getText()); - MazeParamsFixed p = (MazeParamsFixed) params; - save(p,model); - } - }; - saveButton.addActionListener(saveAction); - final JButton saveImcButton = new JButton(labels.getString("save") +" imc"); - Action saveImcAction = new AbstractAction() { - public void actionPerformed(ActionEvent evt) - { - // - System.out.println("save imc"); - setMazeName(saveName.getText()); - saveImc(); - } - }; - saveImcButton.addActionListener(saveImcAction); + final JTextField saveName = new JTextField("newlaby "); + final JButton savePngButton = new JButton(labels.getString("save") +" png"); + Action savePngAction = new AbstractAction() { + public void actionPerformed(ActionEvent evt) + { + // + System.out.println("save png"); + setMazeName(saveName.getText()); + savePng(); + } + }; + savePngButton.addActionListener(savePngAction); + final JButton saveSvgButton = new JButton(labels.getString("save") +" svg"); + Action saveSvgAction = new AbstractAction() { + public void actionPerformed(ActionEvent evt) + { + writeSentence("save png"); + setMazeName(saveName.getText()); + saveSvg(); + } + }; + saveSvgButton.addActionListener(saveSvgAction); + final JButton saveButton = new JButton(labels.getString("save") +" raw"); + Action saveAction = new AbstractAction() { + public void actionPerformed(ActionEvent evt) + { + // + System.out.println("save"); + setMazeName(saveName.getText()); + MazeParamsFixed p = (MazeParamsFixed) params; + save(p,model); + } + }; + saveButton.addActionListener(saveAction); + final JButton saveImcButton = new JButton(labels.getString("save") +" imc"); + Action saveImcAction = new AbstractAction() { + public void actionPerformed(ActionEvent evt) + { + // + System.out.println("save imc"); + setMazeName(saveName.getText()); + saveImc(); + } + }; + saveImcButton.addActionListener(saveImcAction); - JPanel savePanel = new JPanel(new FlowLayout()); - savePanel.add(saveName); - savePanel.add(saveSvgButton); - savePanel.add(savePngButton); - savePanel.add(saveButton); - savePanel.add(saveImcButton); + JMenu saveMenu = new JMenu("Save"); + saveMenu.add(saveName); + saveMenu.add(saveSvgButton); + saveMenu.add(savePngButton); + saveMenu.add(saveButton); + saveMenu.add(saveImcButton); + + return saveMenu; - return savePanel; - } private JPanel createResolveQuitBar() { JPanel resolveQuitBar = new JPanel(new FlowLayout()); - JButton resolveButton = new JButton(labels.getString("resolve")); + + JButton buttonCreate = new JButton(labels.getString("create")); + buttonCreate.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent evt) + { + recreateModel(); + } + }); + resolveQuitBar.add(buttonCreate); + + JButton resolveButton = new JButton(labels.getString("resolve")); resolveButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) @@ -547,7 +535,9 @@ public class Display extends JFrame BoxLayout layout = new BoxLayout(this,BoxLayout.Y_AXIS); setLayout(layout); - add(createSavingBar()); + JMenuBar menuBar = new JMenuBar(); + menuBar.add(createSavingMenu()); + add(menuBar); add(createResolveQuitBar());