我把一个从JPanel继承的类放在JFrame上面,我在处理鼠标事件时,鼠标的点击值是相对JFrame的坐标系的,我怎么让这个转化成JPanel的坐标值。

解决方案 »

  1.   

    大家运行一下这个,明明声明两条相同的线,画出的位置不一样。
    第二:程序本身是按住线段中点附近,然后可以拖动线段,但是好象坐标系不统一,但是我是响应的JPanel的鼠标消息啊,怎么会返回的鼠标值是JFrame的import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.event.*;class tcomponent extends Component {
     int x1,y1;
     int x2,y2;

     boolean select;
     
     public void  setselect(boolean bs)
     {
      select = bs;
     }

    public tcomponent(int x11,int y11,int x12, int y12)
    {
    x1 = x11;
    y1 = y11;
    x2 = x12;
    y2 = y12;
    }

    public void paint(Graphics g)             //auto repaint
    {
    this.setSize(new Dimension(20000,20000));
                       g.setColor(Color.black);
                      g.drawLine(x1,y1,x2,y2);              
    }

     public void change(int x11,int y11,int x12, int y12)
    {
    x1 = x11;
    y1 = y11;
    x2 = x12;
    y2 = y12;
    repaint();
    }

    }
    public class testcomponent extends JFrame
    {
    public testcomponent()
    {
    CenterPanel centerPanel = new CenterPanel();
    Container c = this.getContentPane();
    c.add(centerPanel,BorderLayout.CENTER);     
    setSize(1024,768);
    setTitle("My Strong Will");
    setLocation(0,0);

    show();



    }
    class CenterPanel extends JPanel implements MouseMotionListener, MouseListener
    {
    int x,y;                        
    int x_move,y_move;


    tcomponent pb1;
    tcomponent pb2;
    public CenterPanel()
    {
    pb1 = new tcomponent(200,0,0,200);
    pb1.setselect(false);
    //pb2 = new tcomponent(300,300,500,700);
    pb2 = new tcomponent(200,0,0,200);
    pb2.setselect(false);
    addMouseListener(this);
           addMouseMotionListener(this);
    this.add(pb1);
    this.add(pb2);

    }


    public void mousePressed(MouseEvent e)
    {
            x = e.getX();
           y = e.getY();
          
           System.out.println(x);
           System.out.println(y);      
          
           //only the hit point belong to the midpoint area. 
           if(x>(pb1.x1+pb1.x2)/2-20 && x<(pb1.x1+pb1.x2)/2+20 && y>(pb1.y1+pb1.y2)/2-20 && y<(pb1.y1+pb1.y2)/2+20)
           {
         pb1.select = true;
           System.out.println(pb1.select);
          
           }
          
           if(x>(pb2.x1+pb2.x2)/2-20 && x<(pb2.x1+pb2.x2)/2+20 && y>(pb2.y1+pb2.y2)/2-20 && y<(pb2.y1+pb2.y2)/2+20)
           {       pb2.select = true;
           System.out.println(pb2.select);
           }
          
        }    public void mouseDragged(MouseEvent e)
        { 
        int x_chayi,y_chayi;
        x_move = e.getX();
           y_move = e.getY();
          
           x_chayi = x_move - x;
           y_chayi = y_move - y;
          
           if(pb1.select)
        {
        pb1.change(pb1.x1+x_chayi,pb1.y1+y_chayi,pb1.x2+x_chayi,pb1.y2+y_chayi);
        }
       
         if(pb2.select)
        {
        pb2.change(pb2.x1+x_chayi,pb2.y1+y_chayi,pb2.x2+x_chayi,pb2.y2+y_chayi);
        }
        x = x_move;
        y = y_move;
       
        }
        public void mouseMoved(MouseEvent e)   {  }
        public void mouseClicked(MouseEvent e) { }
        public void mouseReleased(MouseEvent e) 
        {
        pb1.select = false;
        pb2.select = false;
        //System.out.println(pb.select);
        }
        public void mouseEntered(MouseEvent e) { }
        public void mouseExited(MouseEvent e) { }
       
       
    }


    public static void main(String args[])
    {
    new testcomponent();
    }


    }