我在j2me尝试过图片幻灯片。下面是我的图片幻灯片代码。但它会执行图像不会幻灯片显示而不会显示。
如果我点击播放按钮,图像将自动从图像0.jpg幻灯片播放到图像7.jpg。
幻灯片的这个变量
String[] Foto={"/0.jpg", "/1.jpg","/2.jpg", "/3.jpg","/4.jpg", "/5.jpg","/6.jpg", "/7.jpg"};
Image[] img = new Image[8];
和这个循环显示图像..
protected void paint(Graphics g) {
try {
g.setColor(255, 0, 0 );
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(255,0, 0);
g.drawString(text,getWidth()/2, getHeight()/2,
Graphics.TOP | Graphics.HCENTER);
g.setColor(0, 255, 0);
g.setColor(123,0,255);
g.drawRect(74,74, 100,100 );
g.drawRect(72,72, 104,104 );
for ( int i=0;i<8;i++)
if (i==1)
if (i==2)
img[i]=Image.createImage(Foto[i]);
g.drawImage(img [1],124,124, Graphics.VCENTER | Graphics.HCENTER );
g.drawImage(img [2],124,124, Graphics.VCENTER | Graphics.HCENTER );
g.drawImage(img [1],124,124, Graphics.VCENTER | Graphics.HCENTER );
} catch (IOException ex) {
ex.printStackTrace();
}}
答案 0 :(得分:0)
你在Paint()中有一个循环,它将在画布上逐个绘制图像。尝试全局变量currentIndex = 0
protected void paint(Graphics g) {
try {
g.setColor(255, 0, 0 );
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(255,0, 0);
g.drawString(text,getWidth()/2, getHeight()/2,
Graphics.TOP | Graphics.HCENTER);
g.setColor(0, 255, 0);
g.setColor(123,0,255);
g.drawRect(74,74, 100,100 );
g.drawRect(72,72, 104,104 );
img = Image.createImage(Foto[currentIndex]);
g.drawImage(img,124,124, Graphics.VCENTER | Graphics.HCENTER );
} catch (IOException ex) {
ex.printStackTrace();
}}
点击你的按钮,你可以尝试
currentIndex++;
repaint();
如果您想要动画,可以在重绘图像时更改x或y