Wednesday, December 12, 2012
Memperoleh ukuran layar ponsel pada JAVA (J2ME)
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class UkuranLayar extends MIDlet
implements CommandListener {
private Display display;
private CurrentScreen canvas;
private final Command cmdKeluar =
new Command("Keluar", Command.EXIT, 1);
public UkuranLayar() {
display = Display.getDisplay(this);
canvas = new CurrentScreen();
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 CurrentScreen extends Canvas {
public void paint(Graphics g) {
int width = getWidth();
int height = getHeight();
// membersihkan layar dengan warna putih
g.setColor(0xffffff);
g.fillRect(0,0, width, height);
Font font = g.getFont();
int fontHeight = font.getHeight();
String s = "Ukuran layar: " + width + " x " + height;
int fontWidth = font.stringWidth(s);
g.setColor(0,0,0);
g.setFont(font);
g.drawString(s, (width - fontWidth) / 2,
(height - fontHeight) / 2,
g.TOP | g.LEFT);
}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment