Thursday, 15 January 2015

swing - Visual marker of mouse clicks using Java -



swing - Visual marker of mouse clicks using Java -

i creating screen recorder software using java. 80% work has been completed. need create visual marker of mouse clicks using java. can see in playback video mouse has been clicked. how can that?

does have code example?

very simple. read point mouse clicked user getx() , gety() methods of mouselistener. @ point, draw oval drawoval() methods of java.awt.graphics class. seek next code sure solves problem.

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 listeners frame addmousemotionlistener(this); } // override 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 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); // draw string above y coordinate } public static void main(string args[ ]) { new mousexy(); }

}

java swing awt mouse screenshot

No comments:

Post a Comment