Java Graphics Class Example: Master Visual Coding Fast

Robert Jul 13, 2026

Graphical user interfaces are integral in Java application development and the Java Graphics class is a pivotal component in creating and manipulating visuals in Java. This class provides a wide range of functionality, from drawing shapes to creating GUI components. Let's delve into this class with an example that demonstrates its capabilities.

a diagram showing the structure of an object in which it appears to be connected with other objects
a diagram showing the structure of an object in which it appears to be connected with other objects

The Java Graphics class comes under the package java.awt and extends from the Component class. It allows Java developers to draw images, text, and shapes on various Java components like Applet, Frame, Panel, and Canvas. Here's a simple example illustrating the usage of Java Graphics class:

Java Scanner Class Made Easy: Your Ultimate Input Cheat Sheet 🎯💻
Java Scanner Class Made Easy: Your Ultimate Input Cheat Sheet 🎯💻

Java Graphics: Drawing Shapes

The Java Graphics class has numerous methods to draw different types of shapes. Let's explore a few:

13.1 JavaFX | Introducing JavaFX - The Revisionist
13.1 JavaFX | Introducing JavaFX - The Revisionist

Drawing a Rectangle

The drawRect(int x, int y, int width, int height) method can be used to draw a rectangle. The parameters define the starting coordinate (x, y) and the dimensions (width, height) of the rectangle.

Class and Object in Java Explained | OOP Concepts for Beginners
Class and Object in Java Explained | OOP Concepts for Beginners

Here's an example of drawing a blue rectangle:

```java Graphics g = getGraphics(); g.setColor(Color.blue); g.drawRect(50, 50, 100, 100); ```

Drawing an Oval

The drawOval(int x, int y, int width, int height) method draws an oval. The parameters are similar to drawRect, defining the start coordinate and the oval's dimensions.

Java Cheat Sheet: Operators, Scanner & User Input ☕
Java Cheat Sheet: Operators, Scanner & User Input ☕

Here's an example of drawing a red oval:

```java g.setColor(Color.red); g.drawOval(200, 200, 100, 50); ```

Java Graphics: Drawing Text

The Graphics class also allows us to draw text on the screen using the drawString(String text, int x, int y) method. The parameters specify the text to be drawn and its location (x, y).

Java infographic made for my IT finals. Java History Infographic, Java Programming Language Infographic, Java Programming Specs Guide, Java Programming Tutorial, Learn Java Basics, Computer Science Infographic, Go Vs Java Infographic, Java Final Meaning Infographic, Java Programming Design Tips
Java infographic made for my IT finals. Java History Infographic, Java Programming Language Infographic, Java Programming Specs Guide, Java Programming Tutorial, Learn Java Basics, Computer Science Infographic, Go Vs Java Infographic, Java Final Meaning Infographic, Java Programming Design Tips

Drawing Text

Let's write "Hello, Java!" to the screen with black color:

Encapsulation in Java, Real Life Example of Encapsulation in Java
Encapsulation in Java, Real Life Example of Encapsulation in Java
java programs
java programs
Abstract Class in Java (Quick Guide)
Abstract Class in Java (Quick Guide)
Check out This Example Java Code for Building a Simple GUI Application
Check out This Example Java Code for Building a Simple GUI Application
java programs
java programs
What is Abstract Class and Method in Java? Examples and Frequently asked Questions
What is Abstract Class and Method in Java? Examples and Frequently asked Questions
Streaming over Maps with Java 8 - Java Code Geeks
Streaming over Maps with Java 8 - Java Code Geeks
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
Java Input Output Cheat Sheet 💻🔥 | Beginner Friendly
Java Input Output Cheat Sheet 💻🔥 | Beginner Friendly
Complete Java Cheat Sheet for Beginners to Advanced Developers
Complete Java Cheat Sheet for Beginners to Advanced Developers
Java OOP concepts cheat sheet
Java OOP concepts cheat sheet
concept of java #java  #javanotes
concept of java #java #javanotes
Java Cheatsheet
Java Cheatsheet
Grow Your Career in Java Development After 12th
Grow Your Career in Java Development After 12th
Logical Operators in Java: Think Smarter, Code Better ☕🚀
Logical Operators in Java: Think Smarter, Code Better ☕🚀
http://chatgpt.com/g/g-lvtjtj09i-darkhorse-dns
http://chatgpt.com/g/g-lvtjtj09i-darkhorse-dns
java
java
Features Of Java
Features Of Java
Object in java
Object in java
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '

```java g.setColor(Color.black); g.drawString("Hello, Java!", 100, 100); ```

Java Graphics: Further Functionality

Java Graphics offers more functionality like filling shapes, drawing lines, and manipulating images. It also provides methods to manipulate graphics context, such as setting colors, fonts, and line widths.

Understanding and utilizing these features in Java Graphics can greatly enhance the visual appeal of your Java applications. The above examples only scratch the surface of what's possible with the Java Graphics class. Explore the JavaDoc or experiment with the numerous other methods available to unlock its full potential.

So, go ahead and start drawing! Happy coding, and remember to keep iterating and learning to up your Java development skills.