v1.1.1 : code optimization

This commit is contained in:
Klemek
2018-11-04 13:33:48 +01:00
parent 62431c6e04
commit e2a5fc101c
4 changed files with 119 additions and 85 deletions
+1 -1
View File
@@ -6,7 +6,7 @@
<groupId>fr.klemek</groupId> <groupId>fr.klemek</groupId>
<artifactId>mapping</artifactId> <artifactId>mapping</artifactId>
<version>1.1</version> <version>1.1.1</version>
<repositories> <repositories>
<repository> <repository>
+86 -84
View File
@@ -1,8 +1,6 @@
package fr.klemek.mapping; package fr.klemek.mapping;
import java.awt.*; import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.*; import javax.swing.*;
@@ -13,10 +11,10 @@ class MainPanel extends JPanel {
private float ratio = DEFAULT_RATIO; private float ratio = DEFAULT_RATIO;
private transient Map map; private transient Map map;
private int cx; private int x0;
private int cy; private int y0;
private float mw; private float xstep;
private float mh; private float ystep;
private int sx = -1; private int sx = -1;
private int sy = -1; private int sy = -1;
@@ -29,45 +27,7 @@ class MainPanel extends JPanel {
MainPanel(Map map) { MainPanel(Map map) {
this.map = map; this.map = map;
new MouseListener(this);
this.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent e) {
//ignored
}
@Override
public void mouseMoved(MouseEvent e) {
sx = -1;
sy = -1;
if (MainPanel.this.xs != null) {
for (int x = 0; x < MainPanel.this.map.size(); x++)
for (int y = 0; y < MainPanel.this.map.size(); y++)
if (e.getPoint().distance(MainPanel.this.xs[x][y], MainPanel.this.ys[x][y]) < 6) {
sx = x;
sy = y;
return;
}
}
}
});
this.addMouseWheelListener(e -> {
if (sx >= 0 && sy >= 0) {
if (e.isShiftDown()) {
for (int dx = -1; dx < 2; dx++)
for (int dy = -1; dy < 2; dy++)
if (sx + dx >= 0 && sx + dx < MainPanel.this.map.size()
&& sy + dy >= 0 && sy + dy < MainPanel.this.map.size())
MainPanel.this.map.set(sx + dx, sy + dy, MainPanel.this.map.get(sx + dx, sy + dy)
+ (e.getPreciseWheelRotation() < 0 ? 1f : -1f)
* (e.isControlDown() ? 1f : .1f));
} else {
MainPanel.this.map.set(sx, sy, MainPanel.this.map.get(sx, sy)
+ (e.getPreciseWheelRotation() < 0 ? 1f : -1f)
* (e.isControlDown() ? 1f : .1f));
}
}
});
} }
@Override @Override
@@ -77,56 +37,65 @@ class MainPanel extends JPanel {
RenderingHints.KEY_ANTIALIASING, RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON)); RenderingHints.VALUE_ANTIALIAS_ON));
int w = this.getWidth(); g2.clearRect(0, 0, this.getWidth(), this.getHeight());
int h = this.getHeight(); this.computeNodes();
this.cx = Math.round(w / 2f); if (this.showGrid)
this.cy = Math.round(h / 2f); this.drawGrid(g2);
if (w > h) { this.drawMap(g2);
this.mh = h * SIZE * ratio;
this.mw = mh / ratio;
} else {
this.mw = w * SIZE;
this.mh = mw * ratio;
}
g2.clearRect(0, 0, w, h);
drawMap(g2);
} }
private void drawMap(Graphics2D g2) { private void computeNodes() {
g2.setStroke(new BasicStroke(1f)); int w = this.getWidth();
int h = this.getHeight();
int x0 = Math.round(this.cx - this.mw / 2f); float mw;
float mh;
if (w > h) {
mh = h * SIZE * ratio;
mw = mh / ratio;
} else {
mw = w * SIZE;
mh = mw * ratio;
}
this.x0 = Math.round(w / 2f - mw / 2f);
this.y0 = Math.round(h / 2f);
this.xs = new int[this.map.size()][this.map.size()]; this.xs = new int[this.map.size()][this.map.size()];
this.ys = new int[this.map.size()][this.map.size()]; this.ys = new int[this.map.size()][this.map.size()];
float xstep = this.mw / (2 * (this.map.size() - 1)); this.xstep = mw / (2 * (this.map.size() - 1));
float ystep = this.mh / (2 * (this.map.size() - 1)); this.ystep = mh / (2 * (this.map.size() - 1));
g2.setColor(Color.GRAY);
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 = this.getPoint(x0, xstep, ystep, x, y, this.map.get(x, y)); Point p = this.getPoint(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) {
Point p0 = this.getPoint(x0, xstep, ystep, x, y, 0);
if (x > 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 = 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);
}
} }
} }
}
private void drawGrid(Graphics2D g2) {
g2.setColor(Color.GRAY);
for (int x = 0; x < this.map.size(); x++) {
for (int y = 0; y < this.map.size(); y++) {
Point p0 = this.getPoint(x, y, 0);
if (x > 0) {
Point p1 = this.getPoint(x - 1, y, 0);
g2.drawLine(p0.x, p0.y, p1.x, p1.y);
}
if (y > 0) {
Point p1 = this.getPoint(x, y - 1, 0);
g2.drawLine(p0.x, p0.y, p1.x, p1.y);
}
g2.drawLine(p0.x, p0.y, this.xs[x][y], this.ys[x][y]);
}
}
}
private void drawMap(Graphics2D g2) {
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++) {
@@ -136,15 +105,15 @@ class MainPanel extends JPanel {
} }
} }
private Point getPoint(int x0, float xstep, float ystep, int x, int y, float value) { private Point getPoint(int x, int y, float value) {
float center = this.map.size() / 2f; float center = this.map.size() / 2f;
double r = Math.sqrt(Math.pow(x - center, 2) + Math.pow(y - center, 2)); double r = Math.sqrt(Math.pow(x - center, 2) + Math.pow(y - center, 2));
double a = Math.atan2(y - center, x - center); double a = Math.atan2(y - center, x - center);
double x1 = center + r * Math.cos(a + this.angle); double x1 = center + r * Math.cos(a + this.angle);
double y1 = center + r * Math.sin(a + this.angle); double y1 = center + r * Math.sin(a + this.angle);
return new Point( return new Point(
Math.round(x0 + xstep * (float) x1 + xstep * (float) y1), Math.round(this.x0 + this.xstep * (float) x1 + this.xstep * (float) y1),
Math.round(this.cy - ystep * (float) x1 + ystep * (float) y1 - ystep * value) Math.round(this.y0 - this.ystep * (float) x1 + this.ystep * (float) y1 - this.ystep * value)
); );
} }
@@ -162,6 +131,39 @@ class MainPanel extends JPanel {
} }
} }
void computeMouseMoved(Point position) {
sx = -1;
sy = -1;
if (MainPanel.this.xs != null) {
for (int x = 0; x < MainPanel.this.map.size(); x++)
for (int y = 0; y < MainPanel.this.map.size(); y++)
if (position.distance(MainPanel.this.xs[x][y], MainPanel.this.ys[x][y]) < 6) {
sx = x;
sy = y;
return;
}
}
}
void computeMouseWheel(int amount, boolean controlDown, boolean shiftDown) {
if (sx >= 0 && sy >= 0) {
if (shiftDown) {
for (int dx = -1; dx < 2; dx++)
for (int dy = -1; dy < 2; dy++)
changeNode(amount, controlDown, sx + dx, sy + dy);
} else
changeNode(amount, controlDown, sx, sy);
}
}
private void changeNode(int amount, boolean controlDown, int x, int y) {
if (x >= 0 && x < this.map.size()
&& y >= 0 && y < this.map.size())
this.map.set(x, y, MainPanel.this.map.get(x, y)
+ amount * (controlDown ? -1f : -.1f));
}
float getRatio() { float getRatio() {
return ratio; return ratio;
} }
@@ -0,0 +1,32 @@
package fr.klemek.mapping;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
public class MouseListener implements MouseMotionListener, MouseWheelListener {
private MainPanel mp;
MouseListener(MainPanel mp) {
this.mp = mp;
this.mp.addMouseWheelListener(this);
this.mp.addMouseMotionListener(this);
}
@Override
public void mouseDragged(MouseEvent e) {
//ignored
}
@Override
public void mouseMoved(MouseEvent e) {
this.mp.computeMouseMoved(e.getPoint());
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
this.mp.computeMouseWheel(e.getWheelRotation(), e.isControlDown(), e.isShiftDown());
}
}