JPanel上的MouseEvent - 错误的坐标

时间:2012-08-27 06:57:03

标签: java swing awt jpanel mouseevent

我用Java编写了以下微绘画程序:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


class AuxClass1 extends JFrame implements MouseListener, MouseMotionListener{

    private JPanel panel1 = new JPanel();
    private JPanel panel2 = new JPanel();
    private JLabel label1_x = new JLabel();
    private JLabel label1_y = new JLabel();
    private JLabel label1_x_info = new JLabel("");
    private JLabel label1_y_info = new JLabel("");
    //add a container keep panels with widgets 
    private Container con1 = getContentPane();

    private int xval1;
    private int yval1;

    private GridLayout layout1 = new GridLayout(2,2,2,2);

    private JOptionPane info1 = new JOptionPane();

    //get the class that controls the mouse
    public AuxClass1(){
        super("Mouse Experiment");
        panel1.setBackground(Color.WHITE);      
        panel1.setLayout(layout1);
        label1_x.setText("X Location");
        label1_x.setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
        label1_y.setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
        label1_x_info.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
        label1_y_info.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
        label1_y.setText("Y Location");     
        panel1.add(label1_x);
        panel1.add(label1_y);
        panel1.add(label1_x_info);
        panel1.add(label1_y_info);
        con1.add(panel1, BorderLayout.NORTH);
        panel2.setBackground(new Color(100,200,200));
        panel2.setBorder(BorderFactory.createLineBorder(new Color(255,255,0), 2));
        panel2.addMouseListener(this);
        panel2.addMouseMotionListener(this);
        con1.add(panel2, BorderLayout.CENTER);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(500, 500);
        setLocationRelativeTo(null);
        setVisible(true);


    }

    @Override
    public void mouseClicked(MouseEvent arg0) {


    }

    @Override
    public void mouseMoved(MouseEvent arg0) {
        // TODO Auto-generated method stub
        if (arg0.getSource()==panel2){
            x_var = arg0.getX();
            y_var = arg0.getY();
            label1_x_info.setText(Integer.toString(x_var));
            label1_y_info.setText(Integer.toString(y_var));
        }

    }

    @Override
    public void mouseDragged(MouseEvent e) {
        // TODO Auto-generated method stub
        if (e.getSource()==panel2){
            //info1.showMessageDialog(this, "This is an awesome Mouse toolbox!");
            xval1 = e.getX();
            yval1= e.getY();
            AuxClass2 Inst2 = new AuxClass2(xval1, yval1);
            Inst2.paintComponent(getGraphics());
            }

    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        if (arg0.getSource()==panel2){
            label1_x_info.setText("");
            label1_y_info.setText("");
        }
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }


}

class AuxClass2 extends JPanel{

    //JOptionPane info2 = new JOptionPane();
    private int xval2;
    private int yval2;

    public AuxClass2(int input1, int input2){

        xval2 = input1;
        yval2 = input2;
        setSize(500,500);

    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponents(g);
        g.setColor(Color.BLUE);
        g.fillRect(xval2, yval2+70, 5, 5);  

    }

}

public class MainClass{

    private static AuxClass1 Inst1;

    public static void main(String args[]){

        Inst1 = new AuxClass1();


    }

}

除了mouseDragged方法的Y坐标外,它的工作正常(请参阅类t3_aux2中的paintComponent方法)。由于某种原因,该方法使用的Y坐标比panel2中的实际坐标小约70个像素。我怀疑这与t3_aux2类中继承的JPanel方法有关,但不太确定。

如果有人能澄清这一点,那就太酷了。谢谢。

UPD:如果有人就如何改进风格和/或优化代码提出建议,那么也会受到大力赞赏。

UPD2:更改名称以符合Java命名约定。

1 个答案:

答案 0 :(得分:5)

我尝试了你的代码。我认为您的问题来自于使用 t3_aux2 坐标在 t3_aux1 上绘画的事实。我会尝试确认一下,然后我回到这里......

编辑:好的,就是这样。

t3_aux1 构造函数中,如果你写

System.out.println("panel1 height = " + panel1.getHeight());
System.out.println("label1 height = " + label1_x.getHeight())

打印

panel1 height = 42
label1 height = 20

所以你的偏移是42 + 20 + 4 * 2 = 70

4 * 2来自您的线条边框,厚度为2。

由于可以计算确切的偏移量,因此可以动态修复它。

编辑2:

实际上,您使用的坐标来自panel2,因为mouseListener附加到panel2。但是您可以使用JFrame Graphics,而不是使用panel2图形。

写这个应该可以解决你的坐标问题。

inst2.paintComponent(panel2.getGraphics());

但正如克列奥帕特拉所说,你并没有以正确的方式做到这一点。您永远不应该致电getGraphics()paintComponent()。我认为你应该考虑使用java.awt.Canvas作为你的“panel2”对象的超类。

还有一条建议:请注意您的图纸不会被记忆,因此,如果您缩小窗口或将其隐藏在另一个窗口后面,所绘制的所有内容都将丢失。