Java Program to Implement RenderingHints API

This Java program is to Implement RenderingHints API. The RenderingHints class defines and manages collections of keys and associated values which allow an application to provide input into the choice of algorithms used by other classes which perform rendering and image manipulation services.

Here is the source code of the Java program to Implement RenderingHints API. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. import java.awt.RenderingHints;
  2. import java.util.Collection;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6. import java.util.Set;
  7. import java.util.Map.Entry;
  8.  
  9. public class RenderingHintsImpl
  10. {
  11.     private RenderingHints renderingHints;
  12.  
  13.     /**
  14.      * Constructs a new object with keys and values initialized from the
  15.      * specified Map object which may be null.
  16.      **/
  17.     public RenderingHintsImpl(Map<RenderingHints.Key, ?> init)
  18.     {
  19.         renderingHints = new RenderingHints(init);
  20.     }
  21.  
  22.     /** Constructs a new object with the specified key/value pair. **/
  23.     public RenderingHintsImpl(RenderingHints.Key key, Object value)
  24.     {
  25.         renderingHints = new RenderingHints(key, value);
  26.     }
  27.  
  28.     /**
  29.      * Adds all of the keys and corresponding values from the specified
  30.      * RenderingHints object to this RenderingHints object.
  31.      **/
  32.     public void add(RenderingHints hints)
  33.     {
  34.         renderingHints.add(hints);
  35.     }
  36.  
  37.     /** Clears this RenderingHints object of all key/value pairs. **/
  38.     public void clear()
  39.     {
  40.         renderingHints.clear();
  41.     }
  42.  
  43.     /**
  44.      * Creates a clone of this RenderingHints object that has the same contents
  45.      * as this RenderingHints object.
  46.      **/
  47.     public Object clone()
  48.     {
  49.         return renderingHints.clone();
  50.     }
  51.  
  52.     /** Returns true if this RenderingHints contains a mapping for the specified key. **/
  53.     public boolean containsKey(Object key)
  54.     {
  55.         return renderingHints.containsKey(key);
  56.     }
  57.  
  58.     /** Returns true if this RenderingHints maps one or more keys to the specified value. **/
  59.     public boolean containsValue(Object value)
  60.     {
  61.         return renderingHints.containsValue(value);
  62.     }
  63.  
  64.     /** Returns a Set view of the mappings contained in this RenderingHints. **/
  65.     public Set<Map.Entry<Object, Object>> entrySet()
  66.     {
  67.         return renderingHints.entrySet();
  68.     }
  69.  
  70.     /** Returns the value to which the specified key is mapped. **/
  71.     public Object get(Object key)
  72.     {
  73.         return renderingHints.get(key);
  74.     }
  75.  
  76.     /** Returns true if this RenderingHints contains no key-value mappings. **/
  77.     public boolean isEmpty()
  78.     {
  79.         return renderingHints.isEmpty();
  80.     }
  81.  
  82.     /** Returns a Set view of the Keys contained in this RenderingHints. **/
  83.     public Set<Object> keySet()
  84.     {
  85.         return renderingHints.keySet();
  86.     }
  87.  
  88.     /** Maps the specified key to the specified value in this RenderingHints object. **/
  89.     public Object put(Object key, Object value)
  90.     {
  91.         return renderingHints.put(key, value);
  92.     }
  93.  
  94.     /** Copies all of the mappings from the specified Map to this RenderingHints. **/
  95.     public void putAll(Map<?, ?> m)
  96.     {
  97.         renderingHints.putAll(m);
  98.     }
  99.  
  100.     /** Removes the key and its corresponding value from this RenderingHints object. **/
  101.     public Object remove(Object key)
  102.     {
  103.         return renderingHints.remove(key);
  104.     }
  105.  
  106.     /** Returns the number of key-value mappings in this RenderingHints. **/
  107.     public int size()
  108.     {
  109.         return renderingHints.size();
  110.     }
  111.  
  112.     /** Returns a Collection view of the values contained in this RenderinHints. **/
  113.     public Collection<Object> values()
  114.     {
  115.         return renderingHints.values();
  116.     }
  117.  
  118.     public static void main(String... arg)
  119.     {
  120.         RenderingHintsImpl renderingHints = new RenderingHintsImpl(RenderingHints.KEY_ALPHA_INTERPOLATION,
  121.              RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
  122.         renderingHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  123.         renderingHints.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  124.         renderingHints.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
  125.         Map<Object, Object> anotherMap = new HashMap<Object, Object>();
  126.         anotherMap.put(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
  127.         anotherMap.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
  128.         renderingHints.putAll(anotherMap);
  129.         System.out.println("the key set of the renderingHints is ");
  130.         Set<Object> keySet = renderingHints.keySet();
  131.         Iterator<Object> itr = keySet.iterator();
  132.         while (itr.hasNext())
  133.         {
  134.             System.out.print(itr.next() + "\t");
  135.         }
  136.         System.out.println();
  137.         System.out.println("the values of the renderingHints is ");
  138.         Collection<Object> collectionValues = renderingHints.values();
  139.         itr = collectionValues.iterator();
  140.         while (itr.hasNext())
  141.         {
  142.             System.out.print(itr.next() + "\t");
  143.         }
  144.         System.out.println();
  145.         System.out.println("the entry set of the renderingHints is ");
  146.         Iterator<Entry<Object, Object>> eitr;
  147.         Set<Entry<Object, Object>> entrySet = renderingHints.entrySet();
  148.         eitr = entrySet.iterator();
  149.         while (eitr.hasNext())
  150.         {
  151.             System.out.println(eitr.next() + "\t");
  152.         }
  153.         System.out.println("the renderingHints contains Key  KEY_TEXT_ANTIALIASING :" 
  154.            + renderingHints.containsKey(RenderingHints.KEY_TEXT_ANTIALIASING));
  155.         System.out.println("the renderingHints contains Value VALUE_TEXT_ANTIALIAS_ON:"
  156.            + renderingHints.containsValue(RenderingHints.VALUE_TEXT_ANTIALIAS_ON));
  157.         System.out.println("the size of the renderingHints is " + renderingHints.size());
  158.         renderingHints.clear();
  159.         if (renderingHints.isEmpty())
  160.             System.out.println("the renderingHints is empty");
  161.         else
  162.             System.out.println("the renderingHints is not empty");
  163.     }
  164. }

$ javac RenderingHIntsImpl.java
$ java RenderingHintsImpl
 
the key set of the renderingHints is 
Fractional metrics enable key	Alpha blending interpolation method key	Stroke normalization control key	Color rendering quality key	Text-specific antialiasing enable key	Dithering quality key	
the values of the renderingHints is 
Fractional text metrics mode	Highest quality alpha blending methods	Pure stroke conversion for accurate paths	Highest quality color rendering mode	Antialiased text mode	Dithered rendering mode	
the entry set of the renderingHints is 
Fractional metrics enable key=Fractional text metrics mode	
Alpha blending interpolation method key=Highest quality alpha blending methods	
Stroke normalization control key=Pure stroke conversion for accurate paths	
Color rendering quality key=Highest quality color rendering mode	
Text-specific antialiasing enable key=Antialiased text mode	
Dithering quality key=Dithered rendering mode	
the renderingHints contains Key  KEY_TEXT_ANTIALIASING :true
the renderingHints contains Value VALUE_TEXT_ANTIALIAS_ON:true
the size of the renderingHints is 6
the renderingHints is empty

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

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.