The drawRect()
method draws an open rectangle, a box
if you prefer. If you want to draw a filled rectangle, use the
fillRect()
method. Otherwise the syntax is identical.
This program draws a filled square in the center of the applet. This requires you to separate the applet width and height from the rectangle width and height. Here's the code:
import java.applet.*;
import java.awt.*;
public class FillAndCenter extends Applet {
public void paint(Graphics g) {
int appletHeight = this.getSize().height;
int appletWidth = this.getSize().width;
int rectHeight = appletHeight/3;
int rectWidth = appletWidth/3;
int rectTop = (appletHeight - rectHeight)/2;
int rectLeft = (appletWidth - rectWidth)/2;
g.fillRect(rectLeft, rectTop, rectWidth-1, rectHeight-1);
}
}