简体   繁体   English

如何在 BufferedImage 上使用具有抗锯齿功能的 Graphics2D.drawline?

[英]How can I use Graphics2D.drawline with anti-aliasing on a BufferedImage?

Rough and smooth lines粗犷流畅的线条

When drawing on a JPanel , I can set anti-aliasing on as a rendering hint and then draw a line that has smoothed edges.JPanel上绘图时,我可以将抗锯齿设置为渲染提示,然后绘制一条边缘平滑的线条。

If, however, I create a BufferedImage and draw the same line on it with anti-aliasing, and then I use JPanel.drawImage to draw the buffered image, the line is drown roughly (eg anti-aliasing=off).但是,如果我创建一个BufferedImage并在其上使用抗锯齿绘制相同的线,然后使用JPanel.drawImage绘制缓冲图像,则该线被粗略地淹没(例如 anti-aliasing=off)。

I can't determine how to draw a line on a buffered image with anti-aliasing.我无法确定如何在带有抗锯齿的缓冲图像上画一条线。 Can someone clarify how this is done?有人可以澄清这是如何完成的吗?

In the below example, I'm simply creating two JFrame .在下面的示例中,我只是创建了两个JFrame In one frame I create a panel and draw on it directly.在一个框架中,我创建了一个面板并直接在其上绘制。 In the other frame I create a panel, then a buffered image that I draw on, then use JPanel.drawImage to display.在另一个框架中,我创建了一个面板,然后是一个我绘制的缓冲图像,然后使用JPanel.drawImage来显示。 I want the line shown in the buffered image to be drawn with anti-aliasing.我希望缓冲图像中显示的线条使用抗锯齿绘制。

import java.awt.BasicStroke;
import java.awt.BorderLayout;
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;

public class Test {
    public static void main(String[] args){
        doDirect();
        doBuffered();
    }
    public static void doBuffered(){
        JFrame frame = new JFrame();
        frame.setTitle("BufferedImage drawn with antialiasing on");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JPanel canvas = new JPanel(){
            @Override
            protected void paintComponent(Graphics g){
                BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
                Graphics2D ig2d = image.createGraphics();
                ig2d.setColor(Color.BLACK);
                ig2d.setStroke(new BasicStroke(3));
                ig2d.setRenderingHint(
                        RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
                ig2d.drawLine(10, 10, 70, 90);
                
                Graphics2D g2d = (Graphics2D)g;
                g2d.drawImage(image, 0, 0, null);
            }
        };
        canvas.setPreferredSize(new Dimension(100, 100));
        frame.getContentPane().add(canvas, BorderLayout.CENTER);
        frame.setEnabled(true);
        frame.pack();
        frame.setVisible(true);
    }
    public static void doDirect(){
        JFrame frame = new JFrame();
        frame.setTitle("Directly drawn with antialiasing on");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JPanel canvas = new JPanel(){
            @Override
            protected void paintComponent(Graphics g){
                Graphics2D g2d = (Graphics2D)g;
                g2d.setColor(Color.BLACK);
                g2d.setStroke(new BasicStroke(3));
                g2d.setRenderingHint(
                        RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.drawLine(10, 10, 70, 90);
            }
        };
        canvas.setPreferredSize(new Dimension(100, 100));
        frame.getContentPane().add(canvas, BorderLayout.CENTER);
        frame.setEnabled(true);
        frame.pack();
        frame.setVisible(true);
    }
}

I ran your sample and both methods of drawing seemed to apply the antialiasing.我运行了您的示例,两种绘图方法似乎都应用了抗锯齿。 It may be a bug in the version of the jdk you're using.这可能是您正在使用的 jdk 版本中的错误。 I'm running on Windows with jdk 1.8.0_77 and also tried with jdk 1.7.0_79.我正在使用 jdk 1.8.0_77 在 Windows 上运行,并且还尝试使用 jdk 1.7.0_79。

I reported this bug on the official Java page, but reporting it has not registered yet.我在官方Java页面上报告了这个错误,但报告它还没有注册。 Last correct version for Windows was working with antialias: On , for bufferedimage which are on version 8. There are no more JDK Version 8 builds for Windows in x64. Windows 的最后一个正确版本正在使用antialias: On ,用于版本 8 上的bufferedimage 。在 x64 中没有更多适用于 Windows 的 JDK 版本 8 构建。 All of the next versions including the last versions: 18 / 19, OracleJDK and OpenJDK have this unaddressed bug.所有下一个版本,包括最后一个版本:18 / 19、OracleJDK 和 OpenJDK 都有这个未解决的错误。

I speculate that new millennial developers with their new Shaolin Kung-Fu techniques, throw out all the old versions and they build new versions from scratch, in many wrong ways.我推测新的千禧一代开发人员使用他们的新少林功夫技术,抛弃了所有旧版本,他们以许多错误的方式从头开始构建新版本。 It is Java from Oracle a way of death for non business applications in 2022?甲骨文的 Java 是 2022 年非商业应用程序的死亡方式吗? Yes.是的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM