Java Play gopher game

Source: Internet
Author: User
Tags event listener gopher set background


Learn about image processing in 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;//Set the tag, whether the game ends private String dir= "./images/";//Picture directory, Current project under the JLabel jlbmouse;//timer timer;//Time timer random number object, that is, the location of the spawning ground mouse int delay=1100;//delay TIME Toolkit Tk;image Image Cursor MyCursor; JLabel shownum,currentgrade,hitnum;int shownumber=0,hitnumber=0,currentgrades=1;public HitMouse () {Super ("gopher"); This.setdefaultcloseoperation (Jframe.exit_on_close); This.setsize (449, 395); This.setlocationrelativeto (null);//Set the window in the center of the screen SetBackground ();//Set Background This.getcontentpane (). setlayout (null);//Set frame layout mode to empty, only in this way can you know the real location of the picture// Set the mouse for the hammer picture tk = Toolkit.getdefaulttoolkit (); image = Tk.createimage (dir+ "chui1.png"); mycursor = Tk.createcustomcursor ( Image, New Point (10,10), "xxx"); This.setcursor (mycursor); Setmessage ();//Set some hints//set the hamster picture on the background image 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);//Add mouse monitor// Timers timer = new Timer (delay,this), random = new Random (); Timer.start (); AddMenu ();//Add menu this.setresizable (false);// Setting the window size cannot be changed this.setvisible (true);} private void AddMenu () {JMenuBar menubar = new JMenuBar (); This.setjmenubar (menubar); JMenu game = new JMenu ("Games"); JMenuItem jitemnew = new JMenuItem ("New Game"); Jitemnew.setactioncommand ("Neo"); Jitemnew.addactionlistener (this); JMenuItem jitempause = new JMenuItem ("paused"); Jitempause.setactioncommand ("pause"); jitempause.addactionlIstener (this); JMenuItem jitemexit = new JMenuItem ("Exit"); Jitemexit.setactioncommand ("exit"); Jitemexit.addactionlistener (this); Game.add (jitemnew); Game.add (Jitempause); Game.addseparator ();//Menu Set divider line Game.add (jitemexit); Menubar.add (game);} private void SetBackground () {((JPanel) (This.getcontentpane ())). Setopaque (false);//If True, the component paints all the pixels within its bounds. Otherwise, the component may not draw some or all of the pixels, allowing its underlying pixels to be pivoted. ImageIcon bgimage = new ImageIcon ("images/beijing.jpg"); JLabel Bglabel = new JLabel (bgimage); bglabel.setbounds (0, Bgimage.geticonwidth (), Bgimage.geticonheight ()); This.getlayeredpane (). Add (Bglabel, New Integer (Integer.min_value));//Set the background image to the lowest level}private void Setmessage () { ImageIcon shownumb = new ImageIcon (dir+ "chuxiancishu.png"); JLabel ShowLabel = new JLabel (shownumb); Showlabel.setbounds (8, 8,); This.getcontentpane (). Add (ShowLabel); Shownum = new JLabel ("0"); shownum.setbounds (8, N, +); This.getcontentpane (). Add (shownum); ImageIcon hitnumb = new ImageIcon (dir+ "chuxiancishu.png"); JLabel Hitlabel = new JlabeL (hitnumb); Hitlabel.setbounds (148, 8, N/a), This.getcontentpane (). Add (Hitlabel); hitnum = new JLabel ("0"); Hitnum.setbounds (251, 8, This.getcontentpane); add (hitnum); ImageIcon grade = new ImageIcon (dir+ " Dangqiandengji.png "); JLabel Gradelabel = new JLabel (grade); Gradelabel.setbounds (288, 8,); This.getcontentpane (). Add (Gradelabel); Currentgrade = new JLabel ("1"); Currentgrade.setbounds (391, 8,); This.getcontentpane (). Add (Currentgrade);} public static void Main (string[] args) {new Hitmouse ();} public void actionperformed (ActionEvent e) {///For menu item Registration event Listener if (E.getsource () instanceof JMenuItem) {menuitemfun (e);} int Ran=random.nextint (9);//randomly generate a random number of 0~9 (excluding 9) ImageIcon Imagemouse = new ImageIcon (dir+ "dishu.png");// Ensure that every randomly generated hamster picture is a picture Jlbmouse.seticon (Imagemouse) when it is not beaten; switch (RAN) {case 0:jlbmouse.setlocation (in); 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 (133); Break;case 7: Jlbmouse.setlocation (310), Break;case 8:jlbmouse.setlocation (185, N.); Jlbmouse.setvisible (True); Shownumber++;shownum.settext ("" +shownumber); if (!gameplan ()) {//Determine if the game is over, And show the game Process Timer.stop ();}} Listen menu function function private void Menuitemfun (ActionEvent e) {if (E.getactioncommand (). Equalsignorecase ("new")) {// New games 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")) {//Exit system.exit (Exit_on_close);} if (E.getactioncommand (). Equalsignorecase ("pause")) {//Pause timer.stop (); Joptionpane.showmessagedialog (This, "continue, press OK"); 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);//mouse down, the mouse to display the image of the hit, simulated action//if hit the hamster, the hamster replaced by a shot of the picture, simulated hamster was hit 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;} When the mouse is relaxed, the mouse changes back to the original image when not pressed 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) {}} 






Java Play gopher game

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.