Java Pinball Game code _java based on swing implementation

Source: Internet
Author: User


This example describes the Java Pinball game code based on swing implementation. Share to everyone for your reference.



The main functional code is as follows:





code as follows:


Package Game;








Import Java.awt.Graphics;
Import Java.awt.Insets;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import java.util.ArrayList;
Import Java.util.Random;





Import Javax.swing.ButtonGroup;
Import Javax.swing.JMenu;
Import Javax.swing.JMenuBar;
Import Javax.swing.JMenuItem;
Import Javax.swing.JOptionPane;
Import Javax.swing.JRadioButtonMenuItem;





/**
 * Game interface class
 *
 * @author Administrator
 *
 */
public class Game ex Tends Javax.swing.JFrame {
        private static final long Serialversionuid = 1L;
        private static Game UI;
        private Graphics g;
        private Listener ml;
        public Javax.swing.JLabel Text_field;
        public Javax.swing.JProgressBar pbar;
        Private String command;
        public arraylist<mythread> list = new arraylist<mythread> ();
        private Javax.swing.JToggleButton button;





public static void Main (String args[]) {
UI = new Game ();
Ui.initui ();
}



/**


* How to initialize the form


*/


public void Initui () {


Set Title


This.settitle ("is a master on the insistence on 10s");


Set size


This.setsize (610, 635);


Set Shutdown is Exit program


This.setdefaultcloseoperation (3);


To display in the middle of the screen when you open a form


This.setlocationrelativeto (NULL);


To set the streaming layout manager for a form


This.setlayout (New Java.awt.FlowLayout ());


Set the form background color


This.getcontentpane (). SetBackground (Java.awt.Color.black);


Create a text Label object


Text_field = new Javax.swing.JLabel ();


Javax.swing.JLabel lable = new Javax.swing.JLabel ("Time:");


Set text label foreground color


Lable.setforeground (java.awt.Color.red);


Text_field.setforeground (java.awt.Color.red);

Create a progress bar object


Pbar = new Javax.swing.JProgressBar (0, 330);


Create a Button object


button = new Javax.swing.JToggleButton ();


Button.setmargin (New Insets (0, 0, 0, 0));


Button.seticon (New Javax.swing.ImageIcon ("Images/pause.gif"));


Button.setactioncommand ("suspend");


Create an action listener from an anonymous inner class


Java.awt.event.ActionListener Button_listener = new Java.awt.event.ActionListener () {



public void actionperformed (ActionEvent e) {
String com = E.getactioncommand ();



if (Com.equals ("paused")) {


Button.setmargin (New Insets (0, 0, 0, 0));


Button.seticon (New Javax.swing.ImageIcon ("Images/start.gif"));


Button.setactioncommand ("continue");


for (int i = 0; i &lt; list.size (); i++) {


List.get (i). Pausethread ();



}





}
if (Com.equals ("continue")) {





                                          Button.setmargin (New Insets (0, 0, 0, 0));
                                          Button.seticon (New Javax.swing.ImageIcon ("Images/pause.gif"));
                                          Button.setactioncommand ("paused");





                                          for (int i = 0; i < list.size (); i++) {
        & nbsp;                                         List.get (i). Continuethread ();
                                         }





}





}





};
Button.addactionlistener (Button_listener);



This.add (button);


This.add (lable);


This.add (Pbar);


This.add (Text_field);


to the menu bar


JMenuBar bar = Creatmenubar ();


Set a menu bar for a form


This.setjmenubar (bar);


Set the visibility of a form


This.setvisible (TRUE);


}

/**


* How to create a menu bar


*


* @return


*/


Public JMenuBar Creatmenubar () {


Create a menu bar object


JMenuBar bar = new JMenuBar ();


Create a Menu Object


JMenu menu_menu = new JMenu ("menu");


JMenu difficulty_menu = new JMenu ("difficulty");


JMenu help_menu = new JMenu ("help");


Create a menu option object


JMenuItem Star_item = new JMenuItem ("Start");


JMenuItem Exit_item = new JMenuItem ("Exit");


JMenuItem Help_item = new JMenuItem ("Game description");


JMenuItem About_item = new JMenuItem ("about");


Create a radio option


JRadioButtonMenuItem Easy_item = new JRadioButtonMenuItem (


"Simple");


JRadioButtonMenuItem Middle_item = new JRadioButtonMenuItem (


"Medium");


JRadioButtonMenuItem Hard_item = new JRadioButtonMenuItem (


"Difficulties");


Create a group of buttons


Buttongroup Group = new Buttongroup ();


To add a radio button to a group of buttons


Group.add (Easy_item);


Group.add (Middle_item);


Group.add (Hard_item);


To add a radio button to a menu


Difficulty_menu.add (Easy_item);


Difficulty_menu.add (Middle_item);


Difficulty_menu.add (Hard_item);


Create an action listener from an anonymous inner class


ActionListener listener = new ActionListener () {


public void actionperformed (ActionEvent e) {


Command = E.getactioncommand ();


If you choose to start, the thread object is created


if (Command.equals ("Start") &amp;&amp; list.size () = = 0) {


Creatball (20, 1);

}


Exit the program if you choose to exit the button


if (Command.equals ("Exit")) {


System.exit (0);


}



If you select a simple button
if (Command.equals ("simple") && list.size () = = 0) {





Creatball (20, 1);
}





If you select a Medium button
if (Command.equals ("medium") && list.size () = = 0) {
Creatball (50, 2);



}


if (Command.equals ("difficult") &amp;&amp; list.size () = = 0) {


Creatball (40, 2);


}


if (Command.equals ("Game description")) {


Joptionpane.showmessagedialog (NULL,


"Move the mouse, catch the ball with the baffle, if you can't catch, even lose ... \ n game can choose difficulty, including simple, medium, difficult");


}


if (Command.equals ("about")) {


Joptionpane


. Showmessagedialog (NULL,


"This is a small game written in Java ... \ n producer: Shen \ r \ n: August 2010 \ r \ n All rights reserved, pirated! ");


}


}


};


Add Action Listener


Star_item.addactionlistener (listener);


Exit_item.addactionlistener (listener);


Easy_item.addactionlistener (listener);


Middle_item.addactionlistener (listener);


Hard_item.addactionlistener (listener);


Help_item.addactionlistener (listener);


About_item.addactionlistener (listener);

To add a menu option object to a Menu object


Menu_menu.add (Star_item);


Menu_menu.add (Exit_item);


Help_menu.add (Help_item);


Help_menu.add (About_item);


Add a Menu object to the menu bar


Bar.add (Menu_menu);


Bar.add (Difficulty_menu);


Bar.add (Help_menu);


Return Menu Bar Object


return bar;


}



       /**
         * Ways to create thread objects
         *
         * Param speed
         *            : Speed
         *
          */
        public void creatball (int speed, int num) { br>                 Random ran = new Random ();





if (ml = = null) {
g = Ui.getgraphics ();
ml = new Listener (g);
Ui.addmouselistener (ML);
Ui.addmousemotionlistener (ML);



}


for (int i = 0; i &lt; num; i++) {


int x = ran.nextint (600) + 10;


int y = ran.nextint (300) + 100;


Mythread th = new Mythread (g, ML, UI, x, y, speed);


List.add (TH);


Th.start ();


}



}





/**
* The way to get the order
*/
Public String GetCommand () {
return command;
}





}















Copy Code code as follows:



Package Game;






Import Java.awt.Color;


Import Java.awt.Graphics;


Import Java.awt.event.MouseAdapter;


Import java.awt.event.MouseEvent;


/**


* The class of the mouse listener


*


* @author Administrator


*


*/


public class Listener extends Mouseadapter {


Private Graphics G;


private int x = 5, y = 620;


private int width = +, height = 10;





Public Listener (Graphics g) {


THIS.G = g;


}





public void mousemoved (MouseEvent e) {


Set Canvas object Color


Synchronized (THIS.G) {


G.setcolor (Color.Black);


G.fillrect (x, y, width, height);


x = E.getx ();


G.setcolor (Java.awt.Color.green);


G.fillrect (x, y, width, height);


}


}





/**


* Method to get X


*


* @return: X


*/


public int GetX () {


return x;


}





}








I hope this article will help you with your Java programming.


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.