gpt4 book ai didi

java - 鼠标监听器没有给出正确的点

转载 作者:行者123 更新时间:2023-12-01 22:57:29 25 4
gpt4 key购买 nike

我正在制作一个程序,根据用户输入绘制不同的形状。当按下并拖动光标时绘制形状。但由于某种原因,每当按下光标时,形状都会在光标下方约 25 像素处开始。有人知道原因以及如何解决吗?

我的代码

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;


public class DrawShapes extends JFrame
{
private JLabel display;
private Point point1, point2;
private ShapeDrawer drawer;

public DrawShapes()
{
Container contain = getContentPane();

point1 = null;
point2 = null;

//Setup JLabel and add it to container
display = new JLabel("Line");
contain.add(display, BorderLayout.SOUTH);

//Setup JPanel and add it to top of container
drawer = new ShapeDrawer();
drawer.setPreferredSize(new Dimension(500,500));
contain.add(drawer, BorderLayout.NORTH);

//Setup listeners
ShapeListener listener = new ShapeListener();
addKeyListener(listener);
addMouseListener(listener);
addMouseMotionListener(listener);

setTitle("Press l for line, o for oval, r for rectangle");
pack();
setVisible(true);
}

//Class for drawing shapes
private class ShapeDrawer extends JPanel
{
public void paintComponent(Graphics paint)
{
super.paintComponent(paint);

Graphics2D g2 = (Graphics2D) paint;
setBackground(Color.white);
g2.setColor(Color.black);

//Draw different shapes based on user input
if(point1 != null && point2 != null)
{
if(display.getText().equals("Circle"))
{
g2.fillOval(point1.x - 6, point1.y - 6,
6* 2, 6 * 2);
}
else if(display.getText().equals("Oval"))
{
g2.drawOval(point1.x,point1.y,point2.x,point2.y);
}
else if(display.getText().equals("Line"))
{
g2.drawLine(point1.x, point1.y, point2.x, point2.y);
}
else if(display.getText().equals("Rectangle"))
{
g2.drawRect(point1.x, point1.y, point2.x, point2.y);
}
}
}
}

private class ShapeListener implements MouseListener, MouseMotionListener ,KeyListener
{
//Change the label if something else is pressed
public void keyPressed(KeyEvent event)
{
if(event.getKeyText(event.getKeyCode()).equals("C"))
display.setText("Circle");
else if(event.getKeyText(event.getKeyCode()).equals("O"))
display.setText("Oval");
else if(event.getKeyText(event.getKeyCode()).equals("R"))
display.setText("Rectangle");
else if(event.getKeyText(event.getKeyCode()).equals("L"))
display.setText("Line");
}

public void keyReleased(KeyEvent event){};
public void keyTyped(KeyEvent event){};

//Get first point where mouse is pressed
public void mousePressed(MouseEvent event)
{
point1 = event.getPoint();

}

//Get the point while mouse is being pressed
public void mouseDragged(MouseEvent event)
{
point2 = event.getPoint();
drawer.repaint();

}

public void mouseReleased(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
public void mouseClicked(MouseEvent event) {}
public void mouseMoved(MouseEvent event) {}



}
}

以及主要方法

import javax.swing.JFrame;
class ShapesViewer
{
public static void main(String[] args)
{
DrawShapes shapes = new DrawShapes();

shapes.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

最佳答案

您需要将 MouseListener 添加到绘图 JPanel,而不是 JFrame。

更改此:

ShapeListener  listener = new ShapeListener();
addMouseListener(listener);
addMouseMotionListener(listener);

对此:

drawer.ShapeListener  listener = new ShapeListener();
drawer.addMouseListener(listener);
drawer.addMouseMotionListener(listener);

由于菜单栏的原因,JFrame 中的点位置比 JPanel 上方 25 点。

关于java - 鼠标监听器没有给出正确的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23819845/

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