This is a Java Program to Check Whether Antialiasing is Enabled or Not
We have to write a program in Java such that it checks whether the antialiasing is enabled or not
For checking whether antialiasing is enabled or not, 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.
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.
Here is source code of the Java Program to check whether antialiasing is enabled or not. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.
/* Java Program to Check Whether Antialiasing is Enabled or Not */
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
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.
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:
Test case 2 – If Antialiasing is Disabled:
Sanfoundry Global Education & Learning Series – Java Programs.
- Check Java Books
- Practice Programming MCQs
- Practice Information Technology MCQs
- Practice BCA MCQs
- Check Programming Books