java打地鼠遊戲,java打地鼠

來源:互聯網
上載者:User

java打地鼠遊戲,java打地鼠


學習java中的圖片處理


import java.awt.Cursor;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.Random;import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.Timer;public class HitMouse extends JFrame implements ActionListener,MouseListener{boolean isOver=false;//設定標記,遊戲是否結束private String dir="./images/";//圖片目錄,當前工程下JLabel jlbMouse;//地鼠Timer timer;//時間定時器Random random;//隨機數對象,即產生地鼠的位置int delay=1100;//延遲時間Toolkit tk;Image image;Cursor myCursor;JLabel showNum,currentGrade,hitNum;int showNumber=0,hitNumber=0,currentGrades=1;public HitMouse(){super("打地鼠");this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setSize(449, 395);this.setLocationRelativeTo(null);//設定視窗在螢幕中心setbackground();//設定背景this.getContentPane().setLayout(null);//設定架構配置模式為空白,只有這樣,才能知道圖片的真正位置//設定滑鼠為鎚子圖片tk = Toolkit.getDefaultToolkit();image = tk.createImage(dir+"chui1.png");myCursor = tk.createCustomCursor(image, new Point(10,10), "xxx");this.setCursor(myCursor);setMessage();//設定一些提示資訊//在背景圖片的基礎上設定地鼠圖片ImageIcon imageMouse = new ImageIcon(dir+"dishu.png");jlbMouse = new JLabel(imageMouse);jlbMouse.setSize(80,80);this.getContentPane().add(jlbMouse);jlbMouse.setVisible(false);jlbMouse.addMouseListener(this);//添加滑鼠監聽//定時器timer = new Timer(delay,this);random = new Random();timer.start();addMenu();//添加菜單this.setResizable(false);//設定視窗大小不能改變this.setVisible(true);}private void addMenu() {JMenuBar menubar = new JMenuBar();this.setJMenuBar(menubar);JMenu game = new JMenu("遊戲");JMenuItem jitemNew = new JMenuItem("新遊戲");jitemNew.setActionCommand("new");jitemNew.addActionListener(this);JMenuItem jitemPause = new JMenuItem("暫停");jitemPause.setActionCommand("pause");jitemPause.addActionListener(this);JMenuItem jitemExit = new JMenuItem("退出");jitemExit.setActionCommand("exit");jitemExit.addActionListener(this);game.add(jitemNew);game.add(jitemPause);game.addSeparator();//菜單裡設定分隔線game.add(jitemExit);menubar.add(game);}private void setbackground() {((JPanel)(this.getContentPane())).setOpaque(false);//如果為 true,則該組件繪製其邊界內的所有像素。否則該組件可能不繪製部分或所有像素,從而允許其底層像素透視出來。 ImageIcon bgImage = new ImageIcon("images/beijing.jpg");JLabel bgLabel = new JLabel(bgImage);bgLabel.setBounds(0, 25, bgImage.getIconWidth(), bgImage.getIconHeight());this.getLayeredPane().add(bgLabel, new Integer(Integer.MIN_VALUE));//設定背景圖片的層次最低}private void setMessage() {ImageIcon showNumb = new ImageIcon(dir+"chuxiancishu.png");JLabel showLabel = new JLabel(showNumb);showLabel.setBounds(8, 8, 92, 80);this.getContentPane().add(showLabel);showNum = new JLabel("0");showNum.setBounds(110, 8, 92, 80);this.getContentPane().add(showNum);ImageIcon hitNumb = new ImageIcon(dir+"chuxiancishu.png");JLabel hitLabel = new JLabel(hitNumb);hitLabel.setBounds(148, 8, 92, 80);this.getContentPane().add(hitLabel);hitNum = new JLabel("0");hitNum.setBounds(251, 8, 92, 80);this.getContentPane().add(hitNum);ImageIcon grade = new ImageIcon(dir+"dangqiandengji.png");JLabel gradeLabel = new JLabel(grade);gradeLabel.setBounds(288, 8, 92, 80);this.getContentPane().add(gradeLabel);currentGrade = new JLabel("1");currentGrade.setBounds(391, 8, 92, 80);this.getContentPane().add(currentGrade);}public static void main(String[] args) {new HitMouse();}public void actionPerformed(ActionEvent e) { //對功能表項目註冊事件監聽if(e.getSource() instanceof JMenuItem){menuItemFun(e);}int ran=random.nextInt(9);//隨機產生一個0~9(不包括9)的隨機數ImageIcon imageMouse = new ImageIcon(dir+"dishu.png");//保證每次隨機產生的地鼠圖片都是為沒被打時的圖片jlbMouse.setIcon(imageMouse);switch(ran){case 0:jlbMouse.setLocation(55, 63);break;case 1:jlbMouse.setLocation(321, 204);break;case 2:jlbMouse.setLocation(184, 204);break;case 3:jlbMouse.setLocation(47, 203);break;case 4:jlbMouse.setLocation(297, 133);break;case 5:jlbMouse.setLocation(161, 133);break;case 6:jlbMouse.setLocation(21, 133);break;case 7:jlbMouse.setLocation(310, 63);break;case 8:jlbMouse.setLocation(185, 63);break;}jlbMouse.setVisible(true);showNumber++;showNum.setText(""+showNumber);if( !gamePlan() ){//判斷遊戲是否結束,並顯示遊戲進程timer.stop();}}//監聽菜單功能功能private void menuItemFun(ActionEvent e) {if (e.getActionCommand().equalsIgnoreCase("new")) {//新遊戲timer.stop();showNumber=0;hitNumber=0;currentGrades=1;delay=1000;isOver=false;showNum.setText(""+showNumber);hitNum.setText(""+hitNumber);currentGrade.setText(""+currentGrades);timer = new Timer(delay,this);timer.start();}if (e.getActionCommand().equalsIgnoreCase("exit")) {//退出System.exit(EXIT_ON_CLOSE);}if (e.getActionCommand().equalsIgnoreCase("pause")) {//暫停timer.stop();JOptionPane.showMessageDialog(this, "繼續請按“確定”");timer.start();}}private boolean gamePlan() {if(showNumber-hitNumber > 8){JOptionPane.showMessageDialog(this, "Game Over !");isOver=true;return false;}if(hitNumber > 5){hitNumber=0;showNumber=0;currentGrades++;if(delay>100){delay-=50;}else if(delay>=500){delay=500;}timer.setDelay(delay);hitNum.setText(""+hitNumber);showNum.setText(""+showNumber);currentGrade.setText(""+currentGrades);}return true;}public void mouseClicked(MouseEvent e) {}public void mousePressed(MouseEvent e) {if(isOver){return ;}image = tk.createImage(dir+"chui2.png");myCursor = tk.createCustomCursor(image, new Point(10,10), "xxx");this.setCursor(myCursor);//滑鼠按下時,滑鼠顯示打下去的圖片,類比打的動作//如果打中地鼠,則地鼠換成被打中的圖片,類比地鼠被打if(e.getSource()==jlbMouse){ImageIcon imageIconHit = new ImageIcon(dir+"datou.png");jlbMouse.setIcon(imageIconHit);jlbMouse.setVisible(true);}hitNumber++;hitNum.setText(""+hitNumber);}public void mouseReleased(MouseEvent e) {if(isOver){return ;}//當滑鼠放鬆以後,滑鼠變回原來沒按下時的圖片image = tk.createImage(dir+"chui1.png");myCursor = tk.createCustomCursor(image, new Point(10,10), "xxx");this.setCursor(myCursor);}public void mouseEntered(MouseEvent e) {}public void mouseExited(MouseEvent e) {}}






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.