A rectangle can be drawn by using the drawRect () method. This method also takes the four parameters.
The general form of the drawRect () method is
void drawRect(int al, int bl, int w, int h)
where,
a1, b1 is the coordinate of the top left comer of the rectangle
w is the width of the rectangle
h is the height of the rectangle
For example, the statement g.drawRect (20 , 20 , 50 , 30 ) will draw a rectangle starting at (20, 20) with width of 50 pixels and height of 30 pixels as shown in Figure
Example : Draw Rectangle using the drawRect () method.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class shapeRectangle extends Applet
{
public static void main(String[] args)
{
Frame DrawingApplet = new Frame(“Draw Rectangle using the drawRect () method.”);
DrawingApplet.setSize(350, 250);
Applet shapeRectangle = new shapeRectangle();
DrawingApplet.add(shapeRectangle);
DrawingApplet.setVisible(true);
DrawingApplet.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0); }
});
}
public void paint(Graphics g)
{
setBackground(Color.yellow);
g.setColor(Color.blue); // Now we tell g to change the color
g.setFont(new Font(“Arial”,Font.BOLD,14)); // Now we tell g to change the font
g.drawString(“Draw Rectangle using the drawRect () method”, 10, 100);
// draws a Rectangle
g.setColor(Color.black);
g.drawRect(120, 50, 100, 100);
}
}
Note that the drawRect () method draws only the boundary of the rectangle. To draw a solid (filled) rectangle, fillRect () method is used. This method also takes four parameters similar to the drawRect () method.
To draw a solid rectangle having same parameters as above we use the statement g.fillRect (20 , 20 , 50, 30) which draws the rectangle as shown in Figure
Exampel: Draw Solid Rectangle using the fillRect () method
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class shapefillRectangle extends Applet
{
public static void main(String[] args)
{
Frame DrawingApplet = new Frame(“Draw Solid Rectangle using the fillRect () method.”);
DrawingApplet.setSize(370, 250);
Applet shapefillRectangle = new shapefillRectangle();
DrawingApplet.add(shapefillRectangle);
DrawingApplet.setVisible(true);
DrawingApplet.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0); }
});
}
public void paint(Graphics g)
{
setBackground(Color.yellow);
g.setColor(Color.blue); // Now we tell g to change the color
g.setFont(new Font(“Arial”,Font.BOLD,14)); // Now we tell g to change the font
g.drawString(“Draw Solid Rectangle using the fillRect () method”, 5, 20);
// draws a Rectangle
g.setColor(Color.black);
g.fillRect(120, 50, 100, 100);
}
}
A rounded outlined rectangle can be drawn by using the drawRoundRect () method. This method takes the six parameters.
The general form of the drawRoundRect () method is:
void drawRoundRect (int al, int bl, int w, int h, int xdia, int ydia)
where,
xdia is the diameter of the rounding arc (along X-axis)
ydia is the diameter of the rounding arc (along Y-axis)
Similarly, a rounded filled rectangle can be drawn using drawfillRoundRect () method. This method also takes six parameters similar to the drawRoundRect () method.
Example : Draw Rounded Rectangle using the drawRoundRect () method.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class shapeRoundRectangle extends Applet
{
public static void main(String[] args)
{
Frame DrawingApplet = new Frame(“Draw Rounded Rectangle using the drawRoundRect () method.”);
DrawingApplet.setSize(410, 250);
Applet shapeRoundRectangle = new shapeRoundRectangle();
DrawingApplet.add(shapeRoundRectangle);
DrawingApplet.setVisible(true);
DrawingApplet.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0); }
});
}
public void paint(Graphics g)
{
setBackground(Color.yellow);
g.setColor(Color.blue); // Now we tell g to change the color
g.setFont(new Font(“Arial”,Font.BOLD,14)); // Now we tell g to change the font
g.drawString(“Draw Rounded Rectangle using the drawRoundRect ()”, 5, 20);
// draws a Rectangle
g.setColor(Color.black);
g.drawRoundRect(150, 50, 100, 100, 25, 50);
}
}
Note: All the shapes are drawn relative to the Java coordinate system. The origin (0, 0) of the coordinate system is located at its upper-left corner such that the positive x values are to its right and the positive y values are to its bottom.