GAMES LESSON FIVE: Loading Images

While waiting for pictures to load it is a good idea to display a message informing the user that pictures are being loaded. This applet displays such a message. Notice that the pictures are not all loaded in the init method. Instead they are loaded in the run method allowing the paint method to be called so that the loading message can be displayed.

PIC_DISPLAY.java

import java.awt.*; import java.applet.*; import javax.swing.ImageIcon; public class PIC_DISPLAY extends Applet implements Runnable{ Image pics[] = new Image[4]; String titles[] = { "eagle.jpg", "stahl.jpg", "huckleberry.jpg", "jack.jpg" }; Dimension d; boolean iload=false; Image offI; int pnum, inum; Thread tt; public void init(){ d = getSize(); offI=createImage(d.width,d.height); tt = new Thread(this); tt.start(); } public void loadImage(int c){ // pics[c] = new ImageIcon("images/" + titles[c]).getImage(); pics[c] = getImage(getDocumentBase(), "images/"+titles[c]); } public void run(){ while(true){ repaint(); if(iload==false){ loadImage(inum); inum++; if(inum>3) iload=true; } else{ pnum=(pnum+1)%pics.length; } try{ Thread.sleep(2000); }catch(InterruptedException ie){ } } } public void update(Graphics g){ paint(g); } public void paint(Graphics g){ Graphics offG = offI.getGraphics(); offG.setColor(Color.yellow); offG.fillRect(0,0,d.width,d.height); if(iload){ offG.drawImage(pics[pnum],0,0, null); } else{ offG.setColor(Color.blue); offG.drawString("Loading images..." + inum, 30, 100); } g.drawImage(offI,0,0,this); } }


ASSIGNMENT: