Simple picture-processing technology
All pictures Connection: Http://yunpan.cn/cQyWXfIhIUDqX (extract code: 2DB4)
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.util.random;import Javax.swing.imageicon;import Javax.swing.JFrame; Import Javax.swing.jlabel;import Javax.swing.jpanel;import Javax.swing.timer;public class Moleattack extends JFrame Implements ActionListener, MouseListener {private String Strdir = "./image/moleattackimage/";//path where the picture is JLabel Jlbmole, H Itnumber, Appearnumber, gradenumber;//picture tag timer timer; Timer random random; Random number int delay = 1000;int hit = 0, Apper = 0, grade = 0; The number of hits, number of occurrences, rank public moleattack () {this.settitle ("hamster"), setback ();//Set Background setgrade ();//Set Hit count, number of occurrences, Level This.getcontentpane (). setlayout (NULL); Because you want to put the picture in its own position, so the layout will be canceled//set the cursor Toolkit TK = Toolkit.getdefaulttoolkit (); Image image = Tk.createimage (Strdir + " Hammer.png "); Cursor MyCursor = Tk.creaTecustomcursor (Image, new Point (Ten), "XH"); This.setcursor (mycursor);//Set up the gopher ImageIcon imagemole = new ImageIcon ( Strdir + "Mole.png"); jlbmole = new JLabel (imagemole); jlbmole.setsize (+); This.getcontentpane (). Add (Jlbmole); Jlbmole.setvisible (false); This.getcontentpane (). Addmouselistener (this);//Monitor the panel Jlbmole.addmouselistener (this) ;//Listen to whether the Gopher is hit//timer = new Timer (delay, this); Timer.start (); random = new Random (); This.setlocation (200, 200); This.setsize (438, 375); this.setresizable (false); this.setvisible (true);} Set the background private void setback () {((JPanel) (This.getcontentpane ())). Setopaque (false);//Make Getcontentpane () transparent ImageIcon Bgimage = new ImageIcon (Strdir + "background.jpg"); JLabel Bglabel = new JLabel (bgimage); bglabel.setbounds (0, 0, bgimage.geticonwidth (), Bgimage.geticonheight ()); This.getlayeredpane (). Add (Bglabel, New Integer (Integer.min_value));//The priority of the background image needs to be set to a minimum so that the other picture shows the}//setting the hit count, Number of occurrences, level private void Setgrade () {//set occurrences ImageIcon imagenumber = new ImageIcon (Strdir +"Chuxiancishu.png"); JLabel number = new JLabel (imagenumber); Number.setsize (200, 50); Number.setlocation ( -30); This.getcontentpane (). Add (number);//Display data Appearnumber = new JLabel (); Appearnumber.settext ("" + Apper); appearnumber.setlocation (122); Appearnumber.setfont (New Font ("Arial", Font.Bold, 19 ); Appearnumber.setsize (), This.getcontentpane (). Add (Appearnumber);//Set hit count ImageIcon imagehittime = new ImageIcon (Strdir + "dazhongcishu.png"); JLabel hittime = new JLabel (imagehittime); Hittime.setsize (200, 50); Hittime.setlocation (This.getcontentpane); add (hittime);//Display data Hitnumber = new JLabel (); Hitnumber.settext ("" + hit); Hitnumber.setlocation (260, 10); Hitnumber.setfont (New Font ("Arial", Font.Bold, 19)); Hitnumber.setsize (+), This.getcontentpane (). Add (Hitnumber);//actual current level ImageIcon Imagegrade = new ImageIcon (Strdir + "Dangqiandengji.png"); JLabel Grade = new JLabel (Imagegrade); Grade.setsize (200, 50); Grade.setlocation (245); This.getcontentpane (). Add (Grade);//Display Data Gradenumber = nEW JLabel (); Gradenumber.settext ("" + grade); Gradenumber.setlocation (402); Gradenumber.setfont (New Font ("Arial"), Gradenumber.setsize (Font.Bold)); This.getcontentpane (). Add (Gradenumber); public static void Main (string[] args) {new Moleattack ();} @Overridepublic void actionperformed (ActionEvent e) {appearnumber.settext ("" + (++apper));//each occurrence of a hamster, the number of occurrences plus//10 times each occurrence of the gopher, Level plus one (occurrence frequency becomes faster) if (apper% = = 0) {Gradenumber.settext ("" + (++grade)); Timer.setdelay ((int) (DELAY * 0.9));} Prevent the public after the same will be the original appearance ImageIcon Imageticonhit = new ImageIcon (Strdir + "mole.png"); Jlbmole.seticon (Imageticonhit); Jlbmole.setvisible (TRUE);//random occurrence of the location of the mouse int r = random.nextint (9); switch (r + 1) {case 1:jlbmole.setlocation (SI, n); break; Case 2:jlbmole.setlocation (204), Break;case 3:jlbmole.setlocation (204), Break;case 4:jlbmole.setlocation (45 , 203); Break;case 5:jlbmole.setlocation (295, 133); Break;case 6:jlbmole.setlocation (133); Break;case 7: Jlbmole.setlocation (133), Break;case 8:jlbmole.setlocation (310, N.); breakCase 9:jlbmole.setlocation (183, N); Jlbmole.setvisible (TRUE);} @Overridepublic void mouseclicked (MouseEvent e) {} @Overridepublic void mousepressed (MouseEvent e) {// The cursor hits the hamster and turns into a hit like Toolkit tk = Toolkit.getdefaulttoolkit (); Image image = Tk.createimage (Strdir + "hit.png"); Cursor mycursor = tk.createcustomcursor (image, new Point (Ten), "yyy"), This.setcursor (mycursor);//Determines whether to hit if ( E.getsource () = = Jlbmole) {//change the appearance of the hamster ImageIcon imageticonhit = new ImageIcon (Strdir + "touch.png"); Jlbmole.seticon ( Imageticonhit); jlbmole.setvisible (true); Hitnumber.settext ("" + (++hit));//Hit after hit number plus one}} @Overridepublic void mousereleased (MouseEvent e) {//After the cursor is released, Revert to the original look Toolkit tk = Toolkit.getdefaulttoolkit (); Image image = Tk.createimage (Strdir + "hammer.png"); Cursor mycursor = tk.createcustomcursor (image, new Point (Ten), "yyy"); This.setcursor (mycursor);} @Overridepublic void mouseentered (MouseEvent e) {} @Overridepublic void mouseexited (MouseEvent e) {}}
Hit the Hamster in Java