Java Tutorial/J2ME/Graphics
Версия от 17:44, 31 мая 2010; (обсуждение)
Содержание
Change stroke style between drawing
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 RectMIDlet extends MIDlet implements CommandListener {
private Command exitCommand;
Display display;
public void startApp() {
Display display = Display.getDisplay(this);
Displayable d = new RectCanvas();
exitCommand = new Command("Exit", Command.EXIT, 1);
d.addCommand(exitCommand);
d.setCommandListener(this);
display.setCurrent(d);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
notifyDestroyed();
}
}
class RectCanvas extends Canvas {
int width = 0;
int height = 0;
public void paint(Graphics g) {
width = getWidth();
height = getHeight();
g.setGrayScale(255);
g.fillRect(0, 0, width - 1, height - 1);
g.setGrayScale(0);
g.drawRect(0, 0, width - 1, height - 1);
g.drawRect(5, 5, 40, 40);
g.fillRoundRect(25, 25, 40, 40, 15, 15);
}
}
Get stroke style
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 SizeMIDlet extends MIDlet implements CommandListener {
private Command exitCommand;
Display display;
public void startApp() {
Display display = Display.getDisplay(this);
Displayable d = new SizeCanvas();
exitCommand = new Command("Exit", Command.EXIT, 1);
d.addCommand(exitCommand);
d.setCommandListener(this);
display.setCurrent(d);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
notifyDestroyed();
}
}
class SizeCanvas extends Canvas {
int width = 0;
int height = 0;
public void paint(Graphics g) {
width = getWidth();
height = getHeight();
g.setGrayScale(255);
g.fillRect(0, 0, width - 1, height - 1);
g.setGrayScale(0);
g.drawString("Stroke Style:" + g.getStrokeStyle(), 0, 60, Graphics.TOP | Graphics.LEFT);
g.drawRect(0, 0, width - 1, height - 1);
g.setStrokeStyle(Graphics.DOTTED);
g.drawLine(00, 40, 60, 60);
}
}
Set color
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class SetColorGraphicsMIDlet 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(0xFFFF00);
g.drawLine(0, height / 4, width - 1, height / 4);
}
}
Set color and gray scale
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class DrawRectGraphicsMIDlet 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.setGrayScale(100);
g.drawRect(width/4, 0, width/2, height/4);
}
}
Set Gray style
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class SetGrayScaleGraphicsMIDlet 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.setGrayScale(100);
g.drawLine(0, 2, width - 1, height / 2);
}
}
Set stroke style
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class SetStrokeStyleGraphicsMIDlet 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.DOTTED);
g.drawLine(0, height / 2, width - 1, height / 2);
}
}
Translate coordinate
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 OrigTransMIDlet extends MIDlet implements CommandListener {
private Command exitCommand;
Display display;
public void startApp() {
Display display = Display.getDisplay(this);
Displayable d = new OrigTransCanvas();
exitCommand = new Command("exit", Command.EXIT, 1);
d.addCommand(exitCommand);
d.setCommandListener(this);
display.setCurrent(d);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
notifyDestroyed();
}
}
class OrigTransCanvas extends Canvas {
int width = 0;
int height = 0;
public void paint(Graphics g) {
width = getWidth();
height = getHeight();
g.setGrayScale(255);
g.fillRect(0, 0, width - 1, height - 1);
g.setGrayScale(0);
g.drawRect(0, 0, width - 1, height - 1);
int counts = 5;
int step = width / 5;
for (int i = 1; i <= 5; i++) {
g.fillRect(0, 0, step, step);
g.translate(step, step);
}
}
}