Home > Java > javaTutorial > body text

Graphics Class in Java

WBOY
Release: 2024-08-30 15:57:46
Original
551 people have browsed it

It is an abstract class present in java.awt package that extends the Object class of java.lang package that serves as a superclass for all graphics contexts, which allow drawing various components in an application that can be easily realized onto various devices or many real images.

ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Every single object of the Graphics class is a complete package of all the methods needed for the implementation of basic functions of an applet, and with this, its state contains information related to the component on which to draw, current clip, current color, XOR alternation color, font or translations of origin.

Syntax with Parameters

public abstract class Graphics extends Object
Copy after login

Graphics class is an abstract class; thus, we cannot make its objects; instead, we need to use one of its subclasses such as DebugGraphics, Graphics2D. And it is also a public class; it can be accessed using any of the class.

It extends the Object class in java.lang package thus extends all its functions such as clone, equals, etc.

How does Graphic Class work in Java?

Graphics class is used to draw different visualized components on the screen that is considered as a drawing board composed of the infinite number of pixels that lie between the pixels of the output device. All the coordinates that are given as arguments to the function are considered relative to the origin that has been translated before triggering the method. Below is the procedure when we give different points in any of the methods:-

  1. While drawing an outline of a figure, an infinitely thin path is traversed between the pixels and placed a pixel-sized pen at the bottom and right of the anchor points.
  2. While drawing a given rectangle, one extra row at the right and bottom edges are occupied, whereas while filling it, the same boundaries are used to fill the color that has been set using the setColor method.
  3. If one writes a text, the text is written above the base coordinates.
  4. While drawing a baseline for a text, the pixels right below the text are considered to draw the line.

All the operations that can be performed modify the pixels lying within the Shape specified and are controlled using an object of Graphics class. This region is referred to as the user clip that can only be modified using setClip and clipReact methods.

A device clip specifying the main clipping region combined with the above-described user clip defines a composite clip that defines the region for final clipping. All drawings or writings are done in the current color, in the current font, and using the current paint mode.

1. drawRoundRect (int a1, int b1, int width, int height, int horArc, int vertArc )

This function is used to create a rectangle with rounded corners where

a1 – This argument denotes the x coordinate of the rectangle’s top-left corner to be drawn.

b1 – This argument denotes the y coordinate of the rectangle’s top-left corner to be drawn.

width – This argument denotes the width of the rectangle to be drawn.

height – This argument denotes the height of the rectangle to be drawn.

horArc – This argument denotes the horizontal diameter of the arc for all the corners of the rectangle to be drawn.

vertArc – This argument denotes the vertical diameter of the arc for all the corners of the rectangle to be drawn.

The left edge = x and right edge = x+width -1

Top edge = y and bottom edge = y+height -1

2. public abstract void fillRoundRect (int x,int y,int width,int height, int arcWidth,int arcHeight)

This method is used to fill rounded corner rectangle with the color specified as the current color. Parameters interpretation is the same as given in the drawRoundRect() method.

3. public abstract void clipRect (int x, . int y, int width, int height)

This method is used to intersect the current clip with the specifications of the rectangle. In case the current clip area is null, the specified rectangle is set as the new clip that can be modified using setClip methods. These operations do not affect the outside of the clipping area.

4. public void fill3DRect(int a1,int b1, int width,int height,boolean leveled)

This method is used to paint a 3-D highlighted rectangle filled with the color specified using the setColor method. To give a 3D look to the figure, edges will be beveled to some extent and highlighted from the top left corner.

Parameters:

a1 –This argument denotes the x coordinate of the rectangle’s top-left corner to be drawn.

b1 -This argument denotes the y coordinate of the rectangle’s top-left corner to be drawn.

width – This argument denotes the width of the rectangle to be drawn.

height – This argument denotes the height of the rectangle to be drawn.

leveled – a boolean value, if it Is true – rectangle made will be shown as leveled above the surface; otherwise, it will be shown on the same level of the surface.

5. public abstract void drawOval (int a,int b,int width1,int height1)

This method is used to draw the empty oval in the boundaries of the rectangle whose dimensions have been specified. The area of this oval extends upto width+1 pixels and height+1 pixels.

Parameters:

a –This argument denotes the x coordinate of the top left corner of the oval.

b – This argument denotes the y coordinate of the top left corner of the oval.

width1 –This argument denotes the width of the oval.

height1 –This argument denotes the height of the oval.

6. public abstract void setColor (Color)

This method is used to set the current color for the graphics object. It takes the final variable as the value of color from the Color class in java.awt package. All the operations following this line will use this particular color.

Parameters:

c – the new color.

7. public abstract void drawLine (int a1, int b1, int a2, int b2)

This method is used to draw a line, using the current color, between the points (a1, b1) and (a2, b2) in this graphics context’s coordinate system.

Parameters:

a1 –x coordinate of the starting point of the line.

b1 – y coordinate of the starting point of the line

a2 – x coordinate of the ending point of the line.

b2 – y coordinate of the ending point of the line.

Examples of Graphics Class in Java

Different examples are mentioned below:

Example #1

Let’s draw a simple applet in java

Code:

import java.awt.*;
import java.awt.event.*;
import <u>java.awt.geom</u>.*;
public class <u>Demo </u>extends Frame {
public Demo(){
prepareWindow();
}
@Override
public void paint(Graphics g) {
g.setColor(Color.GRAY);
Font currentFont = new Font("Berlin Sans FB Demi",Font.ITALIC, 24);
g.setFont(currentFont);
g.setColor(Color.BLUE);
g.drawString("Welcome to Graphics Class", 100, 150);
g.setColor(Color.GREEN);
g.drawLine(100, 200, 400, 200);
}
public static void main(String[] args){
Demo  awtGraphicsDemo = new Demo();
awtGraphicsDemo.setVisible(true);
}
private void prepareWindow(){
setSize(450,400);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
}
}
Copy after login

Output:

Graphics Class in Java

Example #2

Code:

import java.awt.*;
import java.awt.event.*;
import <u>java.awt.geom</u>.*;
public class <u>Demo </u>extends Frame {
public Demo(){
super("Java AWT Examples");
prepareWindow();
}
public static void main(String[] args){
Demo  awtGraphicsDemo = new Demo();
awtGraphicsDemo.setVisible(true);
}
private void prepareWindow(){
setSize(450,400);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
}
@Override
public void paint(Graphics g) {
g.setColor(Color.GRAY);
Font currentFont = new Font("Berlin Sans FB Demi", Font.ITALIC, 24);
g.setFont(currentFont);
g.drawString("Welcome to Graphics Class", 100, 150);
g.setColor(Color.magenta);
g.drawRoundRect(150, 400, 100, 150, 20, 20);
g.setColor(Color.CYAN);
g.fillRoundRect(400, 400, 150, 120, 20,10);
Font newFont1 = new Font("ALGERIAN", Font.ITALIC, 30);
g.setFont(newFont1);
g.setColor(Color.orange);
g.fill3DRect(600, 400, 500, 120, false);
g.setColor(Color.blue);
g.drawString("Welcome to Graphics Class", 1000, 700);
g.drawOval(600,200,400,100);
g.drawLine(100, 170, 500, 170);
}
}
Copy after login

Output:

Graphics Class in Java

Conclusion

Graphics class provides all the basic operations required to create the visualizing objects on the screen and all information related to its state or font properties and modifying them. However, since it’s an abstract class thus, its instance cannot be created directly, thus called using its subclasses.

The above is the detailed content of Graphics Class in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!