Java implements swing to imitate Kingsoft typing case source code, javaswing

Source: Internet
Author: User

Java implements swing to imitate Kingsoft typing case source code, javaswing

Java implements swing to imitate Kingsoft's typing case source code. More Java technologies will go to 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 Apple letter public int x = 60, y = 0; // The public final int XSPEED = 5, YSPEED = 2; // the speed at which Apple xy moves to the public int center; // The initial center value is public boolean turnleft = true; // whether to move public boolean alive = true to the left; // public Random ran = new Random (); // SEED public TypeFrame tf = null; // framework public Image appleimg = null; // Apple Image public Image bg = Toolkit. getdefatooltoolkit (). getImage ("bg.jpg"); // The background image public Main (TypeFrame tf) {this. tf = tf; x = randomlocation (); // obtain the random x coordinate y = ran. nextInt (20); // obtain 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 (); // obtain the random letter value try {appleimg = ImageIO. read (new File ("apple.gif"); // Apple image} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} public void draw (Graphics g) {Color = g. getColor (); // obtain the context color g. setColor (Color. red); // set the context color g. setFont (new Font ("Dialog", 4, 40); // set the Font if (alive) {g. drawImage (appleimg, x, y, null); // draw an apple image g. drawString (c + "", x + 20, y + 60); // draw an apple letter} g. setColor (color); // set the context color back} public int randomlocation () {// function that generates Apple's random abscissa int x1 = ran. nextInt (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 () {// method for generating letters not the same as the existing Apple letters 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>

Http://java.662p.com/thread-3680-1-1.html

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.