Java Reference
In-Depth Information
Table 13-1. Drawing and Filling Shapes with Graphics
Shape Outline
Filled Shape
drawLine(int x1, int y1, int x2, int y2)
fillTriangle(int x1, int y1, int x2, int y2,
int x3, int y3)
drawRect(int x, int y, int width, int height)
fillRect(int x, int y, int width, int height)
drawRoundRect(int x, int y, int width,
int height, int arcWidth, int arcHeight)
fillRoundRect(int x, int y, int width,
int height, int arcWidth, int arcHeight)
drawArc(int x, int y, int width, int height,
int startAngle, int arcAngle)
fillArc(int x, int y, int width, int height,
int startAngle, int arcAngle)
These methods do basically what you'd expect. The following example demonstrates
some simple drawing using Graphics . It consists of two pieces. First, PacerCanvas demonstrates
some simple drawing and filling:
import javax.microedition.lcdui.*;
public 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);
for (int x = 0; x < w; x += 10)
g.drawLine(0, w - x, x, 0);
int z = 50;
g.drawRect(z, z, 20, 20);
z += 20;
g.fillRoundRect(z, z, 20, 20, 5, 5);
z += 20;
g.drawArc(z, z, 20, 20, 0, 360);
}
}
The next class is Pacer , a MIDlet that uses PacerCanvas .
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
Search WWH ::




Custom Search