📄 gamepanel.java
字号:
//********************************************************************
// GamePanel.java Author: yj_hu
//
// Demonstrates a basic componenet hierarchy.
// It conclude subpanels which regards as 9 holes in the game,
// then add some labels and buttons to make the applications more friendly,
// and after that add the mouselistener and the timer function.
//********************************************************************
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;//import all the classes I need in this program
public class GamePanel extends JPanel
{
private Circle circle1;
private Circle circle2;
private Circle circle3;
private Circle circle4;
private Circle circle5;
private Circle circle6;
private Circle circle7;
private Circle circle8;
private Circle circle9;//create 9 objects of the Circle class
private final int WIDTH = 400, HEIGHT = 300;//set the width and height of the panel
private final int DELAY = 1000, IMAGE_SIZE = 66;//set the delay time and the size of the image
private ImageIcon image;//create the object of the picture
private Timer timer;//create Timer object
private int x, y;//normal variables
private JButton start;//create button object
private Point point1=null;//initial the point as null
private int count;//declare a variable
private int lose;//decleare a variable
private JLabel label1;
private JLabel label2;
private JLabel label3;//create 3 label objects
//-----------------------------------------------------------------
// Constructor: Creates nine Circle objects also the size and color of the background
//-----------------------------------------------------------------
public GamePanel()
{
circle1 = new Circle (100, Color.white, 10,10);
circle2 = new Circle (100, Color.white, 110, 10);
circle3 = new Circle (100, Color.white, 210, 10);
circle4 = new Circle (100, Color.white, 10, 110);
circle5 = new Circle (100, Color.white, 110, 110);
circle6 = new Circle (100, Color.white, 210, 110);
circle7 = new Circle (100, Color.white, 10, 210);
circle8 = new Circle (100, Color.white, 110, 210);
circle9 = new Circle (100, Color.white, 210, 210);//define the radium and color and position of each circle
setPreferredSize (new Dimension(400, 400));//set the fitful size for the panel
setBackground (Color.pink);//set the color of panel background
timer = new Timer(DELAY, new GameListener());//create new object of timer
image = new ImageIcon ("picture.gif");//reference the picture I will use in the program
start=new JButton("Start");//create the Start button
start.addActionListener(new GameListener());//add the Gamelistener
GameListener listener = new GameListener();//create new listener
addMouseListener (listener);//add the mouse listener
addMouseMotionListener (listener);//add the mouse motion listener
lose=0;
count=0;//initialization of the two variables
label1=new JLabel ("you have got "+count+" mice!");
label2=new JLabel("you have lost "+lose+ "mice!");
//fill the labels with the words that will appear on the screen
add(label1);
add(label2);//add the two labels
add(start);//add the start button
}
//-----------------------------------------------------------------
// Draws this panel by requesting that each circle draw itself.
//-----------------------------------------------------------------
public void paintComponent (Graphics page)
{
super.paintComponent(page);//inherite the super class
circle1.draw(page);
circle2.draw(page);
circle3.draw(page);
circle4.draw(page);
circle5.draw(page);
circle6.draw(page);
circle7.draw(page);
circle8.draw(page);
circle9.draw(page);//draw 9 circles
image.paintIcon (this, page, x, y);//make sure the position of the image
}
//***********************************************************************************
// Represents the action listener for the timer,mouse event and label and also button
//*****************************************************************
private class GameListener implements ActionListener,MouseListener,MouseMotionListener
{
//--------------------------------------------------------------
// Updates the position of the image whenever the timer fires an action event.
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
timer.start();//start the game by using timer class
Random generator=new Random();//create a generator to get random numbers
int number;//create a random number to locate a position of the picture
number=generator.nextInt(9)+1;//create random numbers from 1 to 9
//the following steps are at the purpose of
//matching each random number with the position we had on the screen
//that is to say, if the generator has the number of 1,
//the image will appear on the first circle;
//if it has the number of 2,
//the image will appear on the second circle and so on;
//the position has to be confirm by the number of coordinate x and y
if(number==1)
{
x=30;
y=30;
}
else
if(number==2)
{
x=130;
y=30;
}
else
if(number==3)
{
x=230;
y=30;
}
else
if(number==4)
{
x=30;
y=130;
}
else
if(number==5)
{
x=130;
y=130;
}
else
if(number==6)
{
x=230;
y=130;
}
else
if(number==7)
{
x=30;
y=230;
}
else
if(number==8){
x=130;
y=230;
}
else
{
x=230;
y=230;
}
repaint();
//after the match I have to repaint the image
//in order to make it appear one by oneon the screen
}
//--------------------------------------------------------------
// Captures the initial position at which the mouse button is
// pressed.
//--------------------------------------------------------------
public void mousePressed (MouseEvent event)//the definition of the mousePressed event method
{
point1 = event.getPoint();//get the coordinate of the point of x and y
if(point1.x>x&&point1.x<x+66&&point1.y>y&&point1.y<y+66)
//determine whether the mousepress is in the range of the picture
{
count+=1;
//if the mouse press the picture exactly ,I record the number of success mousepress
label1.setText("you have got "+count+" mice!");
//set some words on label1 and display the words on the screen
}
else
//if the mouse fails to press on the appeared picture
{
lose+=1;
//I also record the number of failure mousepress
label2.setText("you have lost "+lose+" mice!");
// set some words on label2 and display the words on the screen
//these words just help the player to understand the game rules
if(lose==5)//if you fail 5 times,you lose the game and the timer class stop working
{
timer.stop();//stop
label3=new JLabel();//another object of label
add(label3);//add this label
label3.setText("you failed...please press start to replay the game");
//when you fail 5 times, the words will apprea on the screen to
//tell that you failed and you have to press the button to start again if you like
}
}
}
//--------------------------------------------------------------
// Provide empty definitions for unused event methods.
//--------------------------------------------------------------
public void mouseDragged (MouseEvent event){}
public void mouseClicked (MouseEvent event) {}
public void mouseReleased (MouseEvent event) {}
public void mouseEntered (MouseEvent event) {}
public void mouseExited (MouseEvent event) {}
public void mouseMoved (MouseEvent event) {}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -