rizard
发贴: 0
积分: 0
|
于 2003-11-03 20:41
这是一个显示多幅(10幅以上)图片的Applet。为何在程序中,有的图片有时显示不了,还出现内存溢出错误(outOfmemory)呢? 望高手能答复!!!!!(急需) package proj_mediatracker;
import java.awt.*; import java.awt.event.*; import java.applet.*; import java.awt.image.*;
public class MyApplet extends Applet implements Runnable { private boolean isStandalone = false; int imgWidth = 100; int imgHeight = 75; Image[] imgs; Image pic; int picID; MediaTracker tracker; Thread loadingThread; //Get a parameter value public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); }
//Construct the applet public MyApplet() { }
//Initialize the applet public void init() { imgs = new Image[16]; tracker = new MediaTracker(this); for (int i = 0; i < imgs.length; i++) { imgs[i] = this.getImage(getCodeBase(),"./imgs/" + i + ".jpg"); tracker.addImage(imgs[i], i); } loadingThread = new Thread(this); loadingThread.start(); try { jbInit(); } catch (Exception e) { e.printStackTrace(); } }
public void run() { try { tracker.waitForAll(); } catch (InterruptedException e) { System.out.println("InterruptedException occurred"); return; } repaint(); }
public void paint(Graphics g) { Graphics2D g2D=(Graphics2D) g;//强制转换 BufferedImage bimage = (BufferedImage)this.createImage(this.getWidth(),this.getHeight()); for(int i = 0; i < imgs.length*4; i++) { bimage.createGraphics().drawImage(this.imgs[i % 16],i * imgWidth % 800,i * imgWidth / 800 % 600 * imgHeight,imgWidth,imgHeight,this); g2D.drawImage(bimage,0,0,this); bimage.flush(); } } public void update(Graphics g){ paint; }
//Component initialization private void jbInit() throws Exception { }
//Get Applet information public String getAppletInfo() { return "Applet Information"; }
//Get parameter info public String[][] getParameterInfo() { return null; } }
rizard edited on 2003-11-03 20:45
|