gpt4 book ai didi

java - MouseEvent getX 和 getY 相对于实际坐标的偏移

转载 作者:行者123 更新时间:2023-12-01 23:15:42 25 4
gpt4 key购买 nike

我正在编写一个基本的绘画程序,但我在 Rectangle 方面遇到了麻烦。和Ellipse工具。如果单击并拖动,您应该能够根据 startpoint 绘制尺寸的形状。和endpoint (都使用 getX()getY() ),问题是这两个形状得到 startpoint对,但是endpoint在 x 和 y 坐标上都有偏移。

下面的代码与我在线条工具中使用的代码(工作正常)几乎相同,除了交换 Line2DRectangle2DEllipse2D分别。

package tools;

import gui.DrawingPanel;

import java.awt.event.ActionEvent;

import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;


/**
* Creates the Rectangle Action.
*@version 1
*/
public class RectangleAction extends AbstractAction {

private final DrawingPanel myPanel;

private Rectangle2D.Double myRectangle;
private double Start_X;
private double Start_Y;

/**
* Constructor for Rectangle Action.
*/
public RectangleAction(final DrawingPanel thePanel) {
super("Rectangle", getImageIcon());

myPanel = thePanel;
putValue(Action.MNEMONIC_KEY, KeyEvent.VK_R);
putValue(Action.SELECTED_KEY, true);
}

@Override
public void actionPerformed(final ActionEvent theEvent) {

myPanel.addMouseListener(new MyMouseListener());
myPanel.addMouseMotionListener(new MyMouseListener());

}

/**
* gets the image icon of the action.
* @return the image icon.
*/
public static ImageIcon getImageIcon() {
return new ImageIcon("./images/rectangle.gif");
}


/**
* Listens for mouse clicks, to draw on our panel.
*/
private class MyMouseListener extends MouseAdapter {


private double myX2;
private double myY2;


/**
* Handles a click event.
*
* @param theEvent The event.
*/
@Override
public void mousePressed(final MouseEvent theEvent) {


Start_X = (double) theEvent.getX();
Start_Y = (double) theEvent.getY();
}

/**
* Handles the release event.
*
* @param theEvent The event.
*/
@Override
public void mouseReleased(final MouseEvent theEvent) {

myX2 = (double) theEvent.getX();
myY2 = (double) theEvent.getY();

myRectangle = new Rectangle2D.Double(Start_X, Start_Y, myX2, myY2);

myPanel.setShape(myRectangle);
myPanel.repaint();

}

/**
* Handles a click event.
*
* @param theEvent The event.
*/
@Override
public void mouseDragged(final MouseEvent theEvent) {

myX2 = (double) theEvent.getX();
myY2 = (double) theEvent.getY();

myRectangle = new Rectangle2D.Double(Start_X, Start_Y, myX2, myY2);

myPanel.setShape(myRectangle);
myPanel.repaint();

}

}

}

enter image description here

我应该注意,我确实查看了 this similar question但它并没有给我我正在寻找的答案;还有DrawingPanel只是一个 JPanel使用 Paint 组件来绘制形状,仅此而已。

最佳答案

myRectangle = new Rectangle2D.Double(Start_X, Start_Y, myX2, myY2);

参数是(x,y,宽度,高度),您试图指定两个点。

您的绘画逻辑假设您始终将鼠标从上/左拖动到下/右。当您根据两点计算宽度/高度时,鼠标总是有可能向上和向左拖动,这会导致负值。

这是我用来正确计算矩形边界的代码:

int x = Math.min(startPoint.x, e.getX());
int y = Math.min(startPoint.y, e.getY());
int width = Math.abs(startPoint.x - e.getX());
int height = Math.abs(startPoint.y - e.getY());

您不需要创建两个监听器,只需共享同一个监听器即可:

//myPanel.addMouseListener(new MyMouseListener());
//myPanel.addMouseMotionListener(new MyMouseListener());
MouseAdapter myMouseAdapter = new MyMouseListener();
myPanel.addMouseListener( myMouseAdapter );
myPanel.addMouseMotionListener( myMouseAdapter);

此外,每次单击按钮时都会将适配器添加到面板。因此,如果您依次单击直线工具、椭圆工具和矩形工具,您将在面板中添加 3 个监听器。我建议您在为当前工具添加监听器之前,应从面板中删除所有监听器。

关于java - MouseEvent getX 和 getY 相对于实际坐标的偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33794995/

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