What is Graphics.fill3DRect() method?
Overview
In this shot, we’ll learn about the fill3DRect() method. It draws a 3D-filled rectangle with specified parameters and fills it with the current color.
This method is provided by the Graphics class present in the java.awt package.
Syntax
fill3DRect(x, y, width, height, boolean)
Parameters
The method accepts the following parameters:
x: Thexcoordinate of the rectangle.y: Theycoordinate of the rectangle.width: Thewidthof the rectangle.height: Theheightof the rectangle.boolean: Determines whether the rectangle to be drawn appears above or below the surface.
Example
Let’s look at an example below:
//import required packages
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel {
public void paint(Graphics g) {
//set color to graphics object
g.setColor (Color.gray);
//fill raised rectangle
g.fill3DRect (25, 10, 50, 75, true);
//fill sinked rectangle
g.fill3DRect (25, 110, 50, 75, false);
}
public static void main(String[] args) {
//Instantiate JFrame
JFrame frame = new JFrame();
//add panel
frame.getContentPane().add(new Main());
//Will exit the program when you close the gui
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set width and height of the frame
frame.setSize(400, 400);
//set visibility of the frame
frame.setVisible(true);
}
}Using the Graphics.fill3DRect() method
Explanation
In the code above:
- Line 12: We set the graphics color as
gray. - Line 15: We draw a raised rectangle with width
50, height75, and25asxcoordinate, and10asycoordinate and fill it with the current colorgray. - Line 18: We draw a sunk rectangle and fill it with the current color
gray.