Java Tutorial/J2ME/Rectangle
Версия от 17:44, 31 мая 2010; (обсуждение)
Содержание
Draw rectangle
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class DrawRectGraphicsMIDletStrokeStyle extends MIDlet {
private Display display;
protected void startApp() {
Canvas canvas = new LineCanvas();
display = Display.getDisplay(this);
display.setCurrent(canvas);
}
protected void pauseApp() {
}
protected void destroyApp(boolean unconditional) {
}
}
class LineCanvas extends Canvas {
public void paint(Graphics g) {
int width = getWidth();
int height = getHeight();
g.setColor(0);
g.setStrokeStyle(Graphics.SOLID);
g.drawRect(width/4, 0, width/2, height/4);
}
}
Fill a rectangle, and a round rectangle
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class J2MEFilledRectangleExample extends MIDlet {
private Display display;
private MyCanvas canvas;
public J2MEFilledRectangleExample() {
display = Display.getDisplay(this);
canvas = new MyCanvas(this);
}
protected void startApp() {
display.setCurrent(canvas);
}
protected void pauseApp() {
}
protected void destroyApp(boolean unconditional) {
}
public void exitMIDlet() {
destroyApp(true);
notifyDestroyed();
}
}
class MyCanvas extends Canvas implements CommandListener {
private Command exit;
private J2MEFilledRectangleExample filledRectangleExample;
public MyCanvas(J2MEFilledRectangleExample filledRectangleExample) {
this.filledRectangleExample = filledRectangleExample;
exit = new Command("Exit", Command.EXIT, 1);
addCommand(exit);
setCommandListener(this);
}
protected void paint(Graphics graphics) {
graphics.setColor(255, 255, 255);
graphics.fillRect(0, 0, getWidth(), getHeight());
graphics.setColor(0, 0, 255);
graphics.fillRect(2, 2, 20, 20);
graphics.fillRoundRect(20, 20, 60, 60, 15, 45);
}
public void commandAction(Command command, Displayable displayable) {
if (command == exit) {
filledRectangleExample.exitMIDlet();
}
}
}
Fill rectangle
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class FillRectGraphicsMIDlet extends MIDlet {
private Display display;
protected void startApp() {
Canvas canvas = new LineCanvas();
display = Display.getDisplay(this);
display.setCurrent(canvas);
}
protected void pauseApp() {
}
protected void destroyApp(boolean unconditional) {
}
}
class LineCanvas extends Canvas {
public void paint(Graphics g) {
int width = getWidth();
int height = getHeight();
g.setColor(0);
g.fillRect(0, 0, width, height);
}
}
Fill Round Rectangle
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class J2MEPacer extends MIDlet {
public void startApp() {
Displayable d = new PacerCanvas();
d.addCommand(new Command("Exit", Command.EXIT, 0));
d.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable s) {
notifyDestroyed();
}
});
Display.getDisplay(this).setCurrent(d);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
class PacerCanvas extends Canvas {
public void paint(Graphics g) {
int w = getWidth();
int h = getHeight();
g.setColor(0xffffff);
g.fillRect(0, 0, w, h);
g.setColor(0x000000);
g.drawRect(12, 12, 20, 20);
g.fillRoundRect(33, 33, 20, 20, 5, 5);
g.drawArc(8, 88, 20, 20, 0, 360);
}
}