|
classpath:E:\JBuilder\jdk1.4\lib\tools.jar;E:\JBuilder\jdk1.4\lib\dt.jar;.
Project path: D:\javawork\j2me\SlideshowMidlet
Picture storage location: D:\javawork\j2me\SlideshowMidlet\
The program is written, compiled, and run in JBuilder
When running, throw an exception: Failed loading images!
The source code is as follows: (There are two classes, the running class is not posted)
import javax.microedition.lcdui.*;
import java.io.*;
public class SSCanvas extends Canvas {
private Display display;
private Image [] slides;
private String [] captions = {"A","B","C","D","E"};
private int curSlide = 0;
public SSCanvas(Display d){
super();
display = d;
//The picture loader starts
try {
slides = new Image[5];
slides[0] = Image.createImage("/A.gif");
slides[1] = Image.createImage("/B.gif");
slides[2] = Image.createImage("/C.gif");
slides[3] = Image.createImage("/D.gif");
slides[4] = Image.createImage("/E.gif");
} catch (IOException e) {
System.err.println("Failed loading images!");
}//Picture loading end
}
void start(){
display.setCurrent(this);
repaint();
}
public void keyPressed(int keyCode){
int action =getGameAction(keyCode);
switch(action){
case LEFT:
if(-- curSlide <0 ){
curSlide = slides.length-1;
}
repaint();
break;
case RIGHT:
if(++ curSlide >= slides.length){
curSlide = 0;
}
repaint();
break;
}
}
public void paint(Graphics g){
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
g.drawImage(slides[curSlide],getWidth()/2,getHeight()/2,Graphics.HCENTER | Graphics.VCENTER);
Font f = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_MEDIUM);
g.setFont(f);
g.setColor(0,0,0);
g.drawString(captions[curSlide],getWidth()/2,0,Graphics.HCENTER | Graphics.TOP);
}
} |
|