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.

advertisement

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.

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

  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.

Runtime Test Cases

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

advertisement

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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn