v1.1 : Optimization, big brush preview and tests

This commit is contained in:
Klemek
2018-11-04 13:08:09 +01:00
parent 1e494d3432
commit 62431c6e04
10 changed files with 136 additions and 50 deletions
Binary file not shown.
Binary file not shown.
+1
View File
@@ -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
+1 -1
View File
@@ -6,7 +6,7 @@
<groupId>fr.klemek</groupId> <groupId>fr.klemek</groupId>
<artifactId>mapping</artifactId> <artifactId>mapping</artifactId>
<version>1.0.1</version> <version>1.1</version>
<repositories> <repositories>
<repository> <repository>
@@ -76,7 +76,7 @@ public class KeyEventListener implements KeyListener {
"Save", "Save",
JOptionPane.YES_NO_OPTION); JOptionPane.YES_NO_OPTION);
if (n2 == JOptionPane.YES_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, JOptionPane.showInternalMessageDialog(this.mp,
"Saved as '" + MainWindow.FILE_NAME + "'", "Saved as '" + MainWindow.FILE_NAME + "'",
"Saved", "Saved",
@@ -90,6 +90,9 @@ public class KeyEventListener implements KeyListener {
} }
} }
break; break;
case KeyEvent.VK_SHIFT:
this.mp.setShiftDown(true);
break;
default: default:
break; break;
} }
@@ -97,6 +100,12 @@ public class KeyEventListener implements KeyListener {
@Override @Override
public void keyReleased(KeyEvent e) { public void keyReleased(KeyEvent e) {
//ignored switch (e.getKeyCode()) {
case KeyEvent.VK_SHIFT:
this.mp.setShiftDown(false);
break;
default:
break;
}
} }
} }
+42 -39
View File
@@ -8,20 +8,23 @@ import javax.swing.*;
class MainPanel extends JPanel { class MainPanel extends JPanel {
final static float DEFAULT_RATIO = .5f; static final float DEFAULT_RATIO = .5f;
private final static float SIZE = 1.2f; private static final float SIZE = 1.2f;
private float ratio = DEFAULT_RATIO; private float ratio = DEFAULT_RATIO;
private Map map; private transient Map map;
private int w, h; private int cx;
private int cx, cy; private int cy;
private float mw, mh; private float mw;
private int sx = -1, sy = -1; private float mh;
private int sx = -1;
private int sy = -1;
private int[][] xs; private int[][] xs;
private int[][] ys; private int[][] ys;
private boolean showGrid; private boolean showGrid;
private boolean shiftDown;
private float angle; private float angle;
MainPanel(Map map) { MainPanel(Map map) {
@@ -74,19 +77,19 @@ class MainPanel extends JPanel {
RenderingHints.KEY_ANTIALIASING, RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON)); RenderingHints.VALUE_ANTIALIAS_ON));
this.w = this.getWidth(); int w = this.getWidth();
this.h = this.getHeight(); int h = this.getHeight();
this.cx = Math.round(this.w / 2f); this.cx = Math.round(w / 2f);
this.cy = Math.round(this.h / 2f); this.cy = Math.round(h / 2f);
if (this.w > this.h) { if (w > h) {
this.mh = this.h * SIZE * ratio; this.mh = h * SIZE * ratio;
this.mw = mh / ratio; this.mw = mh / ratio;
} else { } else {
this.mw = this.w * SIZE; this.mw = w * SIZE;
this.mh = mw * ratio; this.mh = mw * ratio;
} }
g2.clearRect(0, 0, this.w, this.h); g2.clearRect(0, 0, w, h);
drawMap(g2); drawMap(g2);
} }
@@ -105,19 +108,19 @@ class MainPanel extends JPanel {
for (int x = 0; x < this.map.size(); x++) { for (int x = 0; x < this.map.size(); x++) {
for (int y = 0; y < this.map.size(); y++) { 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.xs[x][y] = p.x;
this.ys[x][y] = p.y; this.ys[x][y] = p.y;
if (this.showGrid) { 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) { 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); g2.drawLine(p0.x, p0.y, p1.x, p1.y);
} }
if (y > 0) { 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, p1.x, p1.y);
} }
g2.drawLine(p0.x, p0.y, p.x, p.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 x = 0; x < this.map.size(); x++) {
for (int y = 0; y < this.map.size(); y++) { for (int y = 0; y < this.map.size(); y++) {
if (x > 0) this.drawLine(g2, x - 1, y, x, y);
g2.drawLine(this.xs[x - 1][y], this.ys[x - 1][y], this.xs[x][y], this.ys[x][y]); this.drawLine(g2, x, y - 1, 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]);
} }
} }
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) { 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() { float getRatio() {
return ratio; return ratio;
} }
@@ -190,4 +189,8 @@ class MainPanel extends JPanel {
Map getMap() { Map getMap() {
return map; return map;
} }
void setShiftDown(boolean shiftDown) {
this.shiftDown = shiftDown;
}
} }
@@ -9,7 +9,7 @@ import javax.swing.*;
class MainWindow extends JFrame { class MainWindow extends JFrame {
static final String FILE_NAME = "mapping.csv"; 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 int DEFAULT_SIZE = 17;
private static final String INFO_TEXT = "" + private static final String INFO_TEXT = "" +
"<html>" + "<html>" +
@@ -24,13 +24,13 @@ class MainWindow extends JFrame {
"[0] - Show/hide this info<br>" + "[0] - Show/hide this info<br>" +
"[1] - Show grid<br>" + "[1] - Show grid<br>" +
"[2] - Add random<br>"; "[2] - Add random<br>";
private MainPanel mp; private transient MainPanel mp;
private RefreshThread refresh; private transient RefreshThread refresh;
MainWindow() { MainWindow() {
Map m = new Map(DEFAULT_SIZE); Map m = new Map(DEFAULT_SIZE);
String saved = FileUtils.open(FILE_NAME); String saved = Utils.openFile(FILE_NAME);
if (saved != null) if (saved != null)
m = new Map(saved); m = new Map(saved);
this.mp = new MainPanel(m); this.mp = new MainPanel(m);
@@ -8,13 +8,17 @@ import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; 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))) { try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
bw.write(content); bw.write(content);
return true; 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))) { try (BufferedReader bw = new BufferedReader(new FileReader(fileName))) {
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
String line; String line;
@@ -3,6 +3,7 @@ package fr.klemek.mapping;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
public class MapTest { public class MapTest {
@@ -17,4 +18,33 @@ public class MapTest {
for (int y = 0; y < m.size(); y++) for (int y = 0; y < m.size(); y++)
assertEquals(m.get(x, y), m2.get(x, y), 0.001f); 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);
}
} }
@@ -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());
}
}