java.awt.Graphics2D.setRenderingHint()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(12.5k)|赞(0)|评价(0)|浏览(520)

本文整理了Java中java.awt.Graphics2D.setRenderingHint()方法的一些代码示例,展示了Graphics2D.setRenderingHint()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Graphics2D.setRenderingHint()方法的具体详情如下:
包路径:java.awt.Graphics2D
类名称:Graphics2D
方法名:setRenderingHint

Graphics2D.setRenderingHint介绍

暂无

代码示例

代码示例来源:origin: skylot/jadx

@Override
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g.create();
    try {
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      super.paint(g2d);
    } finally {
      g2d.dispose();
    }
  }
}

代码示例来源:origin: ivan-vasilev/neuralnetworks

private BufferedImage resize(BufferedImage input, int targetWidth, int targetHeight)
  {
    BufferedImage result = new BufferedImage(targetWidth, targetHeight, input.getType());

    Graphics2D g = result.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.drawImage(input, 0, 0, targetWidth, targetHeight, null);
    g.dispose();

    return result;
  }
}

代码示例来源:origin: libgdx/libgdx

/**
 * Draws the glyph to the given image, upscaled by a factor of {@link #scale}.
 * 
 * @param image the image to draw to
 * @param glyph the glyph to draw
 */
private void drawGlyph(BufferedImage image, Glyph glyph) {
  Graphics2D inputG = (Graphics2D) image.getGraphics();
  inputG.setTransform(AffineTransform.getScaleInstance(scale, scale));
  // We don't really want anti-aliasing (we'll discard it anyway),
  // but accurate positioning might improve the result slightly
  inputG.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  inputG.setColor(Color.WHITE);
  inputG.fill(glyph.getShape());
}

代码示例来源:origin: stackoverflow.com

super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(GRAPH_COLOR);
g2.setStroke(GRAPH_STROKE);
for (int i = 0; i < graphPoints.size() - 1; i++) {
g2.setColor(GRAPH_POINT_COLOR);
for (int i = 0; i < graphPoints.size(); i++) {
  int x = graphPoints.get(i).x - GRAPH_POINT_WIDTH / 2;
return new Dimension(PREF_W, PREF_H);
JFrame frame = new JFrame("DrawGraph");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);

代码示例来源:origin: stackoverflow.com

BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
Font font = new Font("Arial", Font.PLAIN, 48);
g2d.setFont(font);
g2d.dispose();
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
g2d = img.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g2d.setFont(font);
fm = g2d.getFontMetrics();
g2d.setColor(Color.BLACK);
g2d.drawString(text, 0, fm.getAscent());
g2d.dispose();

代码示例来源:origin: stackoverflow.com

this.setPreferredSize(new Dimension(SIZE, SIZE));
this.n = n;
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(
  RenderingHints.KEY_ANTIALIASING,
  RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.black);
a = getWidth() / 2;
b = getHeight() / 2;
int r2 = Math.abs(m - r) / 2;
g2d.drawOval(a - r, b - r, 2 * r, 2 * r);
g2d.setColor(Color.blue);
for (int i = 0; i < n; i++) {
  double t = 2 * Math.PI * i / n;
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new CircleTest(9));
f.pack();

代码示例来源:origin: stackoverflow.com

JFrame frame = new JFrame("Testing");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setLayout(new BorderLayout());
  frame.add(new ScalePane());
try {
  original = ImageIO.read(new File("path/to/master.jpg"));
  scaled = getScaledInstanceToFit(original, new Dimension(60, 60));
  ImageIO.write(scaled, "jpg", new File("scaled.jpg"));
  Graphics2D g2d = image.createGraphics();
  g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
  g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  int imageWidth = img.getWidth();
  int imageHeight = img.getHeight();
  scale = getScaleFactorToFit(new Dimension(imageWidth, imageHeight), size);
    BufferedImage tmp = new BufferedImage(Math.max(w, 1), Math.max(h, 1), type);
    Graphics2D g2 = tmp.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
    g2.drawImage(ret, 0, 0, w, h, null);
    g2.dispose();
  ret = new BufferedImage(1, 1, type);

代码示例来源:origin: stackoverflow.com

JFrame frame = new JFrame();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setLayout(new BorderLayout());
  frame.add(new Balls());
int y = getY();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(color);
g.fillOval(0, 0, 30, 30); //adds color to circle
g.setColor(Color.black);
g2.drawOval(0, 0, 30, 30); //draws circle
return new Dimension(30, 30);

代码示例来源:origin: stackoverflow.com

private Image getScaledImage(Image srcImg, int w, int h){
  BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  Graphics2D g2 = resizedImg.createGraphics();

  g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
  g2.drawImage(srcImg, 0, 0, w, h, null);
  g2.dispose();

  return resizedImg;
}

代码示例来源:origin: stackoverflow.com

import javax.swing.*;

public class FrameTestBase extends JFrame {

  public static void main(String args[]) {
    FrameTestBase t = new FrameTestBase();
    t.add(new JComponent() {
      public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                  RenderingHints.VALUE_ANTIALIAS_ON);
        for (int i = 0; i < 50; i++) {
          double delta = i / 10.0;
          double y = 5 + 5*i;
          Shape l = new Line2D.Double(5, y, 200, y + delta);
          g2.draw(l);
        }
      }
    });

    t.setDefaultCloseOperation(EXIT_ON_CLOSE);
    t.setSize(400, 400);
    t.setVisible(true);
  }
}

代码示例来源:origin: geotools/geotools

@SuppressWarnings("PMD.SystemPrintln")
public static void main(String[] args) {
  BufferedImage image = new BufferedImage(300, 300, BufferedImage.TYPE_4BYTE_ABGR);
  Graphics2D g2d = (Graphics2D) image.getGraphics();
  g2d.setColor(Color.BLACK);
  GlyphVector textGlyphVector = font.createGlyphVector(FONT_RENDER_CONTEXT, new char[] {,});
  Shape shape = textGlyphVector.getOutline();
  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  g2d.translate(150, 150);
  g2d.setColor(Color.BLUE);
  g2d.fill(shape);
  g2d.setColor(Color.BLACK);
  g2d.setFont(font);
  g2d.drawString(new String(new char[] {c}), 0, 50);
  g2d.dispose();
  JFrame frame = new JFrame("Test");
  frame.setContentPane(new JLabel(new ImageIcon(image)));
  frame.pack();
  frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  frame.setVisible(true);

代码示例来源:origin: stackoverflow.com

max = (int)Math.max( trueLenth, falseLenght );
gap =  Math.max( 5, 5+(int)Math.abs(trueLenth - falseLenght ) ); 
thumbBounds  = new Dimension(max+gap*2,20);
globalWitdh =  max + thumbBounds.width+gap*2;
setModel( new DefaultButtonModel() );
g.setColor( getBackground() );
g.fillRoundRect( 1, 1, getWidth()-2 - 1, getHeight()-2 ,2 ,2 );
Graphics2D g2 = (Graphics2D)g;
g2.setColor( black );
g2.drawRoundRect( 1, 1, getWidth()-2 - 1, getHeight()-2 - 1, 2,2 );
g2.setColor( white );
g2.drawRoundRect( 1 + 1, 1 + 1, getWidth()-2 - 3, getHeight()-2 - 3, 2,2 );
  g2.setColor( colorBright );
  g2.fillRect(x+w/2-size/2,y+h/2-size/2, size, size);
  g2.setColor( new Color(120,120,120));
g2.setColor( black.darker() );
g2.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON );
g2.setFont( getFont() );
g2.drawString( getText(), lx+gap, y+h/2+h/4 );

代码示例来源:origin: stackoverflow.com

BufferedImage img = new BufferedImage(width, height,
    source.getType());
Graphics2D g = img.createGraphics();
try {
  g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
  g.drawImage(source, 0, 0, width, height, null);
} finally {

代码示例来源:origin: looly/hutool

@Override
public Image createImage(String code) {
  final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  final Graphics2D g = ImageUtil.createGraphics(image, ObjectUtil.defaultIfNull(this.background, Color.WHITE));
  
  // 随机画干扰圈圈
  drawInterfere(g);
  
  // 画字符串
  // 抗锯齿
  g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  g.setFont(font);
  final FontMetrics metrics = g.getFontMetrics();
  final int minY = metrics.getAscent() - metrics.getLeading() - metrics.getDescent();
  int len = code.length();
  int charWidth = width / len;
  for (int i = 0; i < len; i++) {
    // 指定透明度
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.9f));
    g.setColor(ImageUtil.randomColor());
    g.drawString(String.valueOf(code.charAt(i)), i * charWidth, RandomUtil.randomInt(minY, this.height));
  }
  
  return image;
}

代码示例来源:origin: linlinjava/litemall

private void drawTextInImg(BufferedImage baseImage, String textToWrite, int x, int y) {
  Graphics2D g2D = (Graphics2D) baseImage.getGraphics();
  g2D.setColor(new Color(167, 136, 69));
  //TODO 注意,这里的字体必须安装在服务器上
  g2D.setFont(new Font("Microsoft YaHei", Font.PLAIN, 28));
  g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  g2D.drawString(textToWrite, x, y);
  g2D.dispose();
}

代码示例来源:origin: chewiebug/GCViewer

/**
 * Adds version string below <code>logoImage</code>.
 *
 * @param g
 */
private void drawVersionString(Graphics g, ImageIcon logoImage) {
  g.setColor(Color.LIGHT_GRAY);
  g.setFont(new Font("Serif", Font.BOLD, 12));
  // use anti aliasing to draw string
  Graphics2D g2d = (Graphics2D)g;
  Object oldAAHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  String versionString = "version: " + BuildInfoReader.getVersion() + " (" + BuildInfoReader.getBuildDate() + ")";
  g.drawString(versionString,
      getWidth()/2 - g.getFontMetrics().stringWidth(versionString)/2,
      getHeight()/2 + logoImage.getIconHeight()/2 + 25);
  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAAHint);
}

代码示例来源:origin: runelite/runelite

graphics2d.dispose();
      stretchedGraphics.dispose();
    graphics.setColor(Color.BLACK);
    graphics.fillRect(0, 0, client.getCanvas().getWidth(), client.getCanvas().getHeight());
  stretchedGraphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
    client.isStretchedFast()
      ? RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR

代码示例来源:origin: stackoverflow.com

BufferedImage img = new BufferedImage(PREF_W, PREF_H,
   BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
   RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(BASIC_STROKE);
g2.setColor(Color.blue);
int circleCount = 10;
for (int i = 0; i < circleCount ; i++) {
BufferedImage img = new BufferedImage(PREF_W, PREF_H,
   BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
   RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(BASIC_STROKE);
g2.setColor(Color.red);
int circleCount = 10;
for (int i = 0; i < circleCount + 1; i++) {
return new Dimension(PREF_W, PREF_H);
JFrame frame = new JFrame("TestAlphaComposite");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new TestAlphaComposite());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

代码示例来源:origin: jMonkeyEngine/jmonkeyengine

private static BufferedImage scaleDown(BufferedImage sourceImage, int targetWidth, int targetHeight) {
  int sourceWidth  = sourceImage.getWidth();
  int sourceHeight = sourceImage.getHeight();
  BufferedImage targetImage = new BufferedImage(targetWidth, targetHeight, sourceImage.getType());
  Graphics2D g = targetImage.createGraphics();
  g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
  g.drawImage(sourceImage, 0, 0, targetWidth, targetHeight, 0, 0, sourceWidth, sourceHeight, null);
  g.dispose();
  return targetImage;
}

代码示例来源:origin: stackoverflow.com

URL url = new URL("http://i.stack.imgur.com/Nqf3H.jpg");
BufferedImage originalImage = ImageIO.read(url);
final BufferedImage textImage = new BufferedImage(
  originalImage.getWidth(),
  originalImage.getHeight(),
  BufferedImage.TYPE_INT_ARGB);
Graphics2D g = textImage.createGraphics();
FontRenderContext frc = g.getFontRenderContext();
Font font = new Font(Font.SANS_SERIF, Font.BOLD, 250);
g.setClip(null);
g.setStroke(new BasicStroke(2f));
g.setColor(Color.BLACK);
g.setRenderingHint(
  RenderingHints.KEY_ANTIALIASING,
  RenderingHints.VALUE_ANTIALIAS_ON);

相关文章

Graphics2D类方法