---
title: "J2ME图片浏览器"
author: "smallearth"
date: "2012-05-26T12:56:00Z"
tags: []
slug: "j2me"
draft: false
lastmod: "2013-11-30T20:10:46Z"
id: 1983333
url: "https://smallearth.iteye.com/blog/1983333"
---

package Mypaint;

import javax.microedition.midlet.\*;  
import javax.microedition.lcdui.\*;  
import javax.microedition.midlet.MIDletStateChangeException;

public class mypaint extends MIDlet {  
private Display d;  
private mycanvas mc;  
public mypaint() {  
d = Display.getDisplay(this);  
mc = new mycanvas(this);  
// TODO Auto-generated constructor stub  
}

protected void destroyApp(boolean arg0) //throws MIDletStateChangeException  
{}

protected void pauseApp() {}

protected void startApp() throws MIDletStateChangeException {  
d.setCurrent(mc);  
}  
public void exitMIDlet()  
{  
destroyApp(true);  
notifyDestroyed();  
}

}

class mycanvas extends Canvas implements CommandListener  
{  
private Image ima\[] = new Image\[11];  
private Command last;  
private Command next;  
mypaint mp;;  
private int x,y,n=0;;  
public mycanvas(mypaint mp)  
{  
try{  
for(int i=1;i&lt;=10;i++)  
{  
ima\[i-1] = Image.createImage("/"+i+".png");  
}  
}catch(Exception e){}  
x = getWidth();  
y = getHeight();  
this.mp = mp;  
last = new Command("上一个",Command.EXIT,1);  
next = new Command("下一个",Command.SCREEN,2);  
addCommand(last);  
addCommand(next);  
setCommandListener(this);  
}  
public void paint(Graphics g)  
{  
g.setColor(255,255,255);  
g.drawRect(0,0,x,y);  
g.drawImage(ima\[n],x/2,y/2,g.HCENTER | g.VCENTER);  
}  
public void commandAction(Command c,Displayable d)  
{  
if(c == last)  
{  
if(n&gt;=1)  
n--;  
else if(n==0)  
n=9;  
repaint();  
}else if(c == next)  
{  
if(n&lt;9)  
n++;  
else if(n==9)  
n=0;  
repaint();  
}  
}  
public void keyPressed(int k)  
{  
switch(getGameAction(k))  
{  
case Canvas.UP:if(n&gt;0)n--;  
break;

case Canvas.DOWN:if(n&lt;9)n++;  
break;

case Canvas.LEFT:if(n&gt;0)n--;  
break;

case Canvas.RIGHT:if(n&lt;9)n++;  
break;  
}  
repaint();

}

}![](http://my.csdn.net/uploads/201205/26/1338008328_7402.png)![](http://my.csdn.net/uploads/201205/26/1338008342_5482.png)![](http://my.csdn.net/uploads/201205/26/1338008349_3266.png)![](http://my.csdn.net/uploads/201205/26/1338008361_6624.png)![](http://my.csdn.net/uploads/201205/26/1338008369_2588.png)![](http://my.csdn.net/uploads/201205/26/1338008374_9780.png)![](http://my.csdn.net/uploads/201205/26/1338008388_8962.png)![](http://my.csdn.net/uploads/201205/26/1338008396_1025.png)![](http://my.csdn.net/uploads/201205/26/1338008411_2542.png)![](http://my.csdn.net/uploads/201205/26/1338008419_7619.png)