From b96093880566d3dae41a5af1075b99fea202f353 Mon Sep 17 00:00:00 2001 From: galaxyoyo Date: Wed, 7 Jan 2015 21:17:17 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=C3=A9=20fonctions=20pour=20ouverture/sauv?= =?UTF-8?q?egarde=20d'une=20carte?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .classpath | 1 + .../unknown/api/editor/EditorAPI.java | 120 ++++++++++++++++++ .../unknown/api/editor/package-info.java | 4 + .../galaxyoyo/unknown/client/main/Main.java | 14 +- .../java/galaxyoyo/unknown/editor/Editor.java | 39 +++--- .../galaxyoyo/unknown/editor/EditorFrame.java | 12 ++ .../galaxyoyo/unknown/frame/MainFrame.java | 6 +- .../frame/listeners/CreateMapListener.java | 8 -- .../frame/listeners/OpenMapListener.java | 18 +++ 9 files changed, 190 insertions(+), 32 deletions(-) create mode 100644 src/api/java/galaxyoyo/unknown/api/editor/EditorAPI.java create mode 100644 src/api/java/galaxyoyo/unknown/api/editor/package-info.java create mode 100644 src/main/java/galaxyoyo/unknown/editor/EditorFrame.java create mode 100644 src/main/java/galaxyoyo/unknown/frame/listeners/OpenMapListener.java diff --git a/.classpath b/.classpath index b2ff1d1..90a9836 100644 --- a/.classpath +++ b/.classpath @@ -13,6 +13,7 @@ + diff --git a/src/api/java/galaxyoyo/unknown/api/editor/EditorAPI.java b/src/api/java/galaxyoyo/unknown/api/editor/EditorAPI.java new file mode 100644 index 0000000..a9093ba --- /dev/null +++ b/src/api/java/galaxyoyo/unknown/api/editor/EditorAPI.java @@ -0,0 +1,120 @@ +package galaxyoyo.unknown.api.editor; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import javax.swing.JFileChooser; +import javax.swing.filechooser.FileNameExtensionFilter; + +public class EditorAPI +{ + public static Byte[] toBytes(int width, int height) + { + List bytes = new ArrayList(); + + for (int y = 1; y < height; y += 16) + { + for (int x = 1; x < width; x += 16) + { + bytes.add((byte) 0); + } + bytes.add(Byte.MIN_VALUE); + } + + bytes.remove(bytes.lastIndexOf(Byte.MIN_VALUE)); + + return bytes.toArray(new Byte[0]); + } + + public static JFileChooser createJFC() + { + JFileChooser jfc = new JFileChooser(); + + jfc.setFileFilter(new FileNameExtensionFilter("Fichiers monde (*.gworld, *.dat)", "gworld", "dat")); + jfc.setFileHidingEnabled(true); + jfc.setFileSelectionMode(JFileChooser.FILES_ONLY); + File dir = new File("maps"); + dir.mkdirs(); + jfc.setCurrentDirectory(dir); + + return jfc; + } + + public static void saveAs(byte ... bytes) + { + JFileChooser jfc = createJFC(); + File file = null; + while (file == null) + { + jfc.showSaveDialog(null); + file = jfc.getSelectedFile(); + } + + if (!file.getName().toLowerCase().endsWith(".gworld") && !file.getName().toLowerCase().endsWith(".dat")) + { + file = new File(file.getParentFile(), file.getName() + ".gworld"); + } + + save(file, bytes); + } + + public static void save(File file, byte ... bytes) + { + try + { + BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); + bos.write(bytes); + bos.close(); + } + catch (IOException ex) + { + ex.printStackTrace(); + } + } + + public static void open() + { + JFileChooser jfc = createJFC(); + File file = null; + + while (file == null) + { + jfc.showOpenDialog(null); + file = jfc.getSelectedFile(); + System.out.println(file); + } + + open(file); + } + + public static void open(File f) + { + System.out.println(f); + try + { + BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f)); + byte[] bytes = new byte[(int) f.length()]; + System.out.println(bis); + while (bis.read(bytes) != -1); + for (byte b : bytes) + { + System.err.println(b); + } + bis.close(); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + + public static void open(byte[] bytes) + { + } +} diff --git a/src/api/java/galaxyoyo/unknown/api/editor/package-info.java b/src/api/java/galaxyoyo/unknown/api/editor/package-info.java new file mode 100644 index 0000000..4e404f7 --- /dev/null +++ b/src/api/java/galaxyoyo/unknown/api/editor/package-info.java @@ -0,0 +1,4 @@ +/** + * @author galaxyoyo + */ +package galaxyoyo.unknown.api.editor; \ No newline at end of file diff --git a/src/main/java/galaxyoyo/unknown/client/main/Main.java b/src/main/java/galaxyoyo/unknown/client/main/Main.java index 9f19699..02274ab 100644 --- a/src/main/java/galaxyoyo/unknown/client/main/Main.java +++ b/src/main/java/galaxyoyo/unknown/client/main/Main.java @@ -3,7 +3,7 @@ */ package galaxyoyo.unknown.client.main; -import galaxyoyo.unknown.editor.Editor; +import galaxyoyo.unknown.api.editor.EditorAPI; import galaxyoyo.unknown.frame.MainFrame; import java.awt.Color; @@ -185,13 +185,19 @@ public class Main g.drawLine(0, y, width, y); } - Byte[] bytes = Editor.toBytes(baseWidth, baseHeight); + Byte[] Bytes = EditorAPI.toBytes(baseWidth, baseHeight); - for (byte b : bytes) + byte[] bytes = new byte[Bytes.length]; + + for (int i = 0; i < Bytes.length; ++i) { - System.err.print(b); + bytes[i] = Bytes[i]; } + EditorAPI.saveAs(bytes); + + EditorAPI.open(bytes); + try { ImageIO.write(image, "png", new File("img.png")); diff --git a/src/main/java/galaxyoyo/unknown/editor/Editor.java b/src/main/java/galaxyoyo/unknown/editor/Editor.java index 6c8ed10..e2c8b44 100644 --- a/src/main/java/galaxyoyo/unknown/editor/Editor.java +++ b/src/main/java/galaxyoyo/unknown/editor/Editor.java @@ -1,25 +1,28 @@ package galaxyoyo.unknown.editor; -import java.util.ArrayList; -import java.util.List; - public class Editor { - public static Byte[] toBytes(int width, int height) + private final EditorFrame frame; + private byte[] bytes; + + public Editor(byte[] bytes) { - List bytes = new ArrayList(); - - for (int x = 1; x < width; x += 16) - { - for (int y = 1; y < height; y += 16) - { - bytes.add((byte) 0); - } - bytes.add(Byte.MIN_VALUE); - } - - bytes.remove(bytes.lastIndexOf(Byte.MIN_VALUE)); - - return bytes.toArray(new Byte[0]); + frame = new EditorFrame(); + this.bytes = bytes; + } + + public EditorFrame getFrame() + { + return frame; + } + + public byte[] getBytes() + { + return bytes; + } + + public void setBytes(byte[] bytes) + { + this.bytes = bytes; } } diff --git a/src/main/java/galaxyoyo/unknown/editor/EditorFrame.java b/src/main/java/galaxyoyo/unknown/editor/EditorFrame.java new file mode 100644 index 0000000..eb6f848 --- /dev/null +++ b/src/main/java/galaxyoyo/unknown/editor/EditorFrame.java @@ -0,0 +1,12 @@ +package galaxyoyo.unknown.editor; + +import javax.swing.JFrame; + +public class EditorFrame extends JFrame +{ + private static final long serialVersionUID = -2705122356101556462L; + + public EditorFrame() + { + } +} diff --git a/src/main/java/galaxyoyo/unknown/frame/MainFrame.java b/src/main/java/galaxyoyo/unknown/frame/MainFrame.java index 2c60fee..d9ff417 100644 --- a/src/main/java/galaxyoyo/unknown/frame/MainFrame.java +++ b/src/main/java/galaxyoyo/unknown/frame/MainFrame.java @@ -4,6 +4,7 @@ package galaxyoyo.unknown.frame; import galaxyoyo.unknown.frame.listeners.CreateMapListener; +import galaxyoyo.unknown.frame.listeners.OpenMapListener; import java.awt.Dimension; @@ -45,7 +46,7 @@ public class MainFrame extends JFrame private JMenu editer = new JMenu("\u00c9diter"); private JMenu editMaps = new JMenu("Cartes"); private JMenuItem createMap = new JMenuItem("Cr\u00e9er"); - private JMenuItem editMap = new JMenuItem("\u00c9diter"); + private JMenuItem openMap = new JMenuItem("Ouvrir"); /** * Constructeur @@ -66,7 +67,8 @@ public class MainFrame extends JFrame createMap.addActionListener(new CreateMapListener()); editMaps.add(createMap); - editMaps.add(editMap); + openMap.addActionListener(new OpenMapListener()); + editMaps.add(openMap); editer.add(editMaps); menuBar.add(editer); diff --git a/src/main/java/galaxyoyo/unknown/frame/listeners/CreateMapListener.java b/src/main/java/galaxyoyo/unknown/frame/listeners/CreateMapListener.java index 6e394c8..bf10408 100644 --- a/src/main/java/galaxyoyo/unknown/frame/listeners/CreateMapListener.java +++ b/src/main/java/galaxyoyo/unknown/frame/listeners/CreateMapListener.java @@ -13,13 +13,6 @@ import java.awt.event.ActionListener; */ public class CreateMapListener implements ActionListener { - /** - * - */ - public CreateMapListener() - { - } - /* !CodeTemplates.overridecomment.nonjd! * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */ @@ -28,5 +21,4 @@ public class CreateMapListener implements ActionListener { Main.main("--edit", Main.isInDebugMode() ? " --debug true" : ""); } - } diff --git a/src/main/java/galaxyoyo/unknown/frame/listeners/OpenMapListener.java b/src/main/java/galaxyoyo/unknown/frame/listeners/OpenMapListener.java new file mode 100644 index 0000000..2d61bc4 --- /dev/null +++ b/src/main/java/galaxyoyo/unknown/frame/listeners/OpenMapListener.java @@ -0,0 +1,18 @@ +package galaxyoyo.unknown.frame.listeners; + +import galaxyoyo.unknown.api.editor.EditorAPI; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class OpenMapListener implements ActionListener +{ + /* !CodeTemplates.overridecomment.nonjd! + * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) + */ + @Override + public void actionPerformed(ActionEvent event) + { + EditorAPI.open(); + } +}