Hallo, ich programmiere im Moment ein tool zum zeichnen. Dort habe ich ein grid, wo jedes Feld ein Object der Klasse
ist. Wenn das Mausrad gedreht wird, soll auf die aktuelle Position (also Node mit der Mausposition) gezoomed werden. Ich habe es schon fast geschafft, aber man kann nur an die Ecke oben links zoomen.
Klasse Node:
Klasse frame
Ich hoffe ihr versteht, was ich will und gecoded habe. Danke im Voraus
Java:
Node
Java:
class Handler implements MouseWheelListener{
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
int lastSize = Node.SIZE;
if (e.getWheelRotation() != 0) {
// ändert die size aus der klasse
Node.SIZE += scrollSpeed * e.getWheelRotation();
if (Node.SIZE > Node.MAX_SIZE)
Node.SIZE = Node.MAX_SIZE;
else if (Node.SIZE < Node.MIN_SIZE)
Node.SIZE = Node.MIN_SIZE;
if (lastSize != Node.SIZE)
Node.refactor(lastSize);
frame.repaint();
}
}
}
Klasse Node:
Java:
class Node {
public static void refactor(int lastSize) {
for (Node n : nodes) {
int x = n.getX() / lastSize, y = n.getY() / lastSize;
n.setXY(x * SIZE, y * SIZE);
}
createBias(mouse);
}
/** gets the node of the point in parameter
*
*@return matching node OR null if there is no matching node
*
* // die methode funktioniert so, wie ich will
*/
public static Node pick(Point mouse) {
// mouse.x += xBias;
// mouse.y += yBias;
// das mit bias ist ein versuch das zu errichen
int x = (int) Math.floor(mouse.getX() / Node.SIZE), y = (int) Math.floor(mouse.getY() / Node.SIZE);
if (Var.isInBounds(nodes, (int) Math.sqrt(Main.MAX_ZOOM) * x + y))
return Node.nodes.get((int) Math.sqrt(Main.MAX_ZOOM) * x + y);
return null;
}
public static void createBias(Point mouse) {
Node n = pick(mouse);
xBias = n.getX();
yBias = n.getY();
}
}
Klasse frame
Java:
private class Draw extends JLabel{
@Override
protected void paintComponent(Graphics g1d) {
super.paintComponent(g1d);
Graphics2D g2 = (Graphics2D) g1d;
for (int i = 0; i < Node.nodes.size(); i++) {
Node n = Node.nodes.get(i);
Rectangle bound = Var.clone(n.getBounds());
bound.x += Node.xBias;
bound.y += Node.yBias;
if (!getGreaterBounds().contains(bound))
continue;
g2.setColor(n.getColor());
g2.fill(bound);
if (n.getColor().equals(Node.DEFAULT_COLOR)) {
g2.setColor(Color.black);
g2.draw(bound);
}
}
g2.dispose();
}
}