Table of Contents
What is Java Graphics?
If you are an application developer, you will always have to work with Graphic User Interfaces (GUIs). Graphics make anything more attractive, be it a webpage, an application, or a video game. JAVA also allows users to include graphics and designs in their codes. To perform this action, JAVA has a dedicated class called Graphics. This class extends the Object class. You can import it by using the following code:
The class signature for a graphics class is as follows:
public abstract class Graphics extends Object
Instantiating the Graphics class will produce a graphics object. A graphics object is always required to create any kind of graphics is JAVA. The Graphics object encapsulates the following state information in it as given by Oracle:
- The Component Object on which the drawing will be made
- A translation origin for working with coordinates.
- The current clip
- The current colour
- The current font
- The current logical pixel operation function (XOR or Paint)
- The current XOR alteration colour
How are Shapes Drawn in Java?
The entire Graphics system works on coordinates. The coordinate system bases an origin anywhere on the screen as specified. The screen is thereafter divided into infinitely thin coordinates that lie between the pixels. When an object is to be drawn, the coordinates are followed and coloured in between the pixels. Colouration and filling of figures are also done similarly. As the outline is specified clearly, all the coordinates that reside inside the boundary are coloured.
Constructor and Methods of the Graphics Class
The constructed of Graphics is as follows:
protected Graphics ()
This is the default constructor for a graphics context. Due to the abstract nature of the Graphics class, this constructor cannot be directly called by applications.
There are several methods of the Graphics class. All of these methods serve different purposes. Some of the frequently used methods of the class are:
public abstract void drawImage (….) – To draw lines
public abstract void drawOval (…) – To draw an oval shape
public abstract void drawRect (…) – To draw the outline of a rectangle