如何获取画布的准确位置(Java)

发布于 2024-08-16 08:24:10 字数 183 浏览 1 评论 0原文

在我的代码中,我使用鼠标坐标与程序交互。从那里,必须在鼠标的位置绘制一些东西。但是,Canvas 的 getX()/getY() 方法返回保存 Canvas 的窗口的值,但是当我去绘制时,(0,0) 稍微位于右侧和下方(经过条形图)窗口左上角,导致偏离正确位置大约 30 像素。有没有办法调和差异,或者我应该简单地全屏窗口或使用其他方法来完全回避问题?

In my code, I use mouse coordinates to interact with the program. From there, something must be drawn at the mouse's location. However, the getX()/getY() methods for the Canvas return the values of the window holding the Canvas, but when I go to draw, (0,0) is located slightly to the right and below (past the bars) the top left of the window, resulting in being approximately 30 pixels off of the correct location. Is there a way to reconcile the differences, or should I simply fullscreen the window or use some other method to sidestep the problem altogether?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心是晴朗的。 2024-08-23 08:24:10

我猜您已将 MouseListenerMouseMotionListener 注册到了窗口。将它们注册到 Canvas 中,您将获得相对于该小部件的坐标。请参阅此示例代码

I guess you registered the MouseListener and MouseMotionListener to the window. Register them for the Canvas and you get coordinates relative to this widget. See this sample code.

风柔一江水 2024-08-23 08:24:10

使用 getX()/getY() 方法,但在 mosueDragged/click 侦听器内,处理事件。

检查此示例(参数):

protected Graphics2D g2;
protected int xStart;
protected int xEnd;
protected int yStart;
protected int yEnd;

//On the parameters, there is the MouseEvent
public void startDrawing(JPanel canvas, MouseEvent evt) {
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    if (g2 == null)

    {
    g2 = (Graphics2D) canvas.getGraphics();
    }

    xStart = evt.getX();
    yStart = evt.getY();
    xEnd = evt.getX();
    yEnd = evt.getY();
}

然后在鼠标按下事件上使用该方法:

 private void canvasMousePressed(java.awt.event.MouseEvent evt) {                                    

    if(penClicked) {
        (penContainer.get(penCount)).startDrawing(canvas, evt);
        (penContainer.get(penCount)).setColor(color);
        penContainer.add(new Pen());
        penCount++;
  }

Using the getX()/getY() methods but inside a mosueDragged/click listener, working with an event.

Check this example (the parameters):

protected Graphics2D g2;
protected int xStart;
protected int xEnd;
protected int yStart;
protected int yEnd;

//On the parameters, there is the MouseEvent
public void startDrawing(JPanel canvas, MouseEvent evt) {
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    if (g2 == null)

    {
    g2 = (Graphics2D) canvas.getGraphics();
    }

    xStart = evt.getX();
    yStart = evt.getY();
    xEnd = evt.getX();
    yEnd = evt.getY();
}

Then the method is used, on a Mouse pressed event:

 private void canvasMousePressed(java.awt.event.MouseEvent evt) {                                    

    if(penClicked) {
        (penContainer.get(penCount)).startDrawing(canvas, evt);
        (penContainer.get(penCount)).setColor(color);
        penContainer.add(new Pen());
        penCount++;
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文