Hey, ich habe ein Programm, in welchem ich in die Mitte der Gitternetzlinien zoomen will. Ich habe mir viel zu dem Thema angesehen und zuletzt versucht eine StackOverFlow Antwort einzubauen.
Aber egal was ich probiere, es wird immer in die falsche Richtung gezoomt. Wie kann ich also zentral auf das Grid, welches in einem JPanel in einem JFrame integriert ist zoomen?
In den Zeilen 21 - 26 und 72 - 77 ist eigentlich alles relevante
[CODE lang="java" title="Grid.java" highlight="21-26, 72-77"]import java.awt.*;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;
public class Grid {
static int width, height, size, camX, camY;
static double zoom = 1;
static ArrayList<Cell> cells;
public static void init(int width, int height, int size, ArrayList<Cell> cells) {
Grid.width = width;
Grid.height = height;
Grid.size = size;
Grid.cells = cells;
}
static AffineTransform at = new AffineTransform();
public static void render(Graphics2D g2) {
// zooming and panning
at.translate(
(width * size * (1 - zoom)) / 2,
(height * size * (1 - zoom)) / 2);
at.scale(zoom, zoom);
zoom = 1;
g2.setTransform(at);
g2.translate(camX, camY);
for (Cell cell : cells) {
g2.setColor(cell.color);
g2.fillRect(size * cell.x, size * cell.y, size, size);
// count overlapping cells
int times = 0;
for (Cell cellCounter : cells) {
if (cellCounter.x == cell.x && cellCounter.y == cell.y) {
times++;
}
}
if (times > 1) {
g2.setColor(Grid.getContrastColor(cell.color));
g2.setFont(new Font("TimesRoman", Font.PLAIN, size));
g2.drawString(times + "", size * cell.x, size * (cell.y + 1));
}
}
// render Grid
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
g2.setColor(Color.white);
g2.drawRect(size * i, size * j, size, size);
}
}
}
public static void move(Movement move) {
switch (move) {
case GRID_UP:
move(0, 1);
break;
case GRID_DOWN:
move(0, -1);
break;
case GRID_LEFT:
move(1, 0);
break;
case GRID_RIGHT:
move(-1, 0);
break;
case GRID_ZOOM_IN:
zoom = 1.1;
break;
case GRID_ZOOM_OUT:
zoom = 0.9;
break;
}
Main.getStatic.main.frame.repaint();
}
private static void move(int x, int y) {
camX += x * size;
camY += y * size;
}
private static Color getContrastColor(Color color) {
double y = (299 * color.getRed() + 587 * color.getGreen() + 114 * color.getBlue()) / 1000;
return y >= 128 ? Color.black : Color.white;
}
}[/CODE]
Aber egal was ich probiere, es wird immer in die falsche Richtung gezoomt. Wie kann ich also zentral auf das Grid, welches in einem JPanel in einem JFrame integriert ist zoomen?
In den Zeilen 21 - 26 und 72 - 77 ist eigentlich alles relevante
[CODE lang="java" title="Grid.java" highlight="21-26, 72-77"]import java.awt.*;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;
public class Grid {
static int width, height, size, camX, camY;
static double zoom = 1;
static ArrayList<Cell> cells;
public static void init(int width, int height, int size, ArrayList<Cell> cells) {
Grid.width = width;
Grid.height = height;
Grid.size = size;
Grid.cells = cells;
}
static AffineTransform at = new AffineTransform();
public static void render(Graphics2D g2) {
// zooming and panning
at.translate(
(width * size * (1 - zoom)) / 2,
(height * size * (1 - zoom)) / 2);
at.scale(zoom, zoom);
zoom = 1;
g2.setTransform(at);
g2.translate(camX, camY);
for (Cell cell : cells) {
g2.setColor(cell.color);
g2.fillRect(size * cell.x, size * cell.y, size, size);
// count overlapping cells
int times = 0;
for (Cell cellCounter : cells) {
if (cellCounter.x == cell.x && cellCounter.y == cell.y) {
times++;
}
}
if (times > 1) {
g2.setColor(Grid.getContrastColor(cell.color));
g2.setFont(new Font("TimesRoman", Font.PLAIN, size));
g2.drawString(times + "", size * cell.x, size * (cell.y + 1));
}
}
// render Grid
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
g2.setColor(Color.white);
g2.drawRect(size * i, size * j, size, size);
}
}
}
public static void move(Movement move) {
switch (move) {
case GRID_UP:
move(0, 1);
break;
case GRID_DOWN:
move(0, -1);
break;
case GRID_LEFT:
move(1, 0);
break;
case GRID_RIGHT:
move(-1, 0);
break;
case GRID_ZOOM_IN:
zoom = 1.1;
break;
case GRID_ZOOM_OUT:
zoom = 0.9;
break;
}
Main.getStatic.main.frame.repaint();
}
private static void move(int x, int y) {
camX += x * size;
camY += y * size;
}
private static Color getContrastColor(Color color) {
double y = (299 * color.getRed() + 587 * color.getGreen() + 114 * color.getBlue()) / 1000;
return y >= 128 ? Color.black : Color.white;
}
}[/CODE]