Java-a classic swing ball game

Source: Internet
Author: User

 

 

1. ball Games <br/> package teacherball; </P> <p> Import Java. AWT. color; <br/> Import Java. AWT. rectangle; <br/> Import Java. AWT. shape; <br/> Import Java. AWT. geom. ellipse2d; <br/> Import Java. util. random; </P> <p> public class ball {<br/> private Double X, Y; <br/> Public static final int x_size = 20; <br/> Public static final int y_size = 20; <br/> private int xmovedis = 10; <br/> private int ymovedis = 10; <br/> private ellipse2d shape; </P> <p> private color; <br/> Private Static random; </P> <p> static {<br/> random = new random (); <br/>}< br/> Public ball () {<br/> color = new color (random. nextint (256), <br/> random. nextint (256), random. nextint (256); <br/> xmovedis = 5 + random. nextint (5)-2; <br/> ymovedis = 5 + random. nextint (5)-2; <br/>}</P> <p> Public shape getshape () {// Method for returning the ball shape <br/> If (shape = NULL) {<br/> shape = new ellipse2d. double (X, Y, x_size, y_size); <br/>}else {<br/> shape. setframe (X, Y, x_size, y_size); <br/>}< br/> return shape; <br/>}</P> <p> Public void move (rectangle rect) {<br/> X + = xmovedis; <br/> Y + = ymovedis; <br/> // determine the boundary <br/> If (x <rect. getminx () {<br/> X = rect. getminx (); <br/> xmovedis =-xmovedis; <br/>}else if (x + x_size)> rect. getmaxx () {<br/> X = rect. getmaxx ()-x_size; <br/> xmovedis =-xmovedis; </P> <p >}< br/> If (Y <rect. getminy () {<br/> Y = rect. getminy (); <br/> ymovedis =-ymovedis; <br/>}else if (Y + y_size)> rect. getmaxy () {<br/> Y = rect. getmaxy ()-y_size; <br/> ymovedis =-ymovedis; <br/>}</P> <p> Public color getcolor () {<br/> return color; <br/>}</P> <p> Public void setcolor (color) {<br/> This. color = color; <br/>}< br/> 2. main Interface <br/> package teacherball; </P> <p> Import Java. AWT. borderlayout; <br/> Import Java. AWT. panel; <br/> Import Java. AWT. toolkit; <br/> Import Java. AWT. event. actionevent; <br/> Import Java. AWT. event. actionlistener; <br/> Import javax. swing. jbutton; <br/> Import javax. swing. jframe; <br/> Import COM. myemap. UI. face; </P> <p> public class ballframe {<br/> jbutton jbstart; <br/> jbutton jbstop; <br/> jframe JF; <br/> ballpanel bp; <br/> Public ballframe () {<br/> JF = new jframe ("Ball Game"); <br/> JF. setlayout (New borderlayout (); <br/> BP = new ballpanel (); <br/> panel buttonpanel = new Panel (); <br/> jbstart = new jbutton ("START"); <br/> jbstop = new jbutton ("stop"); <br/> buttonpanel. add (jbstart); <br/> buttonpanel. add (jbstop); </P> <p> JF. add (buttonpanel, borderlayout. south); <br/> JF. add (BP, borderlayout. center); <br/> This. actionhandler (); <br/> This. showmi (); <br/>}</P> <p> private void showmi () {<br/> JF. setsize (400,300); <br/> // JF. pack (); <br/> toolkit tool = toolkit. getdefatooltoolkit (); <br/> double X = tool. getscreensize (). getwidth (); <br/> Double Y = tool. getscreensize (). getheight (); <br/> JF. setlocation (INT) (x-jf.getWidth ()/2, (INT) (y-jf.getHeight ()/2); <br/> JF. setvisible (true); <br/> JF. setresizable (false); <br/> JF. setdefaclocloseoperation (jframe. exit_on_close); <br/>}< br/> private void actionhandler () {<br/> jbstart. addactionlistener (New actionlistener () {<br/> Public void actionreceivmed (actionevent e) {<br/> system. out. println ("START pressed"); <br/>/* 1. create a ball <br/> 2. create a ball thread <br/> 3. add ball */<br/> ball = new ball (); <br/> New ballthread (ball, BP. getbounds ()). start (); <br/> bp. addball (ball); <br/>}< br/>}); <br/> jbstop. addactionlistener (New actionlistener () {<br/> Public void actionreceivmed (actionevent e) {<br/> system. out. println ("Stop pressed"); <br/> system. exit (0); <br/>}< br/>}); <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> face. setface (); <br/> New ballframe (); <br/>}< br/> 3. ball panel <br/> package teacherball; </P> <p> Import Java. AWT. graphics; <br/> Import Java. AWT. graphics2d; <br/> Import Java. util. arraylist; <br/> Import Java. util. list; </P> <p> Import javax. swing. jpanel; </P> <p> public class ballpanel extends jpanel {// draws small balls <br/> private list <ball> bils; <br/> Public ballpanel () {<br/> bils = new arraylist <ball> (); <br/> startpanitthread (); <br/>}< br/> Public void addball (ball) {<br/> bils. add (ball); <br/>}< br/> private void startpanitthread () {<br/> New thread () {<br/> @ override <br/> Public void run () {<br/> while (true) <br/>{< br/> repaint (); <br/> try {<br/> thread. sleep (50); // 50 ms <br/>} catch (interruptedexception e) {<br/> E. printstacktrace (); <br/>}< br/> }. start (); <br/>}< br/> @ override <br/> protected void paintcomponent (Graphics g) {<br/> super. paintcomponent (g); <br/> graphics2d g2 = (graphics2d) g; <br/> for (ball: bils) {<br/> g2.setcolor (ball. getcolor (); <br/> g2.fill (ball. getshape (); <br/>}</P> <p >}< br/> 4. ball Thread class <br/> package teacherball; </P> <p> Import Java. AWT. rectangle; <br/> Import Java. util. list; </P> <p> public class ballthread extends thread {// only responsible for moving <br/> private ball; <br/> private rectangle rect; <br/> Public ballthread (ball, rectangle rect) {<br/> This. ball = ball; <br/> This. rect = rect; <br/>}< br/> @ override <br/> Public void run () {<br/> int COUNT = 0; <br/> while (count <10000) <br/> {<br/> ball. move (rect); <br/> try {<br/> thread. sleep (50); <br/>} catch (interruptedexception e) {<br/> E. printstacktrace (); <br/>}< br/> count ++; <br/>}< br/>

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.