0

我正在使用 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 平滑地绘制此边缘?谢谢

4

2 回答 2

3

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

在此处输入图像描述

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();
        }
    }
}
于 2013-04-30T01:37:14.120 回答
2

一些想法:

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

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

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

于 2013-04-29T22:00:10.270 回答