
384 • Java Programming
The method draws a line from (x1, y1) to (x2, y2). It can be used as:
g.drawLine(10,10,50,10);
For drawing rectangle, the Graphics class provides method as:
void drawRect(int top, int left, int width, int bottom);
For example, g.drawRect(20, 20, 120, 50);
The method draws a rectangle whose top left coordinate is given as (20, 20), width 120 and height 50.
The following is another example:
int w = this.getSize().width-1;
int h = this.getSize().height-1;
g.drawRect(0, 0, w, h);
Remember that getSize(). width is the width of the applet and getSize(). height is the height.
The upper left-hand corner ...