Java by API/java.awt/RenderingHints

Материал из Java эксперт
Перейти к: навигация, поиск

RenderingHints: containsValue(Object value)

  
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class Main {
  public static void main(String[] args) {
    new Main();
  }
  Main() {
    JFrame frame = new JFrame();
    frame.add(new MyComponent());
    frame.setSize(300, 300);
    frame.setVisible(true);
  }
}
class MyComponent extends JComponent {
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    RenderingHints rhints = g2d.getRenderingHints();
    boolean antialiasOn = rhints.containsValue(RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  }
}





RenderingHints.KEY_ANTIALIASING

 
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainClass{
  public static void main(String[] args) {
    JFrame jf = new JFrame("Demo");
    Container cp = jf.getContentPane();
    MyCanvas tl = new MyCanvas();
    cp.add(tl);
    jf.setSize(300, 200);
    jf.setVisible(true);
  }
}
class MyCanvas extends JComponent {
  Shape shape;
  public MyCanvas() {
    shape = create();
  }
  protected Shape create() {
    float cm = 72 / 2.54f;
    return new Rectangle2D.Float(cm, cm, 2 * cm, cm);
  }

  public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    
    g2.setPaint(Color.black);
    g2.draw(shape);
  }
}





RenderingHints.VALUE_ANTIALIAS_OFF

  

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class BasicDraw {
  public static void main(String[] args) {
    new BasicDraw();
  }
  BasicDraw() {
    JFrame frame = new JFrame();
    frame.add(new MyComponent());
    frame.setSize(300, 300);
    frame.setVisible(true);
  }
}
class MyComponent extends JComponent {
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
  }
}





RenderingHints.VALUE_ANTIALIAS_ON

 
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainClass{
  public static void main(String[] args) {
    JFrame jf = new JFrame("Demo");
    Container cp = jf.getContentPane();
    MyCanvas tl = new MyCanvas();
    cp.add(tl);
    jf.setSize(300, 200);
    jf.setVisible(true);
  }
}
class MyCanvas extends JComponent {
  Shape shape;
  public MyCanvas() {
    shape = create();
  }
  protected Shape create() {
    float cm = 72 / 2.54f;
    return new Rectangle2D.Float(cm, cm, 2 * cm, cm);
  }

  public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    
    g2.setPaint(Color.black);
    g2.draw(shape);
  }
}





RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HBGR

  

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
public class TextQualityDemoVALUE_TEXT_ANTIALIAS_LCD_HBGR {
  public static void main(String[] args) {
    JFrame frame = new JFrame("LCD Text Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setPreferredSize(new Dimension(630, 460));
    frame.setContentPane(new MyPanel(RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HBGR));
    frame.pack();
    frame.setVisible(true);
  }
}
class MyPanel extends JPanel {
  private Object hintValue;
  public MyPanel(Object hintValue) {
    this.hintValue = hintValue;
    this.setPreferredSize(new Dimension(300, 100));
    String title = hintValue.toString();
    Border border = new TitledBorder(title);
    this.setBorder(border);
  }
  public void paintComponent(Graphics g) {
    Dimension d = this.getSize();
    BufferedImage backBuffer = (BufferedImage) this.createImage(d.width, d.height);
    Graphics2D g2 = backBuffer.createGraphics();
    g2.setColor(Color.WHITE);
    g2.fillRect(0, 0, d.width, d.height);
    g2.setColor(Color.BLACK);
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, hintValue);
    g2.drawRect(0, 0, d.width - 1, d.height - 1);
    g2.drawString("abcdefghijklmnopqrstuvwxyz", 20, 40);
    g2.drawString("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 20, 60);
    g2.drawString("1234567890-=!@#$%^&*()_+,./<>?", 20, 80);
    g.drawImage(backBuffer, 0, 0, this);
  }
}





RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB

  

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
public class TextQualityDemoVALUE_TEXT_ANTIALIAS_LCD_HRGB {
  public static void main(String[] args) {
    JFrame frame = new JFrame("LCD Text Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setPreferredSize(new Dimension(630, 460));
    frame.setContentPane(new MyPanel(RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB));
    frame.pack();
    frame.setVisible(true);
  }
}
class MyPanel extends JPanel {
  private Object hintValue;
  public MyPanel(Object hintValue) {
    this.hintValue = hintValue;
    this.setPreferredSize(new Dimension(300, 100));
    String title = hintValue.toString();
    Border border = new TitledBorder(title);
    this.setBorder(border);
  }
  public void paintComponent(Graphics g) {
    Dimension d = this.getSize();
    BufferedImage backBuffer = (BufferedImage) this.createImage(d.width, d.height);
    Graphics2D g2 = backBuffer.createGraphics();
    g2.setColor(Color.WHITE);
    g2.fillRect(0, 0, d.width, d.height);
    g2.setColor(Color.BLACK);
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, hintValue);
    g2.drawRect(0, 0, d.width - 1, d.height - 1);
    g2.drawString("abcdefghijklmnopqrstuvwxyz", 20, 40);
    g2.drawString("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 20, 60);
    g2.drawString("1234567890-=!@#$%^&*()_+,./<>?", 20, 80);
    g.drawImage(backBuffer, 0, 0, this);
  }
}





RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_VBGR

  

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
public class TextQualityDemoVALUE_TEXT_ANTIALIAS_LCD_VBGR {
  public static void main(String[] args) {
    JFrame frame = new JFrame("LCD Text Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setPreferredSize(new Dimension(630, 460));
    frame.setContentPane(new MyPanel(RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_VBGR));
    frame.pack();
    frame.setVisible(true);
  }
}
class MyPanel extends JPanel {
  private Object hintValue;
  public MyPanel(Object hintValue) {
    this.hintValue = hintValue;
    this.setPreferredSize(new Dimension(300, 100));
    String title = hintValue.toString();
    Border border = new TitledBorder(title);
    this.setBorder(border);
  }
  public void paintComponent(Graphics g) {
    Dimension d = this.getSize();
    BufferedImage backBuffer = (BufferedImage) this.createImage(d.width, d.height);
    Graphics2D g2 = backBuffer.createGraphics();
    g2.setColor(Color.WHITE);
    g2.fillRect(0, 0, d.width, d.height);
    g2.setColor(Color.BLACK);
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, hintValue);
    g2.drawRect(0, 0, d.width - 1, d.height - 1);
    g2.drawString("abcdefghijklmnopqrstuvwxyz", 20, 40);
    g2.drawString("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 20, 60);
    g2.drawString("1234567890-=!@#$%^&*()_+,./<>?", 20, 80);
    g.drawImage(backBuffer, 0, 0, this);
  }
}





RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_VRGB

  

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
public class TextQualityDemoVALUE_TEXT_ANTIALIAS_LCD_VRGB {
  public static void main(String[] args) {
    JFrame frame = new JFrame("LCD Text Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setPreferredSize(new Dimension(630, 460));
    frame.setContentPane(new MyPanel(RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_VRGB));
    frame.pack();
    frame.setVisible(true);
  }
}
class MyPanel extends JPanel {
  private Object hintValue;
  public MyPanel(Object hintValue) {
    this.hintValue = hintValue;
    this.setPreferredSize(new Dimension(300, 100));
    String title = hintValue.toString();
    Border border = new TitledBorder(title);
    this.setBorder(border);
  }
  public void paintComponent(Graphics g) {
    Dimension d = this.getSize();
    BufferedImage backBuffer = (BufferedImage) this.createImage(d.width, d.height);
    Graphics2D g2 = backBuffer.createGraphics();
    g2.setColor(Color.WHITE);
    g2.fillRect(0, 0, d.width, d.height);
    g2.setColor(Color.BLACK);
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, hintValue);
    g2.drawRect(0, 0, d.width - 1, d.height - 1);
    g2.drawString("abcdefghijklmnopqrstuvwxyz", 20, 40);
    g2.drawString("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 20, 60);
    g2.drawString("1234567890-=!@#$%^&*()_+,./<>?", 20, 80);
    g.drawImage(backBuffer, 0, 0, this);
  }
}





RenderingHints.VALUE_TEXT_ANTIALIAS_OFF

  

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class BasicDraw {
  public static void main(String[] args) {
    new BasicDraw();
  }
  BasicDraw() {
    JFrame frame = new JFrame();
    frame.add(new MyComponent());
    frame.setSize(300, 300);
    frame.setVisible(true);
  }
}
class MyComponent extends JComponent {
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
  }
}





RenderingHints.VALUE_TEXT_ANTIALIAS_ON

  
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
public class TextQualityDemoVALUE_TEXT_ANTIALIAS_ON {
  public static void main(String[] args) {
    JFrame frame = new JFrame("LCD Text Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setPreferredSize(new Dimension(630, 460));
    frame.setContentPane(new MyPanel(RenderingHints.VALUE_TEXT_ANTIALIAS_ON));
    frame.pack();
    frame.setVisible(true);
  }
}
class MyPanel extends JPanel {
  private Object hintValue;
  public MyPanel(Object hintValue) {
    this.hintValue = hintValue;
    this.setPreferredSize(new Dimension(300, 100));
    String title = hintValue.toString();
    Border border = new TitledBorder(title);
    this.setBorder(border);
  }
  public void paintComponent(Graphics g) {
    Dimension d = this.getSize();
    BufferedImage backBuffer = (BufferedImage) this.createImage(d.width, d.height);
    Graphics2D g2 = backBuffer.createGraphics();
    g2.setColor(Color.WHITE);
    g2.fillRect(0, 0, d.width, d.height);
    g2.setColor(Color.BLACK);
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, hintValue);
    g2.drawRect(0, 0, d.width - 1, d.height - 1);
    g2.drawString("abcdefghijklmnopqrstuvwxyz", 20, 40);
    g2.drawString("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 20, 60);
    g2.drawString("1234567890-=!@#$%^&*()_+,./<>?", 20, 80);
    g.drawImage(backBuffer, 0, 0, this);
  }
}