The code is as follows
<font size= "3" >package game;import java.awt.graphics;import java.awt.image;import javax.swing.JPanel;public Class Backgroundpanel extends JPanel { private static final long serialversionuid = 1L; Private image image;//background picture public Backgroundpanel () { setopaque (false); SetLayout (null); } public void setimage (image image) { this.image = image; } /** * Draw background * /protected void paintcomponent (Graphics g) { if (image = null) { //Picture width int width = getwidth (); Picture height int height = getheight (); Draw a picture g.drawimage (image, 0, 0, width, height, this); } Super.paintcomponent (g); }} </font>
<font size= "3" >package game;import java.awt.container;import java.awt.event.*;import javax.swing.*;p ublic Class Birdlabel extends JLabel implements Runnable {private static final long serialversionuid = 1L; Randomly generates a thread's sleep time, which controls the bird movement speed private int sleeptime = (int) (Math.random () * 300) + 5; private int y = 100; Private thread thread;//The thread as a member variable private Container parent; private int score = 15;//The score/** * Construction method of the class character */Public Birdlabel () {Super ( ); Create a Bird Icon object ImageIcon icon = new ImageIcon (GetClass (). GetResource ("Bird.gif")); SetIcon (icon);//Set Control icon Addmouselistener (new MouseAction ());//Add mouse event Listener//Add control Event Listener Addcomponentlistener (New Componentaction ()); thread = new thread (this);//Create Thread object}/** * control's control event listener */private Final class Compone Ntaction EXtends componentadapter {public void componentresized (final componentevent e) {THR Ead.start ();//Thread Start}}/** * Control mouse Event listener */private Final class Mousea Ction extends Mouseadapter {public void mousepressed (final MouseEvent e) {if (! Mainframe.readyammo ())//If the bullets are not ready return;//do nothing Mainframe.useammo ();//Consume Bullets appscore ();//Add points destory ();//Destroy This component}} public void Run () {parent = null; int width = 0; try {while (width <= 0 | | | parent = = NULL) {if (parent = = NULL) {parent = GetParent ();//Gets the parents container} else { width = Parent.getwidth ();//Gets the width of the parent container} thread.sleep (10); } for (int i = width; i > 0 && parent = null; I-= 8) { SetLocation (i, y);//move the component position from right to left Thread.Sleep (sleeptime);//Sleep for a moment} } catch (Interruptedexception e) {e.printstacktrace (); if (parent! = null) {Mainframe.appscore (-score * 10);//natural destruction will be deducted} Destory ();//move finished, destroy this component}/** * method to remove this component from the container */public void destory () {if (parent = = null) return; Parent.remove (this);//Remove the gradual parent.repaint () from the parent container; parent = NULL; Terminate thread loop through this statement}/** * Add Points method */private void Appscore () { System.out.println ("Bird is hit"); Mainframe.appscore (15); }}</font>
Detailed Description: http://java.662p.com/thread-3575-1-1.html
Java Swing hunting shooting game source