📜  实现RenderingHints API的Java程序

📅  最后修改于: 2022-05-13 01:55:29.590000             🧑  作者: Mango

实现RenderingHints API的Java程序

Graphics2D 类使用的图形属性是Java.awt.RenderingHints 。此类是从一组呈现提示名称到一组呈现提示值的映射( Java.util.Map)。与其他属性不同,Graphics2D 定义了不止一种方法来设置呈现提示属性。

  • setRenderingHints()指定一组新的提示来替换旧的提示集。
  • addRenderingHints()将一组提示添加到现有的集合中。
  • setRenderingHint()设置当前提示集中单个提示的值。

渲染提示是对Java 2D 应该如何执行渲染的建议。

RenderingHints 类定义了许多名称以 KEY_ 开头的常量。这些常量代表您可以给出的提示类型。该类还定义了许多名称以 VALUE_ 开头的常量。这些是各种提示的合法值。 VALUE 常量的名称清楚地表明每个值与哪个提示 KEY 常量相关联。提示的目的是允许您请求Java 2D 打开或关闭特定功能,例如抗锯齿。此外,这些提示允许您建议Java 2D 应该做出什么样的速度与质量的权衡。请记住,这些是对Java 2D 的提示和建议,而不是命令。并非所有Java 2D 实现都支持所有提示,并且不同的实现具有不同的提示默认值。此外,提示的含义没有精确定义,因此不同的实现可能对提示的解释不同。

RenderingHints 类定义和管理键和关联值的集合,这些集合允许应用程序为其他执行渲染和图像处理服务的类使用的算法选择提供输入。

执行:

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 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 > 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 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 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 anotherMap = new HashMap();
  
        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 keySet = renderingHints.keySet();
  
        Iterator itr = keySet.iterator();
  
        while (itr.hasNext())
        {
            System.out.println(itr.next());
        }
  
        System.out.println();
  
        System.out.println("the values of the renderingHints is -");
  
        Collection 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 > eitr;
  
        Set > 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");
    }
}

输出
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