随便做了个小游戏,很简单,问题可能也有蛮多。主要是学习一些图新界面,和线程的的知识,请多指教,大家可以继续改进
package HitMouse;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Time;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import javax.swing.border.BevelBorder;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
public class HitMouse extends JFrame implements ActionListener, MouseListener {
private static final JLabel gradeLabel = null;
String dir=”./images/”;
JLabel jlbMouse;
Timer time;//定时器
Random orandom;//随机数
int delay=1000;
int grade=0,timeHit=0,timeshow=0;
JLabel labelGree;
JLabel labelShow;
JLabel labelHit;
boolean isHit=false;
private int timeShow;
public HitMouse() {
//主框架
this.setTitle(“打地鼠游戏”);
this.setLocation(200, 200);
this.setSize(438, 375);
this.setResizable(false);
//this.getContentPane().setLayout(null);//与面板显示冲突
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
setBak();//背景函数
//设置光标
Toolkit tk=Toolkit.getDefaultToolkit();//返回一个Tookit对象
Image myImage=tk.createImage(dir+"chui1.png");
Cursor myCursor=tk.createCustomCursor(myImage, new Point(10, 10), "XXX");
//加入光标
mySetCursor(1);
//加入地鼠图片
ImageIcon lbImage=new ImageIcon(dir+"dishu.png");
jlbMouse=new JLabel(lbImage);
jlbMouse.setLocation(100, 100);
jlbMouse.setSize(80, 80);
this.getContentPane().add(jlbMouse);
jlbMouse.addMouseListener(this);
jlbMouse.setVisible(false);
time =new Timer(delay, this);
orandom=new Random();
time.start();
//用一个面板 来加 出现次数 当前等级 打击次数等···
JPanel jpanel=new JPanel();
jpanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
jpanel.setBounds(0, 0, 438,375);
jpanel.setOpaque(false);
this.getContentPane().add(jpanel);
//出现次数
labelShow = new JLabel("0",new ImageIcon(dir+"chuxiancishu.png"),SwingConstants.CENTER);
labelShow.setFont(new Font("幼圆",Font.BOLD,20));
labelShow.setSize(146, 40);
jpanel.add(labelShow);
//打击次数
labelHit = new JLabel("0",new ImageIcon(dir+"dazhongcishu.png"),SwingConstants.CENTER);
labelHit.setFont(new Font("幼圆",Font.BOLD,20));
labelHit.setSize(146, 40);
jpanel.add(labelHit);
labelGree= new JLabel("0",new ImageIcon(dir+"dangqiandengji.png"),SwingConstants.CENTER);
labelGree.setFont(new Font("幼圆",Font.BOLD,20));
labelGree.setSize(146, 40);
jpanel.add(labelGree);
this.setVisible(true);
}
private void setBak() {
((JPanel)(this.getContentPane())).setOpaque(false);//设置透明度
ImageIcon bgimage=new ImageIcon(dir+"beijing.jpg");
JLabel bglabel=new JLabel(bgimage);
bglabel.setBounds(0, 0, bgimage.getIconWidth(), bgimage.getIconHeight());
//this.add(bglabel);//这样不行要分成来显示图片
this.getLayeredPane().add(bglabel, new Integer(Integer.MIN_VALUE));//设置权限,
//public JLayeredPane getLayeredPane()返回此窗体的 layeredPane 对象。 允许分层
}
public static void main(String[] args) {
HitMouse hitmouse=new HitMouse();
}
@Override
public void actionPerformed(ActionEvent e) {
int ran = orandom.nextInt(9);
switch(ran){
case 0:
jlbMouse.setLocation(56, 63);break;
case 1:
jlbMouse.setLocation(322, 204);break;
case 2:
jlbMouse.setLocation(185, 204);break;
case 3:
jlbMouse.setLocation(48, 203);break;
case 4:
jlbMouse.setLocation(298, 133);break;
case 5:
jlbMouse.setLocation(162, 133);break;
case 6:
jlbMouse.setLocation(22, 133);break;
case 7:
jlbMouse.setLocation(311, 63);break;
case 8:
jlbMouse.setLocation(186, 63);break;
}
timeshow++;
labelShow.setText(""+timeshow);
jlbMouse.setVisible(true);
ImageIcon imageIconDishu = new ImageIcon(dir+"dishu.png");
jlbMouse.setIcon(imageIconDishu);
isHit=false;
//游戏玩输的处理
if(timeshow>15){
time.stop();
int a = JOptionPane.showConfirmDialog(this, "很遗憾,您输了,不服,重来吗","您输了!!!",JOptionPane.YES_NO_OPTION);
if(a==JOptionPane.YES_OPTION){
mySetCursor(1);
delay=1000;
grade = 1;
timeshow=0;
timeHit=0;
gradeLabel.setText(""+grade);
labelShow.setText(""+timeshow);
labelHit.setText(""+timeHit);
time.start();
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
//设置光标
mySetCursor(2);
if(e.getSource()==jlbMouse&& !isHit){
isHit=true;
timeHit++;
labelHit.setText(""+timeHit);
ImageIcon datouimage=new ImageIcon(dir+"datou.png");
jlbMouse.setIcon(datouimage);//换图片 接口
this.setVisible(true);}
//游戏规则
if(timeHit>8){
delay-=500;
if(delay<0){
jlbMouse.setVisible(false);
time.stop();
int a = JOptionPane.showConfirmDialog(this, "您打通关了,要重新来吗?","恭喜!!!",JOptionPane.YES_NO_OPTION);
if(a==JOptionPane.YES_OPTION){
grade = 0;
delay=1000;
}else{
System.exit(0);
}
}
grade++;
timeHit=0;
timeshow=0;
time.stop();
jlbMouse.setVisible(false);
mySetCursor(1);//锤子复位
JOptionPane.showConfirmDialog(this,"进入第"+grade+"关 加油!","升级",JOptionPane.CLOSED_OPTION);
labelShow.setText(""+timeshow);
labelHit.setText(""+timeHit);
labelGree.setText(""+grade);
time.start();
}
}
@Override
public void mouseReleased(MouseEvent e) {
mySetCursor(1);
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
private void mySetCursor(int i) {
Toolkit tk = Toolkit.getDefaultToolkit();
Image image;
if(i==1){
image= tk.createImage(dir+"chui1.png");
}else{
image= tk.createImage(dir+"chui2.png");
}
Cursor myCursor = tk.createCustomCursor(image, new Point(10,10), "yyy");
this.setCursor(myCursor);
}
}