Java from getting started to mastering 15-directional key control ball instances

Source: Internet
Author: User

Read the code as the main:

Importjavax.swing.*;Importjava.awt.*;Importjava.awt.event.KeyEvent;ImportJava.awt.event.KeyListener;/*** Created by Administrator on 2015-10-15.*/ Public classArrawkeyextendsJFrame {circlepanel Circlepanel=NULL;  Public Static voidMain (string[] args) {Arrawkey Arrawkey=NewArrawkey (); }    //constructor Function     PublicArrawkey () {Circlepanel=NewCirclepanel ();  This. Add (Circlepanel);  This. Addkeylistener (Circlepanel);  This. SetSize (640, 480);  This. Settitle ("Key Control Ball Instance");  This. Setlocationrelativeto (NULL);  This. Setdefaultcloseoperation (Jframe.exit_on_close);  This. setvisible (true); }}//Define panelclassCirclepanelextendsJPanelImplementsKeyListener {intx = 10; inty = 10;  Public voidPaint (Graphics graphics) {Super. Paint (graphics); Graphics.filloval (x, Y,10, 10); } @Override Public voidkeytyped (KeyEvent e) {} @Override Public voidkeypressed (KeyEvent e) {Switch(E.getkeycode ()) { Casekeyevent.vk_down:y+ = 10;  Break;  Casekeyevent.vk_up:y-= 10;  Break;  Casekeyevent.vk_right:x+ = 10;  Break;  Casekeyevent.vk_left:x-= 10;  Break; }         This. repaint (); } @Override Public voidkeyreleased (KeyEvent e) {}}

Java from getting started to mastering 15-directional key control ball instances

Related Article

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.