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