Monday, December 10, 2012
Menempatkan gambar di atas objek Canvas, pada JAVA (J2ME)
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.IOException;
public class GambarDalamCanvas extends MIDlet
implements CommandListener {
private Display display;
private CanvasGambar canvas;
private final Command cmdKeluar =
new Command("Keluar", Command.EXIT, 1);
public GambarDalamCanvas() {
display = Display.getDisplay(this);
canvas = new CanvasGambar(this);
canvas.addCommand(cmdKeluar);
canvas.setCommandListener(this);
}
public void startApp() {
display.setCurrent(canvas);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c == cmdKeluar) {
destroyApp(false);
notifyDestroyed();
}
}
//inner class
class CanvasGambar extends Canvas {
private GambarDalamCanvas midlet;
private Image image = null;
public CanvasGambar(GambarDalamCanvas midlet) {
this.midlet = midlet;
try {
image = Image.createImage("Java.png");
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public void paint(Graphics g) {
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0, 0, 255);
g.drawString("Contoh gambar dalam canvas", 3, 0,
g.LEFT | g.TOP);
g.drawImage(image, 3, 20, g.LEFT | g.TOP);
}
};
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment