The example in this article describes the pinball game code implemented in Java based on swing. Share it with everyone for your reference.
The main function codes are as follows:
Copy the code 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 extends 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();
}
/**
* Method to initialize the form
*/
public void initUI() {
//Set title
this.setTitle("If you are a master, stick to it for 10s");
//Set size
this.setSize(610, 635);
// Set shutdown to exit the program
this.setDefaultCloseOperation(3);
//Set to display in the middle of the screen when the form is opened
this.setLocationRelativeTo(null);
//Set the flow layout manager of the form
this.setLayout(new java.awt.FlowLayout());
//Set the form background color
this.getContentPane().setBackground(java.awt.Color.black);
//Create text label object
text_field = new javax.swing.JLabel();
javax.swing.JLabel label = new javax.swing.JLabel("Time:");
//Set the text label foreground color
lable.setForeground(java.awt.Color.red);
text_field.setForeground(java.awt.Color.red);
//Create progress bar object
pBar = new javax.swing.JProgressBar(0, 330);
//Create 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("Pause");
// Create action listeners through anonymous inner classes
java.awt.event.ActionListener button_listener = new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
String com = e.getActionCommand();
if (com.equals("pause")) {
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 < 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("Pause");
for (int i = 0; i < list.size(); i++) {
list.get(i).ContinueThread();
}
}
}
};
button.addActionListener(button_listener);
this.add(button);
this.add(lable);
this.add(pBar);
this.add(text_field);
// Go to the menu bar
JMenuBar bar = creatMenuBar();
//Set the menu bar for the form
this.setJMenuBar(bar);
//Set the visibility of the form
this.setVisible(true);
}
/**
* How to create a menu bar
*
* @return
*/
public JMenuBar creatMenuBar() {
//Create menu bar object
JMenuBar bar = new JMenuBar();
//Create menu object
JMenu menu_menu = new JMenu("menu");
JMenu difficulty_menu = new JMenu("Difficulty");
JMenu help_menu = new JMenu("Help");
//Create 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 radio options
JRadioButtonMenuItem easy_item = new JRadioButtonMenuItem(
"Simple");
JRadioButtonMenuItem middle_item = new JRadioButtonMenuItem(
"medium");
JRadioButtonMenuItem hard_item = new JRadioButtonMenuItem(
"difficulty");
//Create a button group
ButtonGroup group = new ButtonGroup();
// Add the radio button to the button group
group.add(easy_item);
group.add(middle_item);
group.add(hard_item);
// Add radio buttons to the menu
difficulty_menu.add(easy_item);
difficulty_menu.add(middle_item);
difficulty_menu.add(hard_item);
// Create action listeners through anonymous inner classes
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
command = e.getActionCommand();
// If start is selected, create the thread object
if (command.equals("start") && list.size() == 0) {
creatBall(20, 1);
}
// If the exit button is selected, exit the program
if (command.equals("Exit")) {
System.exit(0);
}
// If simple button is selected
if (command.equals("simple") && list.size() == 0) {
creatBall(20, 1);
}
// If the medium button is selected
if (command.equals("medium") && list.size() == 0) {
creatBall(50, 2);
}
if (command.equals("difficulty") && list.size() == 0) {
creatBall(40, 2);
}
if (command.equals("Game description")) {
JOptionPane.showMessageDialog(null,
"Move the mouse and use the baffle to catch the ball. If you can't catch it, you lose.../nThe game can choose difficulty, including easy, medium and hard");
}
if (command.equals("about")) {
JOptionPane
.showMessageDialog(null,
"This is a small game written in Java.../nProducer: Shen Guanjun/nTime: August 2010/nCopyright, any reproduction will be investigated!");
}
}
};
//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);
//Add the menu option object to the menu object
menu_menu.add(star_item);
menu_menu.add(exit_item);
help_menu.add(help_item);
help_menu.add(about_item);
//Add the menu object to the menu bar
bar.add(menu_menu);
bar.add(difficulty_menu);
bar.add(help_menu);
// Return the menu bar object
return bar;
}
/**
* Method to create thread object
*
* @param speed
* :speed
*
*/
public void creatBall(int speed, int num) {
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 < 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();
}
}
/**
* How to get the command
*/
public String getCommand() {
return command;
}
}
Copy the code code as follows:
package Game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
/**
* Class of mouse listener
*
* @author Administrator
*
*/
public class Listener extends MouseAdapter {
private Graphics g;
private int x = 5, y = 620;
private int width = 100, height = 10;
public Listener(Graphics g) {
this.g = g;
}
public void mouseMoved(MouseEvent e) {
//Set the 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;
}
}
The running effect is shown in the figure below:
I hope this article will be helpful to everyone’s Java programming.