java中applet的问题,谢谢指教啊!!!总是启动:未初始化小程序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中applet的问题,谢谢指教啊!!!总是启动:未初始化小程序相关的知识,希望对你有一定的参考价值。
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)
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。所以你的图像数组没有初始化成功。 具体的原因,你自己找一找为什么没有成功。 参考技术A 你程序要在浏览器运行,要传一个imagecount给它才可以啊 或者改成这样也可以运行,
String param = getParameter("imagecount");
param = "5";
imageWidth = 400;//image[0].getWidth(this);
imageHeight = 600;//image[0].getHeight(this); 参考技术B iojoijjopjkp[pl[][]
以上是关于java中applet的问题,谢谢指教啊!!!总是启动:未初始化小程序的主要内容,如果未能解决你的问题,请参考以下文章
请教高手知道怎么把TXT文件转换成BIN文件啊 指教一下 谢谢
JAVA计算器代码p 新手学JAVA 做一个计算器,界面没问题,不能计算求高手指教