这是一个文字移动的applet。
但是程序开始之后,文字会有跳跃。而且每次运行的时候跳跃的距离不一样。
程序设计上是,程序载入之后,文字移动15, 然后sleep(100)之后,然后再移动20,然后这样循环。但实际情况是,程序一运行。第一次不是跳跃15,有可能跳了30,或者45,60之类。。就是连续跳了好几次,中间没有sleep。然后有了第一次sleep之后,后面就正常了。源代码入下。/*
* Created on 2006-3-12
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*//**
* @author Chunlei Gu
* @version 1.0, March 12,2006
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.awt.*;
import java.applet.*;public class SimpleJavaApplet extends Applet implements Runnable{
Thread animation;
AudioClip music;
Image background;
final static int screenWidth=400;
final static int screenHeight=400;
final static int moveDistance=15;
final static int frameDelay=1000;
private String messageParam="message_param";
private String imageParam="image_param";
private String audioParam="audio_param";
String message="";
String imageName="";
String audioName="";
int xPosition=screenWidth/2;
int yPosition=screenWidth/2;
int fontSizeCurrent=30;
boolean moveUp=true;
public void init (){
//message=this.getParameter(messageParam);
//imageName=this.getParameter(imageParam);
//audioName=this.getParameter(audioParam);
message="welcome";
imageName="image.jpg";
audioName="audio.mid";
background=getImage(getDocumentBase(),imageName);
music=getAudioClip(getDocumentBase(),audioName);
setSize(screenWidth,screenHeight);
music.loop();
} public void paint(Graphics g){
g.drawImage(background,0,0,this);
if (moveUp){
g.setFont(new Font("Times New Roman",Font.BOLD,fontSizeCurrent));
//fontSizeCurrent+=2;
g.setColor(Color.RED);
if (yPosition>0&& yPosition<screenHeight){
yPosition-=moveDistance;
System.out.println(yPosition);
}
else if (yPosition<=0){
moveUp=false;
yPosition+=moveDistance;
System.out.println(yPosition);
}
}
else{
g.setFont(new Font("Arial",Font.ITALIC,fontSizeCurrent));
//fontSizeCurrent-=2;
g.setColor(Color.BLUE);
if (yPosition<screenHeight){
yPosition+=moveDistance;
System.out.println(yPosition);
}
else if(yPosition>=screenHeight){
moveUp=true;
yPosition-=moveDistance;
System.out.println(yPosition);
}
}
g.drawString(message,xPosition,yPosition);
System.out.println(xPosition+"---"+yPosition);
}
public void start(){
try{
if (this.animation==null){
System.out.println("animation==null");
this.animation=new Thread(this );
System.out.println("new animation ");
this.animation.start();
System.out.println(" animation start");
}
else{
this.animation.start();
System.out.println("animation!=null");
System.out.println(" animation start");
}
}catch (Exception ex){
System.out.println("error:"+ex);
}
}
public void run(){
try{
while (true){
repaint();
System.out.println("try repaint();");
//this.animation.sleep(frameDelay);
Thread.sleep(frameDelay);
System.out.println("sleep ;");
}
}catch (InterruptedException ex){
System.out.println("error"+ex);
}
}
public boolean mouseDown (Event e, int x, int y){
//if(this.animation != null&&this.animation.isAlive()){
//this.stop();
//this.animation=null;
//}
yPosition=screenHeight/2;
this.repaint();
moveUp=true;
fontSizeCurrent=30;
return true;
}
public void update(Graphics g){
paint(g);
System.out.println("update");
}
}由于测试基本每行都加了system.out.println();其中一次的运行结果如下:从中可以发现从200到一直减到35的时候才sleep一次。
animation==null
new animation
try repaint();
animation start
185
200---185
170
200---170
155
200---155
140
200---140
125
200---125
110
200---110
95
200---95
80
200---80
65
200---65
50
200---50
sleep ;
35
200---35
try repaint();
sleep ;
20
200---20
try repaint();
但是程序开始之后,文字会有跳跃。而且每次运行的时候跳跃的距离不一样。
程序设计上是,程序载入之后,文字移动15, 然后sleep(100)之后,然后再移动20,然后这样循环。但实际情况是,程序一运行。第一次不是跳跃15,有可能跳了30,或者45,60之类。。就是连续跳了好几次,中间没有sleep。然后有了第一次sleep之后,后面就正常了。源代码入下。/*
* Created on 2006-3-12
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*//**
* @author Chunlei Gu
* @version 1.0, March 12,2006
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.awt.*;
import java.applet.*;public class SimpleJavaApplet extends Applet implements Runnable{
Thread animation;
AudioClip music;
Image background;
final static int screenWidth=400;
final static int screenHeight=400;
final static int moveDistance=15;
final static int frameDelay=1000;
private String messageParam="message_param";
private String imageParam="image_param";
private String audioParam="audio_param";
String message="";
String imageName="";
String audioName="";
int xPosition=screenWidth/2;
int yPosition=screenWidth/2;
int fontSizeCurrent=30;
boolean moveUp=true;
public void init (){
//message=this.getParameter(messageParam);
//imageName=this.getParameter(imageParam);
//audioName=this.getParameter(audioParam);
message="welcome";
imageName="image.jpg";
audioName="audio.mid";
background=getImage(getDocumentBase(),imageName);
music=getAudioClip(getDocumentBase(),audioName);
setSize(screenWidth,screenHeight);
music.loop();
} public void paint(Graphics g){
g.drawImage(background,0,0,this);
if (moveUp){
g.setFont(new Font("Times New Roman",Font.BOLD,fontSizeCurrent));
//fontSizeCurrent+=2;
g.setColor(Color.RED);
if (yPosition>0&& yPosition<screenHeight){
yPosition-=moveDistance;
System.out.println(yPosition);
}
else if (yPosition<=0){
moveUp=false;
yPosition+=moveDistance;
System.out.println(yPosition);
}
}
else{
g.setFont(new Font("Arial",Font.ITALIC,fontSizeCurrent));
//fontSizeCurrent-=2;
g.setColor(Color.BLUE);
if (yPosition<screenHeight){
yPosition+=moveDistance;
System.out.println(yPosition);
}
else if(yPosition>=screenHeight){
moveUp=true;
yPosition-=moveDistance;
System.out.println(yPosition);
}
}
g.drawString(message,xPosition,yPosition);
System.out.println(xPosition+"---"+yPosition);
}
public void start(){
try{
if (this.animation==null){
System.out.println("animation==null");
this.animation=new Thread(this );
System.out.println("new animation ");
this.animation.start();
System.out.println(" animation start");
}
else{
this.animation.start();
System.out.println("animation!=null");
System.out.println(" animation start");
}
}catch (Exception ex){
System.out.println("error:"+ex);
}
}
public void run(){
try{
while (true){
repaint();
System.out.println("try repaint();");
//this.animation.sleep(frameDelay);
Thread.sleep(frameDelay);
System.out.println("sleep ;");
}
}catch (InterruptedException ex){
System.out.println("error"+ex);
}
}
public boolean mouseDown (Event e, int x, int y){
//if(this.animation != null&&this.animation.isAlive()){
//this.stop();
//this.animation=null;
//}
yPosition=screenHeight/2;
this.repaint();
moveUp=true;
fontSizeCurrent=30;
return true;
}
public void update(Graphics g){
paint(g);
System.out.println("update");
}
}由于测试基本每行都加了system.out.println();其中一次的运行结果如下:从中可以发现从200到一直减到35的时候才sleep一次。
animation==null
new animation
try repaint();
animation start
185
200---185
170
200---170
155
200---155
140
200---140
125
200---125
110
200---110
95
200---95
80
200---80
65
200---65
50
200---50
sleep ;
35
200---35
try repaint();
sleep ;
20
200---20
try repaint();
解决方案 »
- ~~~~~~对于一个Java 初学者 的一些疑问
- finally真的会永远执行吗?
- 如何去掉TextArea中的水平滚条
- JAVA面试题解惑系列(十一)——这些运算符你是否还记得?
- 数组问题
- 如何在JPanel里通过点击按钮激活文件对话框来打开图片文件
- 用SJF排序process,之后打印出*的问题
- java.lang.NoClassDefFoundError
- 关于类设计方面的问题,希望能够讨论讨论
- 请问考过SCJP的大虾,SCJP考试的题目是中文还是英文,证书的有效期是多长时间?
- 请教大家一个怪问题的实现:在public static void main()里写个方法,并在里面直接调用,请问该怎么写?
- 最近想模仿着做个"扫雷",可是遇到了些个问题....
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Event;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;import com.sun.j3d.utils.applet.MainFrame;@SuppressWarnings("serial")
public class SimpleJavaApplet extends Applet implements Runnable { Thread animation; AudioClip music; Image background; final static int screenWidth = 400; final static int screenHeight = 400; final static int moveDistance = 15; final static int frameDelay = 5000; private String messageParam = "message_param"; private String imageParam = "image_param"; private String audioParam = "audio_param"; String message = ""; String imageName = ""; String audioName = ""; int xPosition = screenWidth / 2; int yPosition = screenWidth / 2; int fontSizeCurrent = 30; boolean moveUp = true; public void init() { // message=this.getParameter(messageParam);
// imageName=this.getParameter(imageParam);
// audioName=this.getParameter(audioParam); message = "welcome";
imageName = "image.jpg";
audioName = "audio.mid"; //background = getImage(getDocumentBase(), imageName);
//music = getAudioClip(getDocumentBase(), audioName);
setSize(screenWidth, screenHeight);
//music.loop();
} public void paint(Graphics g) {
g.drawImage(background, 0, 0, this); if (moveUp) {
g.setFont(new Font("Times New Roman", Font.BOLD, fontSizeCurrent));
// fontSizeCurrent+=2;
g.setColor(Color.RED);
if (yPosition > 0 && yPosition < screenHeight) {
yPosition -= moveDistance;
System.out.println(yPosition);
} else if (yPosition <= 0) {
moveUp = false;
yPosition += moveDistance;
System.out.println(yPosition);
}
} else {
g.setFont(new Font("Arial", Font.ITALIC, fontSizeCurrent));
// fontSizeCurrent-=2;
g.setColor(Color.BLUE);
if (yPosition < screenHeight) {
yPosition += moveDistance;
System.out.println(yPosition);
} else if (yPosition >= screenHeight) {
moveUp = true;
yPosition -= moveDistance;
System.out.println(yPosition);
}
} g.drawString(message, xPosition, yPosition);
System.out.println(xPosition + "," + yPosition);
} public void start() {
try {
if (this.animation == null) {
System.out.println("animation==null");
this.animation = new Thread(this);
System.out.println("new animation ");
this.animation.start();
System.out.println(" animation start");
} else {
this.animation.start();
System.out.println("animation!=null");
System.out.println(" animation start");
}
} catch (Exception ex) {
System.out.println("error:" + ex);
}
} public void run() { try {
while (true) {
Thread.sleep(frameDelay);
System.out.println("sleep " + frameDelay);
repaint();
System.out.println("try repaint();");
// this.animation.sleep(frameDelay);
/* Thread.sleep(frameDelay);
System.out.println("sleep " + frameDelay);*/
}
} catch (InterruptedException ex) {
System.out.println("error" + ex);
} } public boolean mouseDown(Event e, int x, int y) {
// if(this.animation != null&&this.animation.isAlive()){
// this.stop();
// this.animation=null;
// }
yPosition = screenHeight / 2;
this.repaint();
moveUp = true;
fontSizeCurrent = 30;
return true;
} public void update(Graphics g) {
paint(g);
System.out.println("update");
} /**
* @param args
*/
public static void main(String[] args) {
new MainFrame(new SimpleJavaApplet(), 400, 400);
}
}