Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaJava Program to Implement RenderingHints API

Java Program to Implement RenderingHints API

Graphics attribute used by the Graphics2D class is java.awt.RenderingHints. This class is a mapping (a java.util.Map) from a set of rendering hint names to a set of rendering hint values. Unlike with other attributes, Graphics2D defines more than one method to set the rendering hints attribute.

  • setRenderingHints() specifies a new set of hints that replaces the old set of hints.
  • addRenderingHints() adds a set of hints to the existing set.
  • setRenderingHint() sets the value for a single hint in the current set of hints.

Rendering hints are suggestions to Java 2D about how it should perform its rendering. 

The RenderingHints class defines a number of constants whose names begin with KEY_. These constants represent the kind of hints you can give. The class also defines a number of constants whose names begin with VALUE_. These are the legal values for the various hints. The names of the VALUE constants make it clear with which hint KEY constant each value is associated. The purpose of the hints is to allow you to request that Java 2D turn a particular feature, such as antialiasing, on or off. In addition, the hints allow you to suggest what kind of speed versus quality trade-offs Java 2D should make. Remember that these are hints and suggestions to Java 2D, not commands. Not all Java 2D implementations support all the hints, and different implementations have different default values for the hints. Furthermore, the meanings of the hints are not precisely defined, so different implementations may interpret the hints differently.

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 that perform rendering and image manipulation services.

Implementation:

Java




// Java Program to show the demonstration to 
// Implement RenderingHints API
  
import java.awt.RenderingHints;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
  
public class RenderingHintsImpl
{
    private RenderingHints renderingHints;
  
    // Constructs a new object with keys and values
    // initialized from the
    // specified Map object which may be null.
  
    public RenderingHintsImpl(Map<RenderingHints.Key, ?> init)
    {
        renderingHints = new RenderingHints(init);
    }
    
     // Constructs a new object with the specified key/value
     // pair. 
    public RenderingHintsImpl(RenderingHints.Key key,
                              Object value)
    {
        renderingHints = new RenderingHints(key, value);
    }
  
     // Adds all of the keys and corresponding values from
     // the specified
     // RenderingHints object to this RenderingHints object.
    public void add(RenderingHints hints)
    {
        renderingHints.add(hints);
    }
    
     // Clears this RenderingHints object of all key/value
     // pairs. 
    public void clear() { renderingHints.clear(); }
  
     // Creates a clone of this RenderingHints object that
     // has the same contents
     // as this RenderingHints object.
    
    public Object clone() { return renderingHints.clone(); }
  
     // Returns true if this RenderingHints contains a
     // mapping for the specified key. 
  
    public boolean containsKey(Object key)
    {
        return renderingHints.containsKey(key);
    }
  
     // Returns true if this RenderingHints maps one or more
     // keys to the specified value. 
    public boolean containsValue(Object value)
    {
        return renderingHints.containsValue(value);
    }
  
     // Returns a Set view of the mappings contained in this
     // RenderingHints
    public Set<Map.Entry<Object, Object> > entrySet()
    {
        return renderingHints.entrySet();
    }
  
     // Returns the value to which the specified key is
     // mapped. 
    public Object get(Object key)
    {
        return renderingHints.get(key);
    }
  
     // Returns true if this RenderingHints contains no
     // key-value mappings. 
    public boolean isEmpty()
    {
        return renderingHints.isEmpty();
    }
  
     // Returns a Set view of the Keys contained in this
     // RenderingHints. 
    public Set<Object> keySet()
    {
       return renderingHints.keySet();
    }
  
     // Maps the specified key to the specified value in
     // this RenderingHints object. 
    public Object put(Object key, Object value)
    {
        return renderingHints.put(key, value);
    }
  
     // Copies all of the mappings from the specified Map to
     // this RenderingHints. 
    public void putAll(Map<?, ?> m)
    {
        renderingHints.putAll(m);
    }
   
     // Removes the key and its corresponding value from
     // this RenderingHints object. 
    public Object remove(Object key)
    {
        return renderingHints.remove(key);
    }
    
     // Returns the number of key-value mappings in this
     // RenderingHints. 
    public int size() { return renderingHints.size(); }
  
     // Returns a Collection view of the values contained in
     // this RenderinHints. 
    public Collection<Object> values()
    {
        return renderingHints.values();
    }
  
    public static void main(String[] arg)
    {
  
        RenderingHintsImpl renderingHints  = new RenderingHintsImpl(
                RenderingHints.KEY_ALPHA_INTERPOLATION,
  
        RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
  
        renderingHints.put(
            RenderingHints.KEY_TEXT_ANTIALIASING,
            RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  
        renderingHints.put(
            RenderingHints.KEY_FRACTIONALMETRICS,
            RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  
        renderingHints.put(
            RenderingHints.KEY_COLOR_RENDERING,
            RenderingHints.VALUE_COLOR_RENDER_QUALITY);
  
        Map<Object, Object> anotherMap = new HashMap<Object, Object>();
  
        anotherMap.put(RenderingHints.KEY_STROKE_CONTROL,
                       RenderingHints.VALUE_STROKE_PURE);
  
        anotherMap.put(RenderingHints.KEY_DITHERING,
                       RenderingHints.VALUE_DITHER_ENABLE);
  
        renderingHints.putAll(anotherMap);
  
        System.out.println("the key set of the renderingHints is - ");
  
        Set<Object> keySet = renderingHints.keySet();
  
        Iterator<Object> itr = keySet.iterator();
  
        while (itr.hasNext())
        {
            System.out.println(itr.next());
        }
  
        System.out.println();
  
        System.out.println("the values of the renderingHints is -");
  
        Collection<Object> collectionValues = renderingHints.values();
  
        itr = collectionValues.iterator();
  
        while (itr.hasNext())
        {
            System.out.println(itr.next());
        }
  
        System.out.println();
  
        System.out.println("the entry set of the renderingHints is -");
  
        Iterator<Entry<Object, Object> > eitr;
  
        Set<Entry<Object, Object> > entrySet = renderingHints.entrySet();
  
        eitr = entrySet.iterator();
  
        while (eitr.hasNext())
        {
            System.out.println(eitr.next());
        }
  
        System.out.println(
            "the renderingHints contains Key  KEY_TEXT_ANTIALIASING :"
            + renderingHints.containsKey(RenderingHints.KEY_TEXT_ANTIALIASING));
  
        System.out.println(
            "the renderingHints contains Value VALUE_TEXT_ANTIALIAS_ON :"
            + renderingHints.containsValue(
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON));
  
        System.out.println("the size of the renderingHints is :"
            + renderingHints.size());
  
        renderingHints.clear();
  
        if (renderingHints.isEmpty())
  
           System.out.println("The renderingHints is empty");
  
        else
  
           System.out.println("The renderingHints is not empty");
    }
}


Output

the key set of the renderingHints is - 
Alpha blending interpolation method key
Fractional metrics enable key
Stroke normalization control key
Color rendering quality key
Dithering quality key
Text-specific antialiasing enable key

the values of the renderingHints is -
Highest quality alpha blending methods
Fractional text metrics mode
Pure stroke conversion for accurate paths
Highest quality color rendering mode
Dithered rendering mode
Antialiased text mode

the entry set of the renderingHints is -
Alpha blending interpolation method key=Highest quality alpha blending methods
Fractional metrics enable key=Fractional text metrics mode
Stroke normalization control key=Pure stroke conversion for accurate paths
Color rendering quality key=Highest quality color rendering mode
Dithering quality key=Dithered rendering mode
Text-specific antialiasing enable key=Antialiased text 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
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments