import java.awt.*;
import java.applet.Applet;
public class exp8 extends java.applet.Applet implements Runnable
{
Image frame[];
Thread thd;
int num; //设置计算帧数的计数器
int pause; //设置每一帧的显示时间
int p;
public void init(){
String fps;
frame=new Image[17];
thd=null;
num=0;
for(int i=0;i<frame.length;i++)
frame[i]=getImage(getCodeBase(),"images/T"+i+".png");
fps=getParameter("speed");
if(fps==null){
fps="10";
try{
p=Integer.parseInt(fps);
}catch{NumberFormatException e}
pause=1000/p; }
}
public void start(){
if(thd==null){
thd = new Thread(this);
thd.start();
}
}
public void stop(){
if(thd!=null){
thd.stop();
thd=null;
}
}
public void run(){
while(true){
try{Thread.sleep(pause);
}catch(InterruptedException e){}
repaint();
num=(num+1)%frame.length;
}
}
public void paint(Graphics g){
g.drawImage(frame[num], 0, 0, this);
}
public void update(Graphics g){
paint(g);
}
}
上面红色的部分老是有问题,我是想把fps这个字符串类型的值转换成能进行数学运算的整数
import java.applet.Applet;
public class exp8 extends java.applet.Applet implements Runnable
{
Image frame[];
Thread thd;
int num; //设置计算帧数的计数器
int pause; //设置每一帧的显示时间
int p;
public void init(){
String fps;
frame=new Image[17];
thd=null;
num=0;
for(int i=0;i<frame.length;i++)
frame[i]=getImage(getCodeBase(),"images/T"+i+".png");
fps=getParameter("speed");
if(fps==null){
fps="10";
try{
p=Integer.parseInt(fps);
}catch{NumberFormatException e}
pause=1000/p; }
}
public void start(){
if(thd==null){
thd = new Thread(this);
thd.start();
}
}
public void stop(){
if(thd!=null){
thd.stop();
thd=null;
}
}
public void run(){
while(true){
try{Thread.sleep(pause);
}catch(InterruptedException e){}
repaint();
num=(num+1)%frame.length;
}
}
public void paint(Graphics g){
g.drawImage(frame[num], 0, 0, this);
}
public void update(Graphics g){
paint(g);
}
}
上面红色的部分老是有问题,我是想把fps这个字符串类型的值转换成能进行数学运算的整数
应该没什么问题啊,不过lz还在用Applet,一般是不是应该都用JApplet了
if(fps==null){
fps="10";
try{
p=Integer.parseInt(fps);
}catch{NumberFormatException e}
pause=1000/p; }
}
}