2016-04-20 123 views
0

我有这段代码,它创建了一个简单的笑脸。我想用鼠标移动来移动脸部的眼睛。这里是代码如何在java中移动鼠标移动的形状

int a,b; 
public void recieve(int x,int y) 
{ 
    a=x; 
    b=y; 
    System.out.println("xaxis"+a+"yaxis"+b); 
} 

@Override 
public void paintComponent(Graphics g) 
{ 
    super.paintComponent(g); 
    System.out.println("x axis: "+a+" -- y axis: "+b); 
    g.drawArc(100, 50, 150, 150, 0, 360); 
    g.drawArc(125, 65, 40, 40, 0, 360); 
    g.drawArc(180, 65, 40, 40, 0, 360); 
    g.drawArc(165, 105, 15, 15, 60, 180); 


    g.fillOval(a-130, 70, 15, 15); 
    g.fillOval(b-185, 70, 15, 15); 
} 
+1

这里的[一个例子(http://stackoverflow.com/questions/34981403/bufferedimage-not-being-cleared-before-each-rendering/35002727#35002727)你在找什么。 –

回答

1

您需要实现MouseMotionListener并将其附加到paintComponent()方法所来自的任何JPanel子类。在其方法中,修改'a'和'b'变量的值。

下面是一个例子。从你的问题来看,我不清楚你想让眼睛做什么类型的动作 - 例如,是否瞳孔应始终在不离开眼睛的情况下“查看”​​鼠标指针 - 从左眼的鼠标指针的x坐标减去x的起始值,并从x的y坐标减去x的起始值您的代码片断所提示的右眼鼠标指针并不会真正产生有意义的结果。

因此,在下面的演示中,眼睛只是追踪鼠标指针。它向你展示了如何拾取鼠标移动事件并获得鼠标的X和Y坐标 - 你如何使用这些来调整瞳孔的位置,而不管你想要的方式取决于你,并且(希望是)现在只是一个数学问题。

import javax.swing.SwingUtilities; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import java.awt.Graphics; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseMotionListener; 

public class MouseTrackingSmilieFace extends JFrame { 

    private int a; 
    private int b; 

    private JPanel smilieFacePanel = new JPanel() { 
     @Override 
     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      g.drawArc(100, 50, 150, 150, 0, 360); 
      g.drawArc(125, 65, 40, 40, 0, 360); 
      g.drawArc(180, 65, 40, 40, 0, 360); 
      g.drawArc(165, 105, 15, 15, 60, 180); 
      g.fillOval(a, b, 15, 15); 
      g.fillOval(a+55, b, 15, 15); 
      // Register the motion listener 
      addMouseMotionListener(new MouseMotionListener() { 

       // Do the same thing whether the mouse button is depressed... 
       public void mouseDragged(MouseEvent e) { 
        processMovement(e); 
       } 

       // ... or not. 
       public void mouseMoved(MouseEvent e) { 
        processMovement(e); 
       } 

       /* Process the movement by capturing the x coordinate of the mouse in member variable 'a' 
       * and the y coordinate in member variable 'b'. 
       */ 
       private void processMovement(MouseEvent e) { 
        a = e.getX(); 
        b = e.getY(); 
        System.out.println("X = " + a + " Y = " + b); 
        smilieFacePanel.repaint(); 
       } 
      }); 
     } 
    }; 

    private MouseTrackingSmilieFace() { 
     // Invoke the JFrame superclass constructor and configure the frame 
     super("Mouse-tracking smilie face"); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setSize(400,400); 
     this.setVisible(true); 
     // Add a panel to the frame and draw the face within it. 
     this.add(smilieFacePanel); 
    } 

    public static void main(String[] args) { 
     // Create on the Event Dispatch Thread. 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       new MouseTrackingSmilieFace(); 
      } 
     }); 
    } 
}