Java Graphics Tutorial

Java Graphics Tutorial

Java Graphics Tutorial

Summary: In this tutorial "Java Graphics Tutorial", you will come to know of Java graphics concepts like frame and its coordinate system, Graphics class etc. This understanding is very necessary for future programs.

The term Graphics is derived from the Latin word Graphicus. Graphics involves drawing various geometrical figures like rectangles, polygons, pictures like buildings and human beings etc. In computer terminology, Graphics also includes devices like laser printers, plotter, graphics board etc.

Regarding hardware requirements, as graphics uses large RAM, it is suggested to have good amount of RAM, VGA (Video Graphics Array) adapter and high resolution monitor.

In this tutorial, Graphics study involves drawing geometrical figures (drawing of images is given separately) and all the classes required to draw are included in the package java.awt in Java API. AWT is an acronym for Abstract Window Toolkit.

Graphics – Required classes

Four classes are used mainly in graphics drawing – Graphics, Frame, Color and Font. They are discussed hereunder.

class Graphics

The graphics is represented by the class java.awt.Graphics. It is an abstract class and includes many methods, used by the programmer, to draw graphics like arcs, ovals etc. presenting text with good colors and fonts with Color class and Font class and also images required for animation. Graphics object is passed as parameter to the paint() method.

class Frame

We require special drawing area for drawing graphics as DOS prompt does not support graphics. For this, we must request the OS to allocate some space on the monitor. The rectangular space requested is known as frame. The frame size is given in terms of width and height in pixels.

The drawing area is known as graphics context. Any figure drawn should fit into this area, else, not visible. The frame is represented by the class java.awt.Frame. The frame created comes by default with a title bar with three icons. The icons include Minimize, Maximize and Close. Close icon does not work by default and requires extra special code.

Following is the class signature

public class Frame extends Window implements MenuContainer

Three important methods of Frame class (inherited from it's super class Component), used to create frame, are setTitle(), setSize() and setVisible(). Any Java class we write to draw graphics should extend Frame class to inherit these methods.

setTitle(String) displays the title on the title bar. This is method is optional and if not used, no title appears. setSize(int width, int height) specifies the size of the frame in pixels. setSize() method creates a frame and adds to the monitor but not visible by default. To make it visible call setVisible(true) method. These three methods are used by every graphics application. The other methods are setBackground() and setLayout() etc.

On the title bar, by default, Java Coffee cup appears. It can be changed to place our own icon, Frame Icon.

Frame Coordinate Structure

The frame coordinate system is very different from our elementary mathematics. The top-left coordinates of the frame are (0, 0), representing the x and y coordinates. The x value increases from left to right and the y value increases from top to bottom.

Java Graphics Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *