Hit the ground rat

Source: Internet
Author: User
Tags gopher

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;
/*
*※ picture shows:
* 1. JLabel class: Construction method JLabel (Icon image)
2. Icon interface: There is an implementation class for ImageIcon, which constructs the method: ImageIcon (String filename)

※ Custom Mouse--set cursor
3. Java.awt.Toolkit Tool Class:
Function One: Public cursor createcustomcursor (Image cursor, point hotSpot, String name): Can create a custom cursor (mouse)
Function two: Public abstract image CreateImage (String filename) creates and returns an Image object
Function three: public static Toolkit Getdefaulttoolkit () Get the default toolkit
Class 4.Image: A superclass that represents all classes of a graphics image
5. Set the cursor to the window: void java.awt.Window.setCursor (cursor cursor)


* Overlay display of pictures and components
A function in the 6.JFrame class
Public JLayeredPane Getlayeredpane () returns the Layeredpane object for this form
Components added through the JLayeredPane object can be layered overlay display
*
*/

public class Hitmouse extends JFrame implements MouseListener, ActionListener {
Private final String dir= "./images/";
Private JLabel jlbmouse;//Hamster
Timer
private timer timer;
private int delay=1000;
Private random orandom;//to ensure true random numbers, use the random class in the Util package
Private JLabel Timesshowlabel,timeshitlabel,gradelabel;
private int timesshow=0,timeshit=0,grade=1;

Private Boolean ishit=false;
Public Hitmouse () {
This.settitle ("Hit the Gopher");
This.setdefaultcloseoperation (Exit_on_close);
Setback ();
This.getcontentpane (). setlayout (null);//shield frame The original content pane after the default BorderLayout, the location of the mouse will be displayed correctly.
If, however, the panel is added below, the location of the gopher is determined by the panel (Getcontentpane gets the panel), and naturally there is no previous borderlayout.
You cannot set a null layout at this time, otherwise the display of components such as "occurrences" above the panel will be messy.

Set cursor
Mysetcursor (1);

Ground rat
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);//allow Gopher to monitor mouse

The following information is displayed in the interface: number of occurrences, number of hits, level
JPanel panel = new JPanel ();
Panel.setborder (Borderfactory.createbevelborder (bevelborder.lowered));
Panel.setpreferredsize (New Dimension (438, 375));
This.getcontentpane (). Add (panel);
Panel.setopaque (FALSE);

Number of occurrences
Timesshowlabel = new JLabel ("0", New ImageIcon (dir+ "Chuxiancishu.png"), Swingconstants.center);
Timesshowlabel.setfont (New Font ("Young Circle", font.bold,20));
Timesshowlabel.setsize (146, 40);
Panel.add (Timesshowlabel);
Hit count
Timeshitlabel = new JLabel ("0", New ImageIcon (dir+ "Dazhongcishu.png"), Swingconstants.center);
Timeshitlabel.setfont (New Font ("Young Circle", font.bold,20));
Timeshitlabel.setsize (146, 40);
Panel.add (Timeshitlabel);
Grade
Gradelabel = new JLabel ("1", New ImageIcon (dir+ "Dangqiandengji.png"), Swingconstants.center);
Gradelabel.setfont (New Font ("Young Circle", font.bold,20));
Gradelabel.setsize (146, 40);
Panel.add (Gradelabel);


Let the gopher keep coming out--with the timer
Timer = new timer (delay,this);//Note that the timer is to be used in the swing package
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);//compile-time polymorphic too, because C is the container type, so at compile time to go to the container class and its parent class to find the Setopaque () method, and does not exist
JPanel p = (JPanel) (This.getcontentpane ());
P.setopaque (false);//the Contents pane of the frame must be set to transparent so that the background can be seen after adding components

ImageIcon bgimage = new ImageIcon (dir+ "beijing.jpg");
JLabel Bglabel = new JLabel (bgimage);
Bglabel.setbounds (0, 0, bgimage.geticonwidth (), Bgimage.geticonheight ());//layered when added, no size, not displayed
This.add (Bglabel);
This.getlayeredpane (). Add (Bglabel,new Integer (integer.min_value));//the next parameter (integer) indicates the position of the current picture (in which layer), the smaller the value, the lower the lower
}

How to handle mouse events
@Override
public void mouseclicked (MouseEvent e) {
}
@Override
public void mousepressed (MouseEvent e) {
Set the picture cursor
Mysetcursor (2);

if (E.getsource () ==jlbmouse &&!ishit) {
ImageIcon Imageicondatou = new ImageIcon (dir+ "datou.png");
Jlbmouse.seticon (Imageicondatou);

Hit count
ishit=true;//hit Mark.
timeshit++;
Timeshitlabel.settext ("+timeshit");
Toolkit.getdefaulttoolkit (). Beep ();

Upgrade
if (timeshit>8) {
Delay-=190;
if (delay<100) {
Jlbmouse.setvisible (FALSE);
Timer.stop ();
int a= Joptionpane.showconfirmdialog (this, "Did you get through the switch again?", "Congratulations!!!", 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, "Enter the section" +grade+ "OFF, refueling!!!");
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) {
}

How to handle Timer events
@Override
public void actionperformed (ActionEvent e) {
int pos = orandom.nextint (9);
Switch (POS) {
Case 0:
Jlbmouse.setlocation (56,63);
Case 1:
Jlbmouse.setlocation (322,204);
Case 2:
Jlbmouse.setlocation (185,204);
Case 3:
Jlbmouse.setlocation (48,203);
Case 4:
Jlbmouse.setlocation (298,133);
Case 5:
Jlbmouse.setlocation (162,133);
Case 6:
Jlbmouse.setlocation (22,133);
Case 7:
Jlbmouse.setlocation (311,63);
Case 8:
Jlbmouse.setlocation (186,63);
}
ImageIcon imagemouse = new ImageIcon (dir+ "dishu.png");
Jlbmouse.seticon (Imagemouse);
ishit=false;//just came out in the new location, set the missed

Number of occurrences plus 1
timesshow++;
Timesshowlabel.settext ("+timesshow");

Game play and lose processing
if (timesshow>15) {
Timer.stop ();
int a= Joptionpane.showconfirmdialog (this, "too vegetable, lose, Defy, re?", "Hung!!!", 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 () better than start
}else if (a==joptionpane.no_option) {
System.exit (0);
}else{
Return
}
}

Jlbmouse.setvisible (TRUE);
}

}

Hit the ground rat

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.