JAVA multithreading making big ball eat small ball one, realize the automatic generation of balls and the consumption model of sports production

Source: Internet
Author: User

/* The code used in the article is only a part of the source can be contacted by email me [email protected]*/

A few days ago with multi-threaded to create a small ball and move, think of feeding frenzy, then whim to write a big ball to eat ball. First of all, the first step is to get the interface done first.

1  Public classBalluiextendsJPanel {2 3     PrivateArraylist<ball> Li =NewArraylist<ball>();4 5      Public Static voidMain (string[] args) {6Ballui bu =NewBallui ();7 Bu. UI ();8     }9 Ten      Public voidUI () { OneJFrame JF =NewJFrame (); AJf.setlocationrelativeto (NULL); -Jf.setresizable (false); -Jf.setsize (700, 700); the          This. SetSize (Jf.getwidth (), Jf.getheight ()); -Jf.settitle ("Ball eating ball"); -  -          This. Setpreferredsize (NewDimension (Jf.getwidth (), Jf.getheight ())); +Jf.add ( This); -  +          A  atJf.setvisible (true); -     } -      -  -}

What do you want to do when the interface is written? Since it is a big ball to eat small ball also use thread, that certainly need a queue, then? If you want to queue, what's in the queue? Must keep the small ball, that naturally wants to create a little ball, that ball has what attribute? coordinate, move speed, size, color, what other parameters do you need to pass in addition to these when creating? Since the color is passed, it must be a small ball, where do we draw? Is it called method or thread to get the ball properties and then draw? I think it would be better to invoke the method, so we're going to pass in a canvas here. Also in the back we need to detect if the ball is colliding, so we need to pass in a queue. The following is the construction of ball class

 Public classBall {Private intsize; Private intx; Private inty; Private intSpeedx; Private intSpeedY; PrivateColor C; PrivateJPanel JF; PrivateArraylist<ball>Li;  PublicBall (intSizeintXintYintSpeedx,intSpeedY, Color C, JPanel JF, arraylist<ball>Li) { This. Size =size;  This. x =x;  This. y =y;  This. Speedx =Speedx;  This. SpeedY =SpeedY;  This. C =C;  This. JF =JF;  This. Li =Li; }//Draw the ball Public voidDraw (Graphics g) {}//move ball Public voidmove () {}//clear the last traces left Public voidClear (Graphics g) {}//Collision detection Public voidCrash (Graphics g) {}}

After creating the small ball, we need to thread to create the ball, change the ball, there is also a problem, we are using one thread or two or more? This way, if only one thread, then we have to change the ball in the creation of a small ball, if the number of smaller balls is not a problem, but when the number of balls, every convenience will be used for a period of time, then there will certainly be a delay, so we need to divide this place into two threads, a thread to create the ball, A thread changes the ball. In fact, the producer consumer model is used: The producer is responsible for producing the data and depositing it into the buffer, and the consumer pulls the data out of the buffer and processes and outputs it.

1  Public classThreadballextendsThread {2 3     PrivateArraylist<ball>Li;4     PrivateJPanel JP;5     PrivateGraphics G;6 7      PublicThreadball (arraylist<ball>Li,jpanel JP) {8          This. Li =Li;9          This. jp=JP;Ten     } One  A      Public voidrun () { -g=jp.getgraphics (); -          while(true) { the              for(intI=0;i<li.size (); i++) { -Ball ball=Li.get (i); - Ball.clear (g); - Ball.crash (g); + Ball.move (); - Ball.draw (g); +             } A             Try { atThread.Sleep (100); -}Catch(interruptedexception e) { - e.printstacktrace (); -             } -         } -     } in      -}

 Public classBalllisextendsThread {PrivateArraylist<ball> Li =NewArraylist<ball>(); PrivateJPanel JP;  PublicBalllis (arraylist<ball>Li,jpanel JP) {         This. Li =Li;  This. JP =JP; }     Public voidrun () { while(true) {            intSize =NewRandom (). Nextint (30) + 10; intx =NewRandom (). Nextint (Jp.getwidth ()); inty = 0; intSpeedx =NewRandom (). Nextint (10) + 10; intSpeedY =NewRandom (). Nextint (10) + 5; Color C=NewColor (NewRandom (). Nextint (256),NewRandom (). Nextint (256),NewRandom (). Nextint (256)); Ball BL=NewBall (Size,x,y,speedx,speedy,c,jp,li);                        Li.add (BL); Try{Thread.Sleep (1000); } Catch(interruptedexception e) {e.printstacktrace (); }                    }    }}

After creating the thread, you must create the object in the Ballui class.

        New Balllis (Li,this);        Ball.start ();        Threadball TB=new Threadball (Li,this);        Tb.start ();

Then, the rest of the task is to achieve the small ball in the method, it is left to you to achieve.

JAVA multithreading making big ball eat small ball one, realize the automatic generation of balls and the consumption model of sports production

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.