我正在使用Java創建屏幕錄像軟件。近80%的工作已經完成。現在我需要使用Java創建鼠標點擊的可視化標記。這樣我可以在回放視頻中看到鼠標點擊的位置。我怎樣才能做到這一點?使用Java的鼠標點擊的視覺標記
有沒有人有任何代碼示例?
我正在使用Java創建屏幕錄像軟件。近80%的工作已經完成。現在我需要使用Java創建鼠標點擊的可視化標記。這樣我可以在回放視頻中看到鼠標點擊的位置。我怎樣才能做到這一點?使用Java的鼠標點擊的視覺標記
有沒有人有任何代碼示例?
很簡單。使用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-20 23:37:57
非常感謝您的回覆。但是你的解決方案是特定於應用您提供的代碼僅適用於JFrame(在應用程序空間內)。但我不想這樣做。當您使用「faststone capture」或「snagit」錄製桌面屏幕時,則不會僅與該應用程序交互。你與整個桌面進行交互。所以我想在應用程序空間之外監視鼠標。有人已經在這裏發表評論說,Java沒有原生的方式來監視應用程序空間之外的鼠標。所以你知道任何其他方式來做我想要的? – 2013-02-20 23:50:18
更新您的問題以反映此意圖。 – trashgod 2013-02-21 00:21:21
這將取決於你如何錄製屏幕了很多。對於Java而言,在應用程序空間之外沒有本地監控鼠標的方式,所以您需要有一個JNI解決方案來監控鼠標點擊。 – MadProgrammer 2013-02-20 00:45:45
這聽起來像一個有趣的項目,它使用'List'來記錄MouseAdapter中的點擊'和'javax.swing.Timer'進行播放。請修改您的問題以包含顯示您遇到的任何問題的[sscce](http://sscce.org/)。 –
trashgod
2013-02-20 02:26:11
我該如何使用JNI或JNA來做到這一點?請告訴我,如果你知道任何方式做到這一點? @MadProgrammer – 2013-02-21 16:07:04