gpt4 book ai didi

java - 当我用鼠标拖动绘制圆形时,形状在某些情况下会移动

转载 作者:行者123 更新时间:2023-12-01 20:25:51 26 4
gpt4 key购买 nike

 @Override
public Shape getShape() {
final Ellipse2D.Double result = new Ellipse2D.Double();
final double px = Math.min(getStart().getX(), getEnd().getX());
final double py = Math.min(getStart().getY(), getEnd().getY());
final double pw = Math.abs(getStart().getX() - getEnd().getX());
result.setFrame(px, py, pw, pw);
return result;
}

所以这个 getShape() 将形状返回给绘制形状的类。 getStart() 获取鼠标单击时的起点,getEnd() 获取鼠标释放时的起点。现在,当我拖动绘制一个圆圈时,如果我向右或向下拖动,圆圈将按预期工作并扩展到鼠标,如果我向上或向左拖动光标,圆圈将按其应有的方式扩展但是 圆形随着光标上下移动,我不知道为什么。

最佳答案

Java 中的形状基于左上角/左上角作为 anchor ,宽度/高度向下/向右绘制。

需要计算点击点和拖动点之间的边界框

public Shape getShape() {
final Ellipse2D.Double result = new Ellipse2D.Double();
final double px = Math.min(getStart().getX(), getEnd().getX());
final double py = Math.min(getStart().getY(), getEnd().getY());
final double pw = Math.abs(getStart().getX() - getEnd().getX());
result.setFrame(px, py, pw, pw);
return result;
}

问题是,您仍然使用点击点和拖动点之间的差异来计算宽度,pw应该是maxXminX

所以this example向您展示如何计算 anchor 和大小属性

I read through that post, but it doesn't solve the dragging expansion of the circle, and how it moves up and down.

那么你做错了什么

I am trying to get a circle drawn not a circle without the same width and height(ellipse).

好的,所以应该总是显示圆是从 anchor 绘制的,因此当 minXminY 小于 clickPointx/y 点,那么您需要根据 clickPoint 和大小的差异来调整它们

Example

因此,这将使其“看起来”好像圆圈始终是从初始点击点绘制的

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SelectionExample {

public static void main(String[] args) {
new SelectionExample();
}

public SelectionExample() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

private Point clickPoint;

private Shape shape;
private Rectangle box;

public TestPane() {
MouseAdapter ma = new MouseAdapter() {

@Override
public void mouseDragged(MouseEvent e) {
int minX = Math.min(e.getX(), clickPoint.x);
int minY = Math.min(e.getY(), clickPoint.y);
int maxX = Math.max(e.getX(), clickPoint.x);
int maxY = Math.max(e.getY(), clickPoint.y);

box = new Rectangle(minX, minY, maxX - minX, maxY - minY);
int size = Math.min(maxX - minX, maxY - minY);
if (minX < clickPoint.x) {
minX = clickPoint.x - size;
}
if (minY < clickPoint.y) {
minY = clickPoint.y - size;
}

shape = new Ellipse2D.Double(minX, minY, size, size);
repaint();
}

@Override
public void mousePressed(MouseEvent e) {
clickPoint = new Point(e.getPoint());
}

};

addMouseListener(ma);
addMouseMotionListener(ma);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (shape != null) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(new Color(0, 0, 255, 64));
g2d.fill(shape);
g2d.setColor(Color.BLUE);
g2d.draw(shape);
g2d.draw(box);
g2d.dispose();
}
}
}

}

关于java - 当我用鼠标拖动绘制圆形时,形状在某些情况下会移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43904865/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com