java语言金山打字_[Java教程]java实现 swing模仿金山打字 案例源码

[Java教程]java实现 swing模仿金山打字 案例源码

0 2014-11-17 12:00:21

java实现 swing模仿金山打字 案例源码,更多Java技术就去Java教程网。http://java.662p.com

代码: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; //苹果上的字母 public int x = 60, y = 0; // 敌人出现的坐标 public final int XSPEED=5,YSPEED=2; //苹果xy方向移动的速度 public int center; //初始中心值 public boolean turnleft = true; //是否向左移动 public boolean alive = true; //是否活着 public Random ran = new Random(); //随机数的种子 public TypeFrame tf=null; //所属的框架 public Image appleimg = null; //苹果的图片 public Image bg = Toolkit.getDefaultToolkit().getImage("bg.jpg"); //背景图片 public Main(TypeFrame tf) { this.tf=tf; x = randomlocation(); //得到随机合格的随机x坐标 y=ran.nextInt(20); //得到随机的y坐标 if(ran.nextInt(2)==0){ turnleft=true; }else { turnleft=false; } center = x; //设置初始中心值为x c=randomchar(); //得到随机的字母值 try { appleimg = ImageIO.read(new File("apple.gif")); //苹果的图片 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void draw(Graphics g) { Color color = g.getColor(); //得到上下文颜色 g.setColor(Color.red); //设置上下文颜色 g.setFont(new Font("Dialog", 4, 40)); //设置字体 if (alive) { g.drawImage(appleimg, x, y, null); //绘制苹果图片 g.drawString(c+ "", x + 20, y + 60); //绘制苹果字母 } g.setColor(color); //将上下文颜色设置回去 } public int randomlocation(){ //产生苹果的随机横坐标的函数 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(){ //产生不与存在的苹果字母相同的字母的方法 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; }}

详细说明:http://java.662p.com/thread-3680-1-1.html

本文网址:http://www.shaoqun.com/a/104820.html

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:admin@shaoqun.com。

JAVA

0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值