上的MouseEvent JPanel的 - 错误的坐标 [英] MouseEvent on JPanel - wrong coordinate

查看:146
本文介绍了上的MouseEvent JPanel的 - 错误的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java编写以下微型画笔程序:

I have written the following micro-paintbrush program in 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像素。我怀疑这是值得做的继承JPanel的方法t3_aux2类,但不能肯定。

It works alright except for the Y coordinate of the mouseDragged method (see paintComponent method in class t3_aux2). For some reason the Y coordinate used by the method is ~70 pixels less than the actual one in panel2. I suspect this is something to do with the inherited JPanel method in t3_aux2 class but not too sure.

如果有人能够澄清这一点,它会很酷。谢谢。

If someone could clarify this point, it would be cool. Thanks.

UPD:如果任何人有关于如何改进作风和/或优化code的建议,这将是pciated太大规模AP $ P $。

UPD: If anyone has suggestions on how to improve style and/or optimize code, that would be massively appreciated too.

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

UPD2: Changed the names to comply with Java naming conventions.

推荐答案

我想你的code。我觉得你的问题来自于一个事实,你的 t3_aux1 使用的 t3_aux2 坐标绘制。我会尝试的东西,以确认这一点,我回到这里...

I tried your code. I think your problem comes from the fact that you paint on the t3_aux1 using t3_aux2 coordinates. I'll try something to confirm that, and I come back here ...

编辑:确定,就是它

t3_aux1 的构造函数,如果你写

in t3_aux1 constructor, if you write

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

So your offset is 42 + 20 + 4*2 = 70

4 * 2来自您的线的边界,厚度为2

4*2 comes from your lines borders with a thickness of 2.

由于它是可以计算出准确的偏移,可以动态地解决它。

Since it's possible to calculate the exact offset, you can dynamically fix it.

编辑2:

事实上,你既然来MouseListener的使用从是Panel2坐标连接到是Panel2。但你画上JFrame的图形,而不是图形是Panel2

In fact, the coordinates you uses come from panel2 since the mouseListener is attached to panel2. But you draw on the JFrame Graphics, not on panel2 graphics.

写这应该解决您的坐标的问题。

writing this should fix your coordinates problem.

inst2.paintComponent(panel2.getGraphics());

但随着克列奥帕特拉说,你不这样做是正确的方式。你不应该叫的getGraphics()的paintComponent()。我想你应该考虑使用 java.awt.Canvas的为你的是Panel2对象的超类。

But as Kleopatra said, you're not doing it the right way. You should never call getGraphics() or paintComponent(). I think you should consider using java.awt.Canvas as a super class for your "panel2" object.

还有一个建议:请注意,您的图纸没有记忆,因此,如果您降低窗口或后面躲起来,一个又一个,得出一切都会丢失

One more advice : Be aware that your drawings aren't memorized, so, if you reduce the window or hide it behind another one, everything drawn will be lost.

这篇关于上的MouseEvent JPanel的 - 错误的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆