使用Java 2D的Antialias高度图边缘

时间:2013-04-29 21:48:52

标签: java rendering java-2d antialiasing terrain

我正在使用Java 2D渲染地形图。地形图如下所示:

你可以看到边缘是如何锯齿状的。我想平滑地渲染地形的边缘,但是使用RenderingHints启用抗锯齿功能不起作用,因为我一次只渲染一个地形图。

这是我渲染地形的代码:

// terrainImageG2 renders to a BufferedImage, obtained via BufferedImage.createGraphics()
terrainImageG2.setBackground(Color.WHITE);
terrainImageG2.clearRect(0, 0, NUM_WIDE, NUM_HIGH);
terrainImageG2.setStroke(new BasicStroke(1.0F));
terrainImageG2.setColor(Color.BLACK);
for (int x = 0; x < NUM_WIDE; x++) {
    terrainImageG2.drawLine(x, NUM_HIGH - originalHeightMap[x], x, NUM_HIGH);
    // originalHeightMap contains the height map, ranging from 0 to NUM_HIGH
    // originalHeightMap 
}

正如您所看到的,我使用drawLine渲染地形,所以如果我在此之前使用:

terrainImageG2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

它实际上并没有消除边缘。如果我启用RenderingHints的抗锯齿,那么这就是地形的样子:

我尝试做一些手动抗锯齿,我将顶部像素渲染为浅灰色而不是黑色。然而,这使得更平坦的区域看起来模糊而不影响更陡峭的区域。看看:

有没有办法可以手动或通过某些API顺利绘制这个边缘?感谢

2 个答案:

答案 0 :(得分:3)

您可以尝试使用其他一些渲染提示......

enter image description here

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestPaint20 {

    public static void main(String[] args) {
        new TestPaint20();
    }

    public TestPaint20() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setBackground(Color.WHITE);
            g2d.setStroke(new BasicStroke(1.0F, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
            g2d.setColor(Color.BLACK);
            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);

            for (int x = 0; x < getWidth(); x++) {
                g2d.drawLine(x, getHeight() - (int) Math.round(Math.random() * (getHeight() - (getHeight() / 4d))), x, getHeight());
                // originalHeightMap contains the height map, ranging from 0 to NUM_HIGH
                // originalHeightMap 
            }
            g2d.dispose();
        }
    }
}

答案 1 :(得分:2)

一些想法:

  • 通过将每条线的顶点连接到其邻居的顶点,在顶部绘制抗锯齿线。这可能更容易调试,只需绘制顶行以查看它的外观,然后填写垂直线,如果你喜欢它的外观。

  • 尝试通过在绘制这个算法时考虑前一个和下一个顶部的位置来计算出类似于抗锯齿方式的算法。因此,如果当前的一个低于或高于前一个和后一个,则在顶部添加一个灰点。如果相同,则不添加点。根据相邻点的高度,使点更亮或更暗。

  • 在绘图之前使用线性平均或某种曲线平滑来平滑数据。这不会真正消除别名,但可能会使图表更具吸引力。