zoukankan      html  css  js  c++  java
  • 打地鼠

    package cn.hncu.games;

    import java.awt.Cursor;
    import java.awt.Dimension;
    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.util.Date;
    import java.util.Random;

    import javax.swing.BorderFactory;
    import javax.swing.ImageIcon;
    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;
    /*
    * ※图片显示:
    * 1. JLabel类: 构造方法 JLabel(Icon image)
    2. Icon接口:有一个实现类为ImageIcon,它的构造方法:ImageIcon(String filename)

    ※自定义鼠标--设置光标
    3. java.awt.Toolkit工具类:
    函数一:public Cursor createCustomCursor(Image cursor, Point hotSpot, String name):可以创建一个自定义的光标(鼠标)
    函数二:public abstract Image createImage(String filename)创建并返回一个Image对象
    函数三:public static Toolkit getDefaultToolkit()获取默认工具包
    4.Image类:是表示图形图像的所有类的超类
    5.给窗口设置光标:void java.awt.Window.setCursor(Cursor cursor)


    ※图片和组件的叠加显示
    6.JFrame类中的一个函数
    public JLayeredPane getLayeredPane()返回此窗体的 layeredPane对象
    通过JLayeredPane对象添加的组件可以分层叠加显示
    *
    */

    public class HitMouse extends JFrame implements MouseListener, ActionListener {
    private final String DIR="./images/";
    private JLabel jlbMouse;//地鼠
    //定时器
    private Timer timer;
    private int delay=1000;
    private Random oRandom;//为确保真正的随机数,用util包中的Random类
    private JLabel timesShowLabel,timesHitLabel,gradeLabel;
    private int timesShow=0,timesHit=0,grade=1;

    private boolean isHit=false;
    public HitMouse() {
    this.setTitle("打地鼠");
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    setBack();
    //this.getContentPane().setLayout(null);//屏蔽框架原内容窗格默认的BorderLayout之后,地鼠的显示位置才能正常。
    //但如果下面添加了panel,那么地鼠的添加位置就由panel来定了(getContentPane获得的是该panel),自然没有之前的BorderLayout了。
    //而且这时还不能设置null布局,否则panel上面的“出现次数”等组件的显示会乱的

    //设置光标
    mySetCursor(1);

    //地鼠
    ImageIcon imageMouse = new ImageIcon(DIR+"dishu.png");
    jlbMouse = new JLabel(imageMouse);
    jlbMouse.setLocation(100, 100);
    jlbMouse.setSize(80, 80);
    this.getContentPane().add(jlbMouse);
    jlbMouse.setVisible(false);
    jlbMouse.addMouseListener(this);//让地鼠监听鼠标

    //接下来在界面中显示如下信息:出现次数,打中次数,等级
    JPanel panel = new JPanel();
    panel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
    panel.setPreferredSize(new Dimension(438, 375));
    this.getContentPane().add(panel);
    panel.setOpaque(false);

    //出现次数
    timesShowLabel = new JLabel("0",new ImageIcon(DIR+"chuxiancishu.png"),SwingConstants.CENTER);
    timesShowLabel.setFont(new Font("幼圆",Font.BOLD,20));
    timesShowLabel.setSize(146, 40);
    panel.add(timesShowLabel);
    //打中次数
    timesHitLabel = new JLabel("0",new ImageIcon(DIR+"dazhongcishu.png"),SwingConstants.CENTER);
    timesHitLabel.setFont(new Font("幼圆",Font.BOLD,20));
    timesHitLabel.setSize(146, 40);
    panel.add(timesHitLabel);
    //等级
    gradeLabel = new JLabel("1",new ImageIcon(DIR+"dangqiandengji.png"),SwingConstants.CENTER);
    gradeLabel.setFont(new Font("幼圆",Font.BOLD,20));
    gradeLabel.setSize(146, 40);
    panel.add(gradeLabel);


    //让地鼠不断出来--用定时器
    timer = new Timer(delay,this);//注意,定时器要用Swing包中的那个
    oRandom = new Random( new Date().getTime() );
    timer.start();

    this.setLocation(200, 200);
    this.setSize(438, 375);
    this.setResizable(false);
    this.setVisible(true);
    }
    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), "aaa");
    this.setCursor(myCursor);
    }

    public static void main(String[] args) {
    new HitMouse();
    }
    private void setBack() {
    //Container c = this.getContentPane();//new JPanel();
    //c.setOpaque(false);//编译时多态过不了,因为c是Container类型,所以在编译时要到Container类及它的父类中去找setOpaque()方法,而不存在
    JPanel p =(JPanel)(this.getContentPane());
    p.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));//后面那个参数(整数)表示分层显示时,当前图片的位置(放在哪一层),数值越小放在越底层
    }

    //鼠标事件的处理方法
    @Override
    public void mouseClicked(MouseEvent e) {
    }
    @Override
    public void mousePressed(MouseEvent e) {
    //设置图片光标
    mySetCursor(2);

    if(e.getSource()==jlbMouse && !isHit){
    ImageIcon imageIconDatou = new ImageIcon(DIR+"datou.png");
    jlbMouse.setIcon(imageIconDatou);

    //打中次数
    isHit=true;//做打中标记
    timesHit++;
    timesHitLabel.setText(""+timesHit);
    Toolkit.getDefaultToolkit().beep();

    //升级
    if(timesHit>8){
    delay -=190;
    if(delay<100){
    jlbMouse.setVisible(false);
    timer.stop();
    int a= JOptionPane.showConfirmDialog(this, "您打通关了,要再来一次吗?","恭喜!!!",JOptionPane.YES_NO_CANCEL_OPTION);
    if(a == JOptionPane.YES_OPTION){
    grade =0;
    delay=1000;
    }else if(a==JOptionPane.NO_OPTION){
    System.exit(0);
    }else{
    return;
    }
    }
    grade++;
    timesShow=0;
    timesHit=0;
    timer.stop();
    jlbMouse.setVisible(false);
    mySetCursor(1);

    JOptionPane.showMessageDialog(this, "进入第"+grade+"关,加油!!!");
    timesShowLabel.setText(""+timesShow);
    timesHitLabel.setText(""+timesHit);
    gradeLabel.setText(""+grade);
    timer.start();
    }
    }

    }
    @Override
    public void mouseReleased(MouseEvent e) {
    mySetCursor(1);
    }
    @Override
    public void mouseEntered(MouseEvent e) {
    }
    @Override
    public void mouseExited(MouseEvent e) {
    }

    //定时器事件的处理方法
    @Override
    public void actionPerformed(ActionEvent e) {
    int pos = oRandom.nextInt(9);
    switch(pos){
    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);
    }
    ImageIcon imageMouse = new ImageIcon(DIR+"dishu.png");
    jlbMouse.setIcon(imageMouse);
    isHit=false;//刚刚在新位置出来,设置未打中

    //出现次数加1
    timesShow++;
    timesShowLabel.setText(""+timesShow);

    //游戏玩输的处理
    if(timesShow>15){
    timer.stop();
    int a= JOptionPane.showConfirmDialog(this, "太菜,输了,不服,重来?","挂了!!!",JOptionPane.YES_NO_CANCEL_OPTION);
    if(a == JOptionPane.YES_OPTION){
    grade =1;
    delay=1000;
    timesShow=0;
    timesHit=0;
    gradeLabel.setText(""+grade);
    timesShowLabel.setText(""+timesShow);
    timesHitLabel.setText(""+timesHit);
    timer.restart();//用restart()比start更好
    }else if(a==JOptionPane.NO_OPTION){
    System.exit(0);
    }else{
    return;
    }
    }

    jlbMouse.setVisible(true);
    }

    }

  • 相关阅读:
    ArcObject获取ArcMap默认地理数据库的路径
    标准IO
    进程关系
    进程环境
    C语言基础知识位运算
    Bash 快捷键
    信号
    UNIX系统文件
    进程
    unix 文件属性
  • 原文地址:https://www.cnblogs.com/1314wamm/p/5600041.html
Copyright © 2011-2022 走看看