Java Thread Aircraft War production

Source: Internet
Author: User

ImportJava.awt.Graphics;Importjava.util.ArrayList;ImportJavax.swing.JFrame;ImportJavax.swing.JPanel; Public classPlanemainextendsJPanel { Public Static voidMain (string[] args) {NewPlanemain (); }    PrivateArraylist<view>list;  PublicPlanemain () {list=NewArraylist<view>(); View background=NewView ("Background.jpg", 0,-60, 700, 460, 2, This);        List.add (background);    Initui (); }    Private voidInitui () {JFrame frame=NewJFrame ("Aircraft war"); Frame.setsize (700, 400); Frame.setdefaultcloseoperation (3); Frame.setlocationrelativeto (NULL); Frame.setresizable (false); Frame.add ( This); Frame.setvisible (true); AddListener Al=NewAddListener ( This, list);  This. Addmouselistener (AL); Thread T=NewThread (AL); T.start ();//Start Thread    }    /*** Rewrite the redraw method of JPanel*/     Public voidPaint (Graphics g) {Super. Paint (g);  for(inti = 0; I < list.size (); i++) {View v=List.get (i); G.drawimage (V.getbackground (), V.getx (), V.gety (), V.getwidth (), V.getheight (), This); }    }}
ImportJava.awt.event.MouseAdapter;Importjava.awt.event.MouseEvent;Importjava.util.ArrayList;ImportJavax.swing.JPanel; Public classAddListenerextendsMouseadapterImplementsRunnable {Private intCount = 0; PrivateJPanel Panel; PrivateArraylist<view>list;  PublicAddListener (JPanel panel, arraylist<view>list) {         This. Panel =Panel;  This. List =list; }     Public voidmousereleased (MouseEvent e) {if(count% 2 = = 0) {View plane=NewView ("Plane.jpg", E.getx (), E.gety (), 50, 50, 3, panel);            List.add (plane); Count++; } Else{View Bullet=NewView ("Bullet.png", E.getx (), E.gety (), 10, 20, 5, panel);            List.add (bullet); Count++; }    }     Public voidrun () { while(true) {             for(inti = 0; I < list.size (); i++) {View v=List.get (i);                V.move (); if(i!=0) v.collisions (list);            } panel.repaint (); Try{Thread.Sleep (100); } Catch(interruptedexception e) {e.printstacktrace (); }        }    }}
ImportJava.awt.Image;Importjava.util.ArrayList;ImportJavax.swing.ImageIcon;ImportJavax.swing.JPanel; Public classView {PrivateImage background; Private intx = 0, y =-60, Movey, width, height; PrivateJPanel Panel; PrivateString ImageName; /*** Construction Method * *@parambackground objects with background images *@paramx start x coordinate *@paramy start y coordinate*/     PublicView (String ImageName,intXintYintWidthintheight,intMovey, JPanel Panel) {         This. ImageName =ImageName;  This. Background =NewImageIcon ( This. GetClass (). GetResource (ImageName)). GetImage ();  This. x =x;  This. y =y;  This. width =width;  This. Height =height;  This. Movey =Movey;  This. Panel =Panel; }     Public intgetwidth () {returnwidth; }     Public voidSetWidth (intwidth) {         This. width =width; }     Public intgetheight () {returnheight; }     Public voidSetHeight (intheight) {         This. Height =height; }     PublicImage Getbackground () {returnbackground; }     Public voidsetbackground (Image background) { This. Background =background; }     Public intGetX () {returnx; }     Public voidSetX (intx) { This. x =x; }     Public intGetY () {returny; }     Public voidSety (inty) { This. y =y; }     Public intGetmovey () {returnMovey; }     Public voidSetmovey (intMovey) {         This. Movey =Movey; }     PublicJPanel Getpanel () {returnPanel; }     Public voidSetpanel (JPanel panel) { This. Panel =Panel; }     Public voidMove () {if(Imagename.equals ("Background.jpg") ) {y+=Movey; if(y = = 0) y=-60; } Else if(Imagename.equals ("Bullet.png") ) {y+=Movey; if(Y >= 400) y= 0; } Else if(Imagename.equals ("Plane.jpg") ) {y-=Movey; if(Y <= 0) y= 400; }    }    /*** Collision Method*/     Public voidCollisions (arraylist<view>list) {         for(inti = 1; I < list.size (); i++) {View v=List.get (i); if( This!=v) {DoubleDistance = Math.sqrt (( This. x-v.x) * ( This. x-v.x)+ Math.pow ( This. Y-V.Y, 2)); if(Distance <= This. Height +v.height) {System.out.println (V.imagename+ "and" + This. ImageName+ "A collision occurred"); }            }        }    }}

Java Thread Aircraft War 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.