我正在使用Java创建一个屏幕录像机软件。几乎80%的工作已经完成。现在我需要使用Java创建鼠标点击的可视化标记。这样我就可以在播放视频中看到鼠标点击的位置。我怎样才能做到这一点?
有没有人有任何代码示例?
答案 0 :(得分: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();
}
}