Java hamster game, java hamster
Learning 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; // you can specify whether the game ends with private String dir = ". /images/"; // image directory, under the current project, JLabel jlbMouse; // hamster Timer timer; // time Timer Random random; // Random number object, that is, the location where the hamster is generated int delay = 1100; // delay Time Toolkit tk; Image image Image; Cursor myCursor; JLabel showNum, cur Required grade, hitNum; int showNumber = 0, hitNumber = 0, currentGrades = 1; public HitMouse () {super ("Hamster"); this. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); this. setSize (449,395); this. setLocationRelativeTo (null); // set the window to setbackground () in the center of the screen; // set the background this. getContentPane (). setLayout (null); // set the frame Layout mode to null. Only in this way can you know the true position of the image. // set the mouse to the hammer image tk = Toolkit. getdefatooltoolkit (); image = tk. createImage (dir + "chui1.png"); myCursor = tk. c ReateCustomCursor (image, new Point (10, 10), "xxx"); this. setCursor (myCursor); setMessage (); // set some prompt information // set the hamster image ImageIcon imageMouse = new ImageIcon (dir + "dishu.png") based on the background image "); jlbMouse = new JLabel (imageMouse); jlbMouse. setSize (80, 80); this. getContentPane (). add (jlbMouse); jlbMouse. setVisible (false); jlbMouse. addMouseListener (this); // Add a mouse listener // timer Timer = new timer (delay, this); random = new Random (); timer. start (); AddMenu (); // Add menu this. setResizable (false); // set the window size to not change this. setVisible (true);} private void addMenu () {JMenuBar menubar = new JMenuBar (); this. setJMenuBar (menubar); JMenu game = new JMenu ("game"); JMenuItem jitemNew = new JMenuItem ("new game"); jitemNew. setActionCommand ("new"); jitemNew. addActionListener (this); JMenuItem jitemPause = new JMenuItem ("paused"); jitemPause. setActionCommand ("pause"); jitemPause. addActionLi Stener (this); JMenuItem jitemExit = new JMenuItem ("exit"); jitemExit. setActionCommand ("exit"); jitemExit. addActionListener (this); game. add (jitemNew); game. add (jitemPause); game. addSeparator (); // set the separation line game in the menu. add (jitemExit); menubar. add (game);} private void setbackground () {(JPanel) (this. getContentPane ())). setOpaque (false); // if it is true, the component draws all pixels within its boundary. Otherwise, the component may not draw part or all pixels, so that the underlying pixels can be viewed. 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); // sets the lowest level of the background image} 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); I MageIcon 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 actionreceivmed (ActionEvent e) {// listen to if (e. g EtSource () instanceof JMenuItem) {menuItemFun (e);} int ran = random. nextInt (9); // randomly generate a 0 ~ 9 (excluding 9) Random Number ImageIcon imageMouse = new ImageIcon (dir + "dishu.png"); // ensure that the randomly generated hamster image is the image jlbMouse when it is not hit. 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: jl BMouse. 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 () {// checks whether the game is over and displays the game process timer. stop () ;}// listener menu function private void menuItemFun (ActionEvent e) {if (e. getActionCommand (). specified signorecase ("new") {// new game 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 (). e QualsIgnoreCase ("exit") {// exit System. exit (EXIT_ON_CLOSE);} if (e. getActionCommand (). equalsIgnoreCase ("pause") {// pause timer. stop (); JOptionPane. showMessageDialog (this, "continue to 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); // when you press the mouse, the mouse displays the picture to be played. The simulated action // if you hit the mouse, the mouse changes to the picture to be played, the simulated hamster is beaten 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 image = tk when it is not pressed. 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 ){}}