java中applet的问题,谢谢指教啊!!!总是启动:未初始化小程序
importjava.awt.*;importjava.applet.*;importjava.awt.image.*;importjava.awt.event.*;im...
import java.awt.*;
import java.applet.*;
import java.awt.image.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
public class TurnPicture extends JApplet
{
private Thread runner;
private Image image[];
private BufferedImage showImage;
private Graphics2D showG;
private int currentImage;
private int imageCount;
private int imageWidth;
private int imageHeight;
private String imageName;
private ImagePanel p;
public void init()
{
try
{
String param=getParameter("imagecount");
if (param!=null)
imageCount=Integer.parseInt(param);
else
throw new Exception();
image=new Image[imageCount];
for (int i=0;i<imageCount ;i++ )
{
imageName=getParameter("image"+(i+1));
if (imageName==null)
throw new Exception();
loadImage(i);
}
}
catch (Exception e)
{
showStatus("Error:"+e);
}
imageWidth=image[0].getWidth(this);
imageHeight=image[0].getHeight(this);
showImage=new BufferedImage(imageWidth,imageHeight,BufferedImage.TYPE_INT_RGB);
showG=showImage.createGraphics();
showG.drawImage(image[0],0,0,null);
p=new ImagePanel();
setContentPane(p);
runner=new Thread(p);
runner.start();
}
public void loadImage(int i)
{
try
{
URL url=new URL(getDocumentBase(),imageName);
image[i]=getImage(url);
MediaTracker tracker=new MediaTracker(this);
tracker.addImage(image[1],0);
tracker.waitForID(0);
}
catch (InterruptedException e)
{
showStatus("Loading interrupted");
}
catch (MalformedURLException e)
{
showStatus("Bad URL");
}
}
public void start()
{
if (runner==null)
{
runner=new Thread(p);
runner.start();
}
}
public void stop()
{
runner.interrupt();
runner=null;
}
public class ImagePanel extends JPanel implements Runnable
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(showImage==null)return;
g.drawImage(showImage,0,0,null);
}
public void run()
{
while (!Thread.interrupted())
{
try
{
Thread.sleep(1000);
for (int i=0;i<imageWidth/2;i++ )
{
runner.sleep(30);
showG.setColor(Color.white);
showG.fillRect(0,0,imageWidth,imageHeight);
showG.drawImage(image[currentImage],i,0,imageWidth-2*i,imageHeight,null);
repaint();
}
currentImage=(currentImage+1)%imageCount;
for (int i=0;i<imageWidth/2 ;i++ )
{
runner.sleep(30);
showG.setColor(Color.white);
showG.fillRect(0,0,imageWidth,imageHeight);
showG.drawImage(image[currentImage],(imageWidth/2)-i,0,2*i,imageHeight,null);
repaint();
}
}
catch (InterruptedException e)
{
}
}
}
}
} 展开
import java.applet.*;
import java.awt.image.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
public class TurnPicture extends JApplet
{
private Thread runner;
private Image image[];
private BufferedImage showImage;
private Graphics2D showG;
private int currentImage;
private int imageCount;
private int imageWidth;
private int imageHeight;
private String imageName;
private ImagePanel p;
public void init()
{
try
{
String param=getParameter("imagecount");
if (param!=null)
imageCount=Integer.parseInt(param);
else
throw new Exception();
image=new Image[imageCount];
for (int i=0;i<imageCount ;i++ )
{
imageName=getParameter("image"+(i+1));
if (imageName==null)
throw new Exception();
loadImage(i);
}
}
catch (Exception e)
{
showStatus("Error:"+e);
}
imageWidth=image[0].getWidth(this);
imageHeight=image[0].getHeight(this);
showImage=new BufferedImage(imageWidth,imageHeight,BufferedImage.TYPE_INT_RGB);
showG=showImage.createGraphics();
showG.drawImage(image[0],0,0,null);
p=new ImagePanel();
setContentPane(p);
runner=new Thread(p);
runner.start();
}
public void loadImage(int i)
{
try
{
URL url=new URL(getDocumentBase(),imageName);
image[i]=getImage(url);
MediaTracker tracker=new MediaTracker(this);
tracker.addImage(image[1],0);
tracker.waitForID(0);
}
catch (InterruptedException e)
{
showStatus("Loading interrupted");
}
catch (MalformedURLException e)
{
showStatus("Bad URL");
}
}
public void start()
{
if (runner==null)
{
runner=new Thread(p);
runner.start();
}
}
public void stop()
{
runner.interrupt();
runner=null;
}
public class ImagePanel extends JPanel implements Runnable
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(showImage==null)return;
g.drawImage(showImage,0,0,null);
}
public void run()
{
while (!Thread.interrupted())
{
try
{
Thread.sleep(1000);
for (int i=0;i<imageWidth/2;i++ )
{
runner.sleep(30);
showG.setColor(Color.white);
showG.fillRect(0,0,imageWidth,imageHeight);
showG.drawImage(image[currentImage],i,0,imageWidth-2*i,imageHeight,null);
repaint();
}
currentImage=(currentImage+1)%imageCount;
for (int i=0;i<imageWidth/2 ;i++ )
{
runner.sleep(30);
showG.setColor(Color.white);
showG.fillRect(0,0,imageWidth,imageHeight);
showG.drawImage(image[currentImage],(imageWidth/2)-i,0,2*i,imageHeight,null);
repaint();
}
}
catch (InterruptedException e)
{
}
}
}
}
} 展开
3个回答
展开全部
去你调试了,出现空指针异常。
java.lang.NullPointerException
at BaiDu.TurnPicture.init(TurnPicture.java:52)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
错误的原因我帮你找出来了。
在init()函数中,imageWidth=image[0].getWidth(this);
imageHeight=image[0].getHeight(this);
这两名出现的空指针异常,原因是image[0]跟本就没有被初始化。
String param=getParameter("imagecount");
if (param!=null)
imageCount=Integer.parseInt(param);
else
throw new Exception();
image=new Image[imageCount];
for (int i=0;i<imageCount ;i++ )
{
imageName=getParameter("image"+(i+1));
if (imageName==null)
throw new Exception();
loadImage(i);
}
}
catch (Exception e)
{
这里面 imageCount 值为0。所以你的图像数组没有初始化成功。 具体的原因,你自己找一找为什么没有成功。
java.lang.NullPointerException
at BaiDu.TurnPicture.init(TurnPicture.java:52)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
错误的原因我帮你找出来了。
在init()函数中,imageWidth=image[0].getWidth(this);
imageHeight=image[0].getHeight(this);
这两名出现的空指针异常,原因是image[0]跟本就没有被初始化。
String param=getParameter("imagecount");
if (param!=null)
imageCount=Integer.parseInt(param);
else
throw new Exception();
image=new Image[imageCount];
for (int i=0;i<imageCount ;i++ )
{
imageName=getParameter("image"+(i+1));
if (imageName==null)
throw new Exception();
loadImage(i);
}
}
catch (Exception e)
{
这里面 imageCount 值为0。所以你的图像数组没有初始化成功。 具体的原因,你自己找一找为什么没有成功。
展开全部
你程序要在浏览器运行,要传一个imagecount给它才可以啊 或者改成这样也可以运行,
String param = getParameter("imagecount");
param = "5";
imageWidth = 400;//image[0].getWidth(this);
imageHeight = 600;//image[0].getHeight(this);
String param = getParameter("imagecount");
param = "5";
imageWidth = 400;//image[0].getWidth(this);
imageHeight = 600;//image[0].getHeight(this);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
iojoijjopjkp[pl[][]
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询