Java Program to Check Whether Antialiasing is Enabled or Not

Problem Description:

We have to write a program in Java such that it checks whether the antialiasing is enabled or not

Expected Input and Output

For checking whether antialiasing is enabled, we can have the following sets of input and output.

1. When Antialiasing is Enabled :

If antialiasing is enabled, it is expected that the message “Antialiasing is Enabled” is displayed.

2. When Antialiasing is Disabled :

If antialiasing is disabled, it is expected that the message “Antialiasing is Disabled” is displayed.

Problem Solution

1. Create a label with appropriate dimensions for the output message.
2. Convert the graphics object to Graphics2D type explicitly.
3. Create an object of class RenderingHints and get the attributes associated with the Graphics2D object.
4. Antialiasing is enabled if the object contains the value VALUE_ANTIALIAS_ON.
5. Display message accordingly.

Program/Source Code:

The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.

import java.applet.*;
import java.awt.*;
import java.awt.RenderingHints;
public class Antialiasing extends Applet
{
    Label text;
    //Create the label
    public void init()
    {
    setBackground(Color.white);
    setLayout(null);
    text = new Label();
    text.setBounds(100,150,150,100);
    this.add(text);
    }
    //Function to check if antialiasing is enabled or not
    public void paint(Graphics g)
    {
    Graphics2D G = (Graphics2D)g;
    RenderingHints rh = G.getRenderingHints();
    if(rh.containsValue(RenderingHints.VALUE_ANTIALIAS_ON))
        text.setText("Antialiasing is Enabled");
    else
        text.setText("Antialiasing is Disabled");
    }
}
/*
<applet code = Antialiasing.class width=400 height=400>
</applet>
*/

To compile an execute the program use the following commands :

>>>javac Antialiasing.java 
>>>appletviewer Antialiasing.java

 

Program Explanation

1. Explicitly type cast the object of Graphics class to Graphics2D type.
2. Use method getRenderingHints() to get the attributes of the Graphics2D object.
3. Use method containsValue(parameter) to check is the object contains the parameter. To check if antialiasing is enabled, the parameter is RenderingHints.VALUE_ANTIALIAS_ON.

Runtime Test Cases

Here’s the run time test cases for checking whether antialiasing is enabled or not for different input cases.

Test case 1 – If Antialiasing is Enabled:
java-program-antialiasing-enabled

Test case 2 – If Antialiasing is Disabled:
java-program-antialiasing-disabled

Source link

Leave a Comment