Java implementation Swing Imitation Jinshan typing case source code

Source: Internet
Author: User

Java implementation Swing Imitation Jinshan typing case source code, more Java technology to go to the Java Tutorial Network. Http://java.662p.com

Code:

<font size= "3" >import java.awt.color;import java.awt.font;import java.awt.graphics;import java.awt.Image; Import Java.awt.toolkit;import java.io.file;import Java.io.ioexception;import Java.util.random;import   Javax.imageio.imageio;public class Main {public char C; The letters on the Apple are public int x = 0, y =  The enemy appears in coordinates public final int xspeed=5,yspeed=2;    The speed at which the Apple moves in xy direction public int center;  Initial Center Value public boolean turnleft = true;    Whether to move public to the left Boolean alive = true;   Whether to live public random ran = new random ();    Random number of seed public typeframe tf=null;   The owning frame public Image appleimg = null; Picture of apple Public image bg = Toolkit.getdefaulttoolkit (). GetImage ("bg.jpg");                Background picture public Main (Typeframe tf) {THIS.TF=TF;  x = Randomlocation ();  Random x-coordinate y=ran.nextint (20) is obtained.          Get the random y-coordinate if (Ran.nextint (2) ==0) {turnleft=true;      }else {turnleft=false;   } center = x;  Set the initial center value to x C=randomchar ();  Get the random alphabetic value of try {appleimg = Imageio.read (New File ("Apple.gif"));                        Apple picture} catch (IOException e) {//TODO auto-generated catch block                E.printstacktrace ();  }} public void Draw (Graphics g) {Color color = G.getcolor ();   Get context Color G.setcolor (color.red);   Sets the context color G.setfont (new Font ("Dialog", 4, 40));    Sets the font if (alive) {g.drawimage (appleimg, x, y, null);   Draw Apple Pictures g.drawstring (c+ "", X + +, Y + 60);  Draw the Apple Letter} g.setcolor (color); Set the context color back} public int randomlocation () {//the function int x1=ran.nextint that produces the Apple's random horizontal axis (TypEFRAME.GAME_WIDTH-40);                                for (int i = 0; i < tf.apples.size (); i++) {if (Math.Abs (X1-tf.apples.get (i). x) <60) {                        return Randomlocation ();        }} return x1;                } public char Randomchar () {//produces a method that does not have the same letter as the existing Apple letter Char ch= (char) (' A ' +ran.nextint (26));                                for (int i = 0; i < tf.apples.size (); i++) {if (Ch==tf.apples.get (i). c)                return Randomchar ();        } return ch; }} </font>

Detailed Description: http://java.662p.com/thread-3680-1-1.html

Java implementation Swing Imitation Jinshan typing case source code

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.