有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用Java摆动鼠标点击的可视标记

我正在用Java创建一个屏幕录制软件。几乎80%的工作已经完成。现在我需要使用Java创建鼠标点击的视觉标记。这样我就可以在回放视频中看到鼠标被点击的位置。我该怎么做

有人有代码示例吗


共 (1) 个答案

  1. # 1 楼答案

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

    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();
        }
    

    }