Java Program to Check Whether Antialiasing is Enabled or Not

This is a 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 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 :

advertisement
advertisement
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

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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  1. /* Java Program to Check Whether Antialiasing is Enabled or Not */
  2. import java.applet.*;
  3. import java.awt.*;
  4. import java.awt.RenderingHints;
  5. public class Antialiasing extends Applet
  6. {
  7.     Label text;
  8.     //Create the label
  9.     public void init()
  10.     {
  11. 	setBackground(Color.white);
  12. 	setLayout(null);
  13. 	text = new Label();
  14. 	text.setBounds(100,150,150,100);
  15. 	this.add(text);
  16.     }
  17.     //Function to check if antialiasing is enabled or not
  18.     public void paint(Graphics g)
  19.     {
  20. 	Graphics2D G = (Graphics2D)g;
  21. 	RenderingHints rh = G.getRenderingHints();
  22. 	if(rh.containsValue(RenderingHints.VALUE_ANTIALIAS_ON))
  23. 	    text.setText("Antialiasing is Enabled");
  24. 	else
  25. 	    text.setText("Antialiasing is Disabled");
  26.     }
  27. }
  28. /*
  29. <applet code = Antialiasing.class width=400 height=400>
  30. </applet>
  31. */

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.

advertisement
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

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.