0

我正在使用 Java 创建屏幕录像机软件。近80%的工作已经完成。现在我需要使用 Java 创建鼠标点击的视觉标记。这样我就可以在播放视频中看到鼠标被点击的地方。我怎样才能做到这一点?

有没有人有任何代码示例?

4

1 回答 1

0

非常简单。使用 MouseListener 的 getX() 和 getY() 方法读取用户单击鼠标的点。此时,使用 java.awt.Graphics 类的 drawOval() 方法绘制一个椭圆。试试下面的代码,我相信它可以解决你的问题。

import java.awt.*;   
import java.awt.event.*;

// no window closing code
public class MouseXY extends Frame implements MouseListener, MouseMotionListener  
{
          int x , y;
          String str =" ";
          public MouseXY()  
          {
                 setSize(500, 500);
                 setVisible(true); 
                 addMouseListener(this);                // register both the listeners with frame
                 addMouseMotionListener(this);                              
           }                                            // override the 5 abstract methods of ML
           public void mouseEntered(MouseEvent e)   
           { 
                  setBackground(Color.green);
                  x = e.getX();
                  y = e.getY();
                  str ="Mouse Entered";
                  repaint();
           }                                                                   
           public void mouseExited(MouseEvent e)    
           {
                  setBackground(Color.red);
                  x = e.getX();
                  y = e.getY();
                  str ="Mouse Exited";
                  repaint();
           }
           public void mouseClicked(MouseEvent e)
           {
                  setBackground(Color.gray);
                  x = e.getX();
                  y = e.getY();
                  str ="Mouse Clicked";
                  repaint();
           }
           public void mouseReleased(MouseEvent e)   
           {
                  setBackground(Color.blue);
                  x = e.getX();
                  y = e.getY();
                  str ="Mouse Released";
                  repaint();
           }
           public void mousePressed(MouseEvent e)   
           {
               setBackground(Color.lightGray);
               x = e.getX();
               y = e.getY();
               str ="Mouse pressed";
               repaint();
          }                                                       // override the 2 abstract methods of MML
          public void mouseDragged(MouseEvent e)
          {
               setBackground(Color.magenta);
               x = e.getX(); 
               y = e.getY();
               str ="Mouse Dragged";
               repaint();
          }
          public void mouseMoved(MouseEvent e)  
          {
               setBackground(Color.yellow);
               x = e.getX();
               y = e.getY();
               str = "Mouse Moved";
               repaint();
          }
          public void paint(Graphics g)  
          {
               g.setColor(Color.blue);
               g.fillOval(x , y , 10 , 10);
               g.drawString(x +", "+ y , x , y);
               g.drawString(str , x , y -10);             // to draw the string above y coordinate
    }                              
    public static void main(String args[ ])   
    {
               new MouseXY();
    }

}

于 2013-02-20T12:55:23.877 回答