From 8ec6e2c2cd48721c927f0884bd02751b37eb9e9e Mon Sep 17 00:00:00 2001 From: philippe lhardy Date: Fri, 8 Dec 2017 22:10:56 +0100 Subject: [PATCH] QnD support .imc saving use external artgaphics-0.1.0.jar (part of sharedrawweb project ) savec .imc files --- java/Makefile | 14 +-- java/org/artisanlogiciel/games/Display.java | 101 +++++++++++++++++++- 2 files changed, 108 insertions(+), 7 deletions(-) diff --git a/java/Makefile b/java/Makefile index 29ddb5b..4bc93ec 100644 --- a/java/Makefile +++ b/java/Makefile @@ -1,6 +1,8 @@ PACKAGE=org.artisanlogiciel.games PACKAGE_DIR=$(subst .,/,$(PACKAGE)) OUT=out +# external libraries +LIBS=../libs/artgaphics-0.1.0.jar EDITOR=emacs $(OUT): @@ -11,21 +13,21 @@ clean: @find $(OUT) -name "*.class" -type f -print0|xargs -0 rm 2>/dev/null || echo "nothing to clean" test: - javac -d $(OUT) $(PACKAGE_DIR)/LabyModel.java - javac -d $(OUT) $(PACKAGE_DIR)/Main.java + javac -cp $(LIBS) -d $(OUT) $(PACKAGE_DIR)/LabyModel.java + javac -cp $(LIBS) -d $(OUT) $(PACKAGE_DIR)/Main.java java -cp $(OUT) $(PACKAGE).Main run/%: $(OUT) - javac -d $(OUT) $(PACKAGE_DIR)/$(subst run/,,$@).java - java -cp $(OUT) $(PACKAGE)/$(subst run/,,$@) + javac -cp $(LIBS):$(OUT) -d $(OUT) $(PACKAGE_DIR)/$(subst run/,,$@).java + java -cp $(OUT):$(LIBS) $(PACKAGE)/$(subst run/,,$@) display: run/Display display/%: $(OUT) - javac -d $(OUT) $(PACKAGE_DIR)/Display.java - java -cp $(OUT) $(PACKAGE).Display $(subst display/,,$@) + javac -cp $(LIBS) -d $(OUT) $(PACKAGE_DIR)/Display.java + java -cp $(OUT):$(LIBS) $(PACKAGE).Display $(subst display/,,$@) compile/%: javac -d $(OUT) $(PACKAGE_DIR)/$(subst compile/,,$@).java diff --git a/java/org/artisanlogiciel/games/Display.java b/java/org/artisanlogiciel/games/Display.java index 1beac4c..30158ce 100644 --- a/java/org/artisanlogiciel/games/Display.java +++ b/java/org/artisanlogiciel/games/Display.java @@ -7,6 +7,7 @@ import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Graphics; +import java.awt.Point; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -15,6 +16,7 @@ import java.awt.event.ComponentEvent; import java.awt.event.InputEvent; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; +import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -44,6 +46,9 @@ import javax.swing.event.ChangeListener; import org.artisanlogiciel.games.stl.Wall3d; import org.artisanlogiciel.util.UTF8Control; +import org.artisanlogiciel.graphics.Drawing; +import org.artisanlogiciel.graphics.DrawingLine; + /** * Display is Main JFrame for this tool **/ @@ -62,6 +67,8 @@ public class Display extends JFrame MazeParams params = null; + Drawing drawing = new Drawing(); + Display(LabyModel model,int W, int H, MazeParams params) { super(labels.getString("title")); @@ -190,6 +197,82 @@ public class Display extends JFrame } } + void addWallInDrawing(int pX, int pY, Drawing d) + { + short walls = model.getWalls(pX,pY); + // todo + int w = 2; + int h = 2; + int ox=0; + int oy=0; + + int x = ox + (int) (pX * w); + int y = oy + (int) (pY * h); + DrawingLine dl = new DrawingLine(); + if ( (pY == 0) && ((walls & Brick.UP) == Brick.UP)) + { + dl.addPoint(new Point(x, y)); + dl.addPoint(new Point(x + (int) w, y)); + } + if ((walls & Brick.DOWN) == Brick.DOWN) + { + dl.addPoint(new Point(x, y + (int) h)); + dl.addPoint(new Point(x + (int) w, y + (int) h)); + } + if ((walls & Brick.RIGHT) == Brick.RIGHT) + { + dl.addPoint(new Point(x + (int) w, y)); + dl.addPoint(new Point(x + (int) w, y + (int) h)); + } + if ( (pX == 0 ) && ((walls & Brick.LEFT) == Brick.LEFT)) + { + dl.addPoint(new Point(x, y)); + dl.addPoint(new Point(x, y + (int) h)); + } + d.addLine(dl); + } + + Drawing createDrawing() + { + Drawing d = new Drawing(); + { + short walls; + + // draw all walls within clip bounds horiz first then lines + for (int y = 0; y < model.getHeight(); y++) + { + for (int x = 0; x < model.getWidth(); x ++) + { + addWallInDrawing(x,y,d); + } + } + } + return d; + } + + void saveImc() + { + Drawing d = createDrawing(); + + if ( d != null ) + { + File outfile = new File(params.getSaveDir(), params.getName() + ".imc"); + System.out.println("Saving to " + outfile + " ..."); + try + { + DataOutputStream out = new DataOutputStream(new FileOutputStream(outfile)); + d.saveLinesKompressed(out); + out.flush(); + out.close(); + System.out.println("... Done."); + } + catch (IOException io) + { + io.printStackTrace(System.err); + } + } + } + void savePng() { File file = new File("snapshot.png"); @@ -475,6 +558,21 @@ public class Display extends JFrame } }; 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"); + /* + MazeParamsFixed p = (MazeParamsFixed) params; + p.setName(saveName.getText()); + save(p,model); + */ + saveImc(); + } + }; + saveImcButton.addActionListener(saveImcAction); final JButton quitButton = new JButton(labels.getString("quit")); Action quitAction = new AbstractAction() { @@ -494,6 +592,7 @@ public class Display extends JFrame resizecontrol.add(saveName); resizecontrol.add(savePngButton); resizecontrol.add(saveButton); + resizecontrol.add(saveImcButton); resizecontrol.add(quitButton); add(controlDisplayPanel, BorderLayout.SOUTH); @@ -717,7 +816,7 @@ public class Display extends JFrame // not set by default for debug, will show any path boolean showAll = false; - + // for a given (x,y) pixel return cell position. Position getPosition(int x, int y) {