Drawing Shapes
The Graphics
and Graphics2D
classes provide several methods for drawing different types of shapes and figures. Here are some of the most commonly used methods:
Lines:
drawLine(int x1, int y1, int x2, int y2)
: Draws a line between two points.
Rectangles:
drawRect(int x, int y, int width, int height)
: Draws the outline of a rectangle.fillRect(int x, int y, int width, int height)
: Fills a rectangle with the current color.
Ovals:
drawOval(int x, int y, int width, int height)
: Draws the outline of an oval inscribed in the specified rectangle.fillOval(int x, int y, int width, int height)
: Fills an oval with the current color.
Arcs:
drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
: Draws the outline of an arc.fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
: Fills an arc with the current color.
Polygons:
drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
: Draws the outline of a polygon defined by arrays of x and y coordinates.fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
: Fills a polygon with the current color.
Round Rectangles:
drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
: Draws the outline of a rectangle with rounded corners.fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
: Fills a rectangle with rounded corners.
Example Program to draw various shapes using Graphics
import javax.swing.*;
import java.awt.*;
public class ShapeTest extends JFrame {
public ShapeTest() {
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public void paint(Graphics g) {
g.drawOval(40, 40, 60, 60); // Circle
g.drawRect(80, 30, 200, 200); // Square
g.drawRect(200, 100, 100, 200); // Rectangle
g.drawString("Drawing shapes", 10, 10);
setBackground(Color.WHITE);
g.fillRect(130, 30, 100, 80); // Rectangle
g.drawOval(30, 130, 50, 60); // Circle
setForeground(Color.RED);
g.fillOval(130, 130, 50, 60); // Filled Circle
g.drawArc(30, 200, 40, 50, 90, 60); // Arc
g.fillArc(30, 130, 40, 50, 180, 40); // Filled Arc
}
public static void main(String[] args) {
new ShapeTest();
}
}
Setting Up the JFrame:
The ShapeTest class extends JFrame, which is a window container for GUI components.
In the constructor (ShapeTest()), we set the frame size to 400x400 pixels, set the close operation, and make it visible.
Drawing Shapes:
The paint(Graphics g) method is overridden to handle custom drawing.
We use the Graphics object (g) to draw various shapes:
We use the Graphics object (g) to draw various shapes:
g.drawOval(40, 40, 60, 60);: Draws an oval (circle) with its top-left corner at (40, 40) and dimensions 60x60 pixels.
g.drawRect(80, 30, 200, 200);: Draws a rectangle with its top-left corner at (80, 30) and dimensions 200x200 pixels.
g.drawRect(200, 100, 100, 200);: Draws another rectangle.
g.fillRect(130, 30, 100, 80);: Fills a rectangle with a white background.
g.drawOval(30, 130, 50, 60);: Draws another oval.
g.fillOval(130, 130, 50, 60);: Fills an oval with red color.
g.drawArc(30, 200, 40, 50, 90, 60);: Draws an arc (part of an ellipse).
g.fillArc(30, 130, 40, 50, 180, 40);: Fills an arc with a different angle.
Text:
g.drawString("Drawing shapes", 10, 10);: Displays the text “Drawing shapes” at coordinates (10, 10).
Colors:
We set the background color to white (setBackground(Color.WHITE)).
The foreground color (used for filling shapes) is red (setForeground(Color.RED)).
Main Method:The main method creates an instance of ShapeTest, which displays the shapes in the JFrame.
Example Program Demonstrating Various Shapes using advanced Graphics2D
Here's an example Java Swing program that demonstrates how to draw different shapes using the methods mentioned above:
import javax.swing.*;
import java.awt.*;
public class ShapesExample extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
// Set anti-aliasing for smoother graphics
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Draw a line
g2d.setColor(Color.BLUE);
g2d.drawLine(10, 10, 200, 10);
// Draw a rectangle
g2d.setColor(Color.RED);
g2d.drawRect(10, 30, 100, 50);
g2d.fillRect(120, 30, 100, 50);
// Draw an oval
g2d.setColor(Color.GREEN);
g2d.drawOval(10, 90, 100, 50);
g2d.fillOval(120, 90, 100, 50);
// Draw an arc
g2d.setColor(Color.ORANGE);
g2d.drawArc(10, 150, 100, 100, 0, 180);
g2d.fillArc(120, 150, 100, 100, 0, 180);
// Draw a polygon
int[] xPoints = {10, 60, 110};
int[] yPoints = {300, 250, 300};
g2d.setColor(Color.MAGENTA);
g2d.drawPolygon(xPoints, yPoints, 3);
g2d.fillPolygon(new int[]{120, 170, 220}, new int[]{300, 250, 300}, 3);
// Draw a round rectangle
g2d.setColor(Color.CYAN);
g2d.drawRoundRect(10, 320, 100, 50, 20, 20);
g2d.fillRoundRect(120, 320, 100, 50, 20, 20);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Shapes Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 400);
frame.add(new ShapesExample());
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(ShapesExample::createAndShowGUI);
}
}
Explanation
ShapesExample Panel:
- Extends
JPanel
and overrides thepaintComponent
method. - Uses
Graphics2D
for better control and rendering quality. - Draws various shapes: line, rectangle, oval, arc, polygon, and round rectangle.
- Extends
Running the Application:
- The
createAndShowGUI
method sets up the frame and adds theShapesExample
panel. - The
main
method usesSwingUtilities.invokeLater
to ensure the GUI creation and updates are handled on the Event Dispatch Thread.
- The
Comments
Post a Comment