Coders Packet

Graphics in JAVA

By KHYATI MADDALI

In this guide, you will learn about the graphics class and the methods you can use to develop applet in Java. You will also see the implementation of some of the methods in graphics.

Before we start, let’s learn what graphics class is, why we should use it and what function it plays. ‘Graphics’ is an abstract class that permits the applet to produce or draw different components. It also implements it in a way that it can be viewed or displayed. An object that belongs to the Graphics class encases all important information or data for the basic functions that Java supports. Primarily, there are two different types of methods in the Graphics class:

  • Draw methods – such as drawImage

  • Fill methods – such as fillOval

There are other methods that help the programmer adjust the attributes. Such methods directly impact how the filling or drawing looks when displayed. we will learn more about these methods while developing the code.

The Graphics class is found in the awt package in Java. It is an essential requirement for developing a java applet as it integrates other classes such as Color, Image, etc to get the desired output.

Now that we have a basic idea about the Graphics class, let’s start coding and try to understand it with an example.

First, let’s import the packages we require to use the Graphics methods and create an applet. Note that, the java.swing.JFrame class is a type of container where functions like buttons, labels, etc are packed to create a GUI.

import java.awt.*;
import javax.swing.JFrame;

next, we have declared a public class named ‘example2’ which inherits the properties of Canvas. Canvas is a module in Java that symbolizes the area where the user input (such as a sketch or drawing) is conceived in the application. Extend is a keyword in Java that denotes extension of the functionality of the class, also known as inheritance in java.

The public void paint(Graphics g)line ensures that the paint() method is executed and implemented when the user decides to draw or paint in the space.

Now, let’s look at the methods in the Graphics class which we have used in the code, line by line.

The drawstring method is used for drawing text. The general syntax is as follows: 

g.drawString (String str, int x, int y);

in this code, we have used the following: 

g.drawString("LET'S LEARN JAVA!",30,30);

In the next step ; as the name suggests, we are using the setBackground() method to set the colour of the background of the applet window. Here, we have set it as white.

setBackground(Color.WHITE);

the next method we are using in the Graphics class is the fillRect method. This method is used to fill the rectangular shape with a color of priority and required dimensions of the rectangle. The general syntax for this method is:

g.fillRect(int x, int y, int width, int height);

for this example we have used the same as :

g.fillRect(160, 30,80, 80);

the drawOval method is another standard and well used Graphics method that helps the user to draw the shape of an oval of the height or width you require. The general syntax for this method is:

g.drawOval(int x, int y, int width, int height);

In this instance, we have used the following: 

g.drawOval(30,130,50, 60);

In Java, the setForeground() reference is used to fix the color in which the text is displayed. Here, we have fixed it as dark Gray color.

setForeground(Color.DARK_GRAY);

Similar to the fillRect reference, the fillOval method is used to fill the oval shape with a color of priority and required dimensions. The general syntax for this method is:

g.fillOval(int x, int y, int width, int height);

In this instance, we have used the following: 

g.fillOval(130,130,50, 60);

the drawArc method is used to draw a discoid or oval or elliptical arc. The general syntax for the drawArc method is as follows:  

#general syntax
g.drawArc(int x, int y, int width, int height, int start_angle, int arc_angle);  
#Let's use this in our example
g.drawArc(30, 200, 80,100,90,75);

similar to the drawArc method, but the fillArc method is used to fill the elliptical or oval or circular arc. The general syntax for this method is:

#general syntax
g.fillArc(int x, int y, int width, int height, int start_angle, int arc_angle);
#Let's use this as a part of our code! 
g.fillArc(30, 130, 40,50,180,40);

and the last method we have used is the drawLine method. As the name suggests, this method is used to draw a line from one point or set of coordinates to another. the general syntax and the part of code are as follows: 

#general syntax
g.drawLine(int x1, int y1, int x2, int y2); 
#Let's use this line as a part of our code!
g.drawLine(250,100 ,300,150);

now, let’s define the main class and set the title of the applet using the setTitle() method. We are using the setSize() method to define the size parameter of the applet.  The setVisibile() method ensures that the frame appears on your screen when the program is run. Also, note that the setVisible method only accepts Boolean conditions such as true or false.

Since we have learned about all the bits of the code, let’s put it all together in a single flow.

Code:

import java.awt.*;
import javax.swing.JFrame;

public class example2 extends Canvas{

    public void paint(Graphics g) {
        g.drawString("LET'S LEARN JAVA!",30,30);
        setBackground(Color.WHITE);
        g.fillRect(160, 30,80, 80);
        g.drawOval(30,130,50, 60);
        setForeground(Color.DARK_GRAY);
        g.fillOval(130,130,50, 60);
        g.drawArc(30, 200, 80,100,90,75);
        g.fillArc(30, 130, 40,50,180,40);
        g.drawLine(250,100 ,300,150);

    }
    public static void main(String[] args) {
        example2 k = new example2();
        JFrame f= new JFrame();
        f.add(k);
        f.setSize(400,400);
        //f.setLayout(null);
        f.setTitle("Graphics example by Khyati");
        f.setVisible(true);
    }
}

Now, let's run our code and see how these methods helped us create an active GUI using the Graphics class! 

output:

And there! you have now learned and implemented the methods in Graphics Class by creating an applet in Java! 

Download Complete Code

Comments

No comments yet