Set Color in Java

Rashmi Patidar Aug 10, 2021 Jun 07, 2021
Set Color in Java

Graphics is the pictorial representation of graphs, images, shapes that helps statistics to understand better. Java programming allows users to draw the Graphics instance as per the need. Graphics is an abstract class that has various abstract functions for drawing components over the desired devices.

Below is the code block to show how graphics work.

import javax.swing.*;
import java.applet.Applet;
import java.awt.*;

public class ColorsAndFrames {
    public static void main(String[] args) {
        ShowGraphics graphics = new ShowGraphics();
        JFrame frame = new JFrame();
        frame.setVisible(true);
        frame.setSize(400, 400);
        frame.add(graphics);
    }

    static class ShowGraphics extends Applet {
        public void paint(Graphics g) {
            g.setColor(Color.GREEN);
            g.drawOval(30, 130, 50, 60);
            Color myColor = new Color(255, 200, 113);
            g.setColor(myColor);
            g.fillRect(40, 60, 30, 60);
        }
    }
}

In the above code block, the public class holds the driver code and a static class to draw shapes. The ShowGraphics class is a user-defined class that extends the Applet class. The Applet does not run on its own but as a supporting program in other applications. It is generally defined to use along with web pages and in HTML code.

It has a method paint that comes from the java.awt.Component object. The method paints applets and takes a Graphics instance as a parameter. The object holds basic information to draw images like circles, rectangles, ovals and make manipulations over the axis. The Graphics instance gets used to invoke various functions.

On the other hand, the setColor function sets the graphic context color to the desired color. It can be a custom user-defined color or can be from the Color class. Custom colors can get defined by giving RGB values in the constructor of the Color class. The parameters are in order of Red, Green, and Blue, and the values range from 0-255. The drawOval() function is a method that draws the oval shapes. It takes four parameters x,y, width, and height.

The driver code that is the main class created the instance of the Applet extended class. The JFrame class is instantiated to hold Java Swing components that initially are invisible. So, the window is explicitly allowed to be visible using the setVisible method. The method takes a Boolean parameter and does not return any value.

Once the window starts appearing, its size is defined using the setSize function. The given parameters take the values in pixels. An instance of the ShowGraphics class is passed to the add function. The method will append the specified graph into the container. It throws NullPointerException when the component passes are null.

Other than the Java program, the Applet class can get directly called from the HTML code, and the container in the browser renders the information from the Applet class.

Below is the attached Applet image that is formed as an output when you execute the program above:

Applet viewer frame holding two shapes

Rashmi Patidar avatar Rashmi Patidar avatar

Rashmi is a professional Software Developer with hands on over varied tech stack. She has been working on Java, Springboot, Microservices, Typescript, MySQL, Graphql and more. She loves to spread knowledge via her writings. She is keen taking up new things and adopt in her career.

LinkedIn

Related Article - Java Color