Java Swing How to - Set renderer hint








Question

We would like to know how to set renderer hint.

Answer

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.RenderingHints;
/* w w w .  j av  a  2 s  .  c  o  m*/
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Main extends JFrame {
  MyPanel myComponent = new MyPanel();
  JLabel contentPane = new JLabel("test");
  Main() {
    contentPane.setLayout(new GridBagLayout());
    setContentPane(contentPane);
    add(myComponent);
  }

  public static void main(String[] args) {
    Main rc = new Main();
    rc.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    rc.pack();
    rc.setVisible(true);
  }
}

class MyPanel extends JPanel {

  private String target;
  private String raised = "200000";
  private Image background;

  public MyPanel() {
    setPreferredSize(new Dimension(200, 100));
  }

  @Override
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D twoD = (Graphics2D) g;

    RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
    rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    twoD.setRenderingHints(rh);

    twoD.setColor(Color.BLACK);

    twoD.drawString(raised, 5, 90);
  }
}