In the Following example AppletDrawRect shows how to Draw Rectangle or Fill Rectangle and draw filled rounded corner rectangle or set foreground color of an Applet window using drawRect,fillRect, drawRoundRect, fillRoundRect method of Graphics class. The Syntax for The Syntax for drawRect(int xTopLeft, int yTopLeft, int width, int height); and The Syntax for fillRect(int xTopLeft, int yTopLeft, int width, int height); and The Syntax for round cornered square drawRoundRect(int xTopLeft, int yTopLeft, int width, int height, int arcWidth, int arcHeight), and The Syntax for fillRoundRect(int xTopLeft, int yTopLeft, int width, int height, int arcWidth, int arcHeight)
Following example demonstrates how to set color’s with RGB values, for this you can use a Color object. For AppletDrawRect example, an RGB(R for Red, G for Green and B
for Blue) value for dark blue Color is 0 red, 0 is green, and 200 is blue. Following example AppletDrawRect shows of an applet that displays dark blue Rectangle .
Here is the java code for the program AppletDrawRect :.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class AppletDrawRect extends Applet
{
public static void main(String[] args)
{
Frame RectApplet = new Frame("Draw Rectangle in Applet Window Example");
RectApplet.setSize(350, 250);
Applet AppletDrawRect = new AppletDrawRect();
RectApplet.add(AppletDrawRect);
RectApplet.setVisible(true);
RectApplet.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);} });
}
public void paint(Graphics g)
{
g.setColor(Color.darkGray);
g.setFont(new Font("Arial",Font.BOLD,14));
g.drawString("Draw Rounded Corner Rectangle", 50, 40);
g.setFont(new Font("Arial",Font.BOLD,10));
g.drawString("http://ecomputernotes.com", 200, 205);
//set color to Blue
Color darkblue = new Color(0, 0, 200);
g.setColor(darkblue);
//this will draw a Rectangle of width 50 & height 70 at (10,50)
//The Syntax for drawRect(int xTopLeft, int yTopLeft, int width, int height);
g.drawRect(10,50,50,70);
//this will Fill Rectangle of width 50 & height 70 at (70,50)
//The Syntax for fillRect(int xTopLeft, int yTopLeft, int width, int height);
g.fillRect(70,50,50,70);
// If you speficy same width 50 and height 50, the drawRect method will draw a square!
//The Syntax for square drawRect(int xTopLeft, int yTopLeft, int width, int height);
g.drawRect(130,50,50,50);
//this will draw a round cornered rectangle of width 50 & height 70 at (10,130)
//The Syntax for round cornered square drawRoundRect(int xTopLeft, int yTopLeft, int width, int height, int arcWidth, int arcHeight)
g.drawRoundRect(10,130,50,70,20,20);
//draw filled rounded corner rectangle
//The Syntax for fillRoundRect(int xTopLeft, int yTopLeft, int width, int height, int arcWidth, int arcHeight)
g.fillRoundRect(70,130,50,70,20,20);
//this will draw a round cornered square
//The Syntax for round cornered square drawRoundRect(int xTopLeft, int yTopLeft, int width, int height, int arcWidth, int arcHeight)
g.drawRoundRect(130,130,50,50,10,10);
}
}