public static void main(String[] args) {
final ArrayList<double[]> points = new ArrayList<double[]>();
points.add(new double[] { 100, 250 });
points.add(new double[] { 400, 100 });
points.add(new double[] { 600, 250 });
points.add(new double[] { 400, 250 });
points.add(new double[] { 200, 325 });
points.add(new double[] { 600, 400 });
JFrame f = new JFrame();
final Canvas c = new Canvas() {
private static final long serialVersionUID = 1L;
@Override
public void paint(Graphics g) {
super.paint(g);
g.clearRect(0, 0, this.getWidth(), this.getHeight());
for (double[] p : points) {
g.drawOval((int) p[0], (int) p[1], 10, 10);
g.drawString(Arrays.toString(p), (int) p[0] + 10, (int) p[1] + 25);
}
}
};
c.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
double w_max = 800;
double w_min = 0;
double h_max = 800;
double h_min = 0;
// for (double[] p : points) {
// if (p[0] > w_max)
// w_max = p[0];
// if (p[1] > h_max)
// h_max = p[1];
// if (p[0] < w_min)
// w_min = p[0];
// if (p[1] < h_min)
// h_min = p[1];
// }
double w = (w_max - w_min) / 2.0;
double h = (h_max - h_min) / 2.0;
switch (e.getKeyChar()) {
case 'w':
for (double[] p : points) {
p[1] = (p[1] - h) * 1.25 + h;
}
break;
case 'a':
for (double[] p : points) {
p[0] = (p[0] - w) * 0.8 + w;
}
break;
case 's':
for (double[] p : points) {
p[1] = (p[1] - h) * 0.8 + h;
}
break;
case 'd':
for (double[] p : points) {
p[0] = (p[0] - w) * 1.25 + w;
}
break;
default:
break;
}
// ???
for (double[] p : points) {
p[0] = new BigDecimal(p[0]).round(MathContext.DECIMAL32).doubleValue();
p[1] = new BigDecimal(p[1]).round(MathContext.DECIMAL32).doubleValue();
}
// ???
c.repaint();
}
});
f.add(c);
f.setSize(800, 800);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setVisible(true);
}