On-Machine topic (beginner)-Mouse control Ball (Java)

Source: Internet
Author: User

This section realizes the small ball follows the mouse movement function, here to realize the mouse movement monitoring, the code is as follows:

Import Java.awt.color;import java.awt.frame;import Java.awt.graphics;import Java.awt.panel;import Java.awt.event.mouseevent;import Java.awt.event.mousemotionlistener;public class Keylistenerdemo {public static void Main (string[] args) {Frame frame = new Frame (frame.setsize); Frame.setbackground (Color.Black); Mypanel Mypanel = new Mypanel (); Frame.add (Mypanel); Frame.addmousemotionlistener (Mypanel); Mypanel.addmousemotionlistener (Mypanel); Frame.show ();}}  Class Mypanel extends Panel implements Mousemotionlistener {int x = 150;int y =; @Overridepublic void Paint (Graphics g) {G.setcolor (Color.White); G.filloval (x, Y, 20, 20);} @Overridepublic void mousedragged (MouseEvent e) {} @Overridepublic void mousemoved (MouseEvent e) {x=e.getx (); Y=e.gety () ; repaint ();}}

The code runs the following example:


On-Machine topic (beginner)-Mouse control Ball (Java)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.