diff --git a/download/mapping-1.0.1.jar b/download/mapping-1.0.1.jar deleted file mode 100644 index e3e79b1..0000000 Binary files a/download/mapping-1.0.1.jar and /dev/null differ diff --git a/download/mapping-1.1.jar b/download/mapping-1.1.jar new file mode 100644 index 0000000..efca413 Binary files /dev/null and b/download/mapping-1.1.jar differ diff --git a/make_jar.bat b/make_jar.bat new file mode 100644 index 0000000..8802e72 --- /dev/null +++ b/make_jar.bat @@ -0,0 +1 @@ +mvn clean package && del download\*.jar && xcopy /Y target\*-jar-with-dependencies.jar download && powershell -command "get-childitem download\* | foreach { rename-item $_ $_.Name.Replace('-jar-with-dependencies', '') }" && git add download/*.jar \ No newline at end of file diff --git a/pom.xml b/pom.xml index 6607282..c85ca64 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ fr.klemek mapping - 1.0.1 + 1.1 diff --git a/src/main/java/fr/klemek/mapping/KeyEventListener.java b/src/main/java/fr/klemek/mapping/KeyEventListener.java index d0ede56..828b5c8 100644 --- a/src/main/java/fr/klemek/mapping/KeyEventListener.java +++ b/src/main/java/fr/klemek/mapping/KeyEventListener.java @@ -76,7 +76,7 @@ public class KeyEventListener implements KeyListener { "Save", JOptionPane.YES_NO_OPTION); if (n2 == JOptionPane.YES_OPTION) { - if (FileUtils.save(MainWindow.FILE_NAME, this.mp.getMap().toString())) { + if (Utils.saveFile(MainWindow.FILE_NAME, this.mp.getMap().toString())) { JOptionPane.showInternalMessageDialog(this.mp, "Saved as '" + MainWindow.FILE_NAME + "'", "Saved", @@ -90,6 +90,9 @@ public class KeyEventListener implements KeyListener { } } break; + case KeyEvent.VK_SHIFT: + this.mp.setShiftDown(true); + break; default: break; } @@ -97,6 +100,12 @@ public class KeyEventListener implements KeyListener { @Override public void keyReleased(KeyEvent e) { - //ignored + switch (e.getKeyCode()) { + case KeyEvent.VK_SHIFT: + this.mp.setShiftDown(false); + break; + default: + break; + } } } diff --git a/src/main/java/fr/klemek/mapping/MainPanel.java b/src/main/java/fr/klemek/mapping/MainPanel.java index 006ee5f..5d18200 100644 --- a/src/main/java/fr/klemek/mapping/MainPanel.java +++ b/src/main/java/fr/klemek/mapping/MainPanel.java @@ -8,20 +8,23 @@ import javax.swing.*; class MainPanel extends JPanel { - final static float DEFAULT_RATIO = .5f; - private final static float SIZE = 1.2f; + static final float DEFAULT_RATIO = .5f; + private static final float SIZE = 1.2f; private float ratio = DEFAULT_RATIO; - private Map map; - private int w, h; - private int cx, cy; - private float mw, mh; - private int sx = -1, sy = -1; + private transient Map map; + private int cx; + private int cy; + private float mw; + private float mh; + private int sx = -1; + private int sy = -1; private int[][] xs; private int[][] ys; private boolean showGrid; + private boolean shiftDown; private float angle; MainPanel(Map map) { @@ -74,19 +77,19 @@ class MainPanel extends JPanel { RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)); - this.w = this.getWidth(); - this.h = this.getHeight(); - this.cx = Math.round(this.w / 2f); - this.cy = Math.round(this.h / 2f); - if (this.w > this.h) { - this.mh = this.h * SIZE * ratio; + int w = this.getWidth(); + int h = this.getHeight(); + this.cx = Math.round(w / 2f); + this.cy = Math.round(h / 2f); + if (w > h) { + this.mh = h * SIZE * ratio; this.mw = mh / ratio; } else { - this.mw = this.w * SIZE; + this.mw = w * SIZE; this.mh = mw * ratio; } - g2.clearRect(0, 0, this.w, this.h); + g2.clearRect(0, 0, w, h); drawMap(g2); } @@ -105,19 +108,19 @@ class MainPanel extends JPanel { for (int x = 0; x < this.map.size(); x++) { for (int y = 0; y < this.map.size(); y++) { - Point p = getPoint(x0, xstep, ystep, x, y, this.map.get(x, y)); + Point p = this.getPoint(x0, xstep, ystep, x, y, this.map.get(x, y)); this.xs[x][y] = p.x; this.ys[x][y] = p.y; if (this.showGrid) { - Point p0 = getPoint(x0, xstep, ystep, x, y, 0); + Point p0 = this.getPoint(x0, xstep, ystep, x, y, 0); if (x > 0) { - Point p1 = getPoint(x0, xstep, ystep, x - 1, y, 0); + Point p1 = this.getPoint(x0, xstep, ystep, x - 1, y, 0); g2.drawLine(p0.x, p0.y, p1.x, p1.y); } if (y > 0) { - Point p1 = getPoint(x0, xstep, ystep, x, y - 1, 0); + Point p1 = this.getPoint(x0, xstep, ystep, x, y - 1, 0); g2.drawLine(p0.x, p0.y, p1.x, p1.y); } g2.drawLine(p0.x, p0.y, p.x, p.y); @@ -125,30 +128,12 @@ class MainPanel extends JPanel { } } - - g2.setColor(Color.BLACK); - for (int x = 0; x < this.map.size(); x++) { for (int y = 0; y < this.map.size(); y++) { - if (x > 0) - g2.drawLine(this.xs[x - 1][y], this.ys[x - 1][y], this.xs[x][y], this.ys[x][y]); - if (y > 0) - g2.drawLine(this.xs[x][y - 1], this.ys[x][y - 1], this.xs[x][y], this.ys[x][y]); + this.drawLine(g2, x - 1, y, x, y); + this.drawLine(g2, x, y - 1, x, y); } } - - g2.setColor(Color.RED); - - if (sx >= 0 && sy >= 0) { - if (sx > 0) - g2.drawLine(this.xs[sx - 1][sy], this.ys[sx - 1][sy], this.xs[sx][sy], this.ys[sx][sy]); - if (sy > 0) - g2.drawLine(this.xs[sx][sy - 1], this.ys[sx][sy - 1], this.xs[sx][sy], this.ys[sx][sy]); - if (sx < this.map.size() - 1) - g2.drawLine(this.xs[sx + 1][sy], this.ys[sx + 1][sy], this.xs[sx][sy], this.ys[sx][sy]); - if (sy < this.map.size() - 1) - g2.drawLine(this.xs[sx][sy + 1], this.ys[sx][sy + 1], this.xs[sx][sy], this.ys[sx][sy]); - } } private Point getPoint(int x0, float xstep, float ystep, int x, int y, float value) { @@ -163,6 +148,20 @@ class MainPanel extends JPanel { ); } + private void drawLine(Graphics2D g2, int x1, int y1, int x2, int y2) { + if (x1 >= 0 && x1 < this.map.size() && + y1 >= 0 && y1 < this.map.size() && + x2 >= 0 && x2 < this.map.size() && + y2 >= 0 && y2 < this.map.size()) { + if (Utils.dist2(x1, y1, sx, sy) < (this.shiftDown ? 2 : 1) || Utils.dist2(x2, y2, sx, sy) < (this.shiftDown ? 2 : 1)) { + g2.setColor(Color.RED); + } else { + g2.setColor(Color.BLACK); + } + g2.drawLine(this.xs[x1][y1], this.ys[x1][y1], this.xs[x2][y2], this.ys[x2][y2]); + } + } + float getRatio() { return ratio; } @@ -190,4 +189,8 @@ class MainPanel extends JPanel { Map getMap() { return map; } + + void setShiftDown(boolean shiftDown) { + this.shiftDown = shiftDown; + } } diff --git a/src/main/java/fr/klemek/mapping/MainWindow.java b/src/main/java/fr/klemek/mapping/MainWindow.java index 162a41f..b661767 100644 --- a/src/main/java/fr/klemek/mapping/MainWindow.java +++ b/src/main/java/fr/klemek/mapping/MainWindow.java @@ -9,7 +9,7 @@ import javax.swing.*; class MainWindow extends JFrame { static final String FILE_NAME = "mapping.csv"; - private static final String VERSION = "1.0.1"; + private static final String VERSION = "1.1"; private static final int DEFAULT_SIZE = 17; private static final String INFO_TEXT = "" + "" + @@ -24,13 +24,13 @@ class MainWindow extends JFrame { "[0] - Show/hide this info
" + "[1] - Show grid
" + "[2] - Add random
"; - private MainPanel mp; - private RefreshThread refresh; + private transient MainPanel mp; + private transient RefreshThread refresh; MainWindow() { Map m = new Map(DEFAULT_SIZE); - String saved = FileUtils.open(FILE_NAME); + String saved = Utils.openFile(FILE_NAME); if (saved != null) m = new Map(saved); this.mp = new MainPanel(m); diff --git a/src/main/java/fr/klemek/mapping/FileUtils.java b/src/main/java/fr/klemek/mapping/Utils.java similarity index 76% rename from src/main/java/fr/klemek/mapping/FileUtils.java rename to src/main/java/fr/klemek/mapping/Utils.java index 2487bce..33a87ca 100644 --- a/src/main/java/fr/klemek/mapping/FileUtils.java +++ b/src/main/java/fr/klemek/mapping/Utils.java @@ -8,13 +8,17 @@ import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; -final class FileUtils { +final class Utils { - private FileUtils() { + private Utils() { } - static boolean save(String fileName, String content) { + static int dist2(int x1, int y1, int x2, int y2) { + return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); + } + + static boolean saveFile(String fileName, String content) { try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) { bw.write(content); return true; @@ -24,7 +28,7 @@ final class FileUtils { } } - static String open(String fileName) { + static String openFile(String fileName) { try (BufferedReader bw = new BufferedReader(new FileReader(fileName))) { StringBuilder output = new StringBuilder(); String line; diff --git a/src/test/java/fr/klemek/mapping/MapTest.java b/src/test/java/fr/klemek/mapping/MapTest.java index d7cd2c2..92d3937 100644 --- a/src/test/java/fr/klemek/mapping/MapTest.java +++ b/src/test/java/fr/klemek/mapping/MapTest.java @@ -3,6 +3,7 @@ package fr.klemek.mapping; import org.junit.Test; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; public class MapTest { @@ -17,4 +18,33 @@ public class MapTest { for (int y = 0; y < m.size(); y++) assertEquals(m.get(x, y), m2.get(x, y), 0.001f); } + + @Test + public void testReset() { + Map m = new Map(15); + m.randomize(); + assertNotEquals(0, m.get(0, 0), 0.001f); + m.reset(); + assertEquals(0, m.get(0, 0), 0.001f); + } + + @Test + public void testChangeSize() { + Map m = new Map(15); + m.randomize(); + float value = m.get(0, 0); + assertEquals(15, m.size()); + m.changeSize(18); + assertEquals(18, m.size()); + assertEquals(value, m.get(0, 0), 0.001f); + assertEquals(0, m.get(17, 17), 0.001f); + } + + @Test + public void testRandomize() { + Map m = new Map(15); + assertEquals(0, m.get(0, 0), 0.001f); + m.randomize(); + assertNotEquals(0, m.get(0, 0), 0.001f); + } } \ No newline at end of file diff --git a/src/test/java/fr/klemek/mapping/UtilsTest.java b/src/test/java/fr/klemek/mapping/UtilsTest.java new file mode 100644 index 0000000..d1ce411 --- /dev/null +++ b/src/test/java/fr/klemek/mapping/UtilsTest.java @@ -0,0 +1,39 @@ +package fr.klemek.mapping; + +import java.io.File; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +public class UtilsTest { + + @Test + public void testDist2() { + assertEquals(0, Utils.dist2(1, 1, 1, 1)); + assertEquals(1, Utils.dist2(1, 1, 2, 1)); + assertEquals(2, Utils.dist2(1, 1, 2, 2)); + } + + @Test + public void testSaveOpenFile() { + File f = new File("test.txt"); + if (f.exists()) + assertTrue(f.delete()); + + assertNull(Utils.openFile("test.txt")); + + assertTrue(Utils.saveFile("test.txt", "test\ntest\n")); + assertTrue(f.exists()); + + String content = Utils.openFile("test.txt"); + assertNotNull(content); + assertEquals("test\ntest\n", content); + + assertTrue(f.delete()); + } + +} \ No newline at end of file