在Java中绘制带有尖角的边界路径

时间:2013-02-23 09:33:26

标签: java swing user-interface drawing java-2d

我正在开发一个在地图上显示GPS轨迹的应用程序。我想把轨道画成任意厚度的彩色路径。我找到了GeneralPath类似乎正是我想要的。但是,我希望彩色路径也有黑色边框。我找不到任何关于如何在路径上添加边框的内容,所以我想出了一个快速的hacky解决方案,首先绘制一条粗黑色路径,然后在顶部绘制一条细彩色路径。

SSCCE:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.GeneralPath;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class PathBorder {
    private JFrame frame;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    PathBorder window = new PathBorder();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public PathBorder() {
        initialize();
    }

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel() {
            protected void paintComponent(Graphics g) {
                GeneralPath path;
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.setStroke(new BasicStroke(5.5f));
                g2d.setColor(Color.BLACK);
                path = new GeneralPath();
                path.moveTo(0, 0);
                path.lineTo(100, 100);
                path.lineTo(200, 100);
                path.lineTo(100, 80);
                g2d.draw(path);
                g2d.setStroke(new BasicStroke(3));
                g2d.setColor(Color.YELLOW);
                g2d.draw(path);
            }
        };
        frame.setBackground(Color.CYAN);
        frame.add(panel);
    }
}

这是一张照片(摘自SSCCE)以突出我的问题。请参阅下面的红色圆圈,注意外边框有一个间隙。我希望填补这个空白,以便边界是连续的。

enter image description here

以下是我的应用程序中一些真实曲目的实际屏幕截图:

enter image description here

如果你仔细观察赛道右下方的尖锐发夹转弯,你会发现边框会短暂丢失......下面的详细图片会让它更清晰。

enter image description here

我不确定如何解决此问题,但我愿意接受建议,要么保持GeneralPath策略,要么完全使用不同的想法。

1 个答案:

答案 0 :(得分:6)

尝试capjoin参数以获得更好的效果。例如。

PathBorder

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.GeneralPath;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class PathBorder {

    private JFrame frame;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    PathBorder window = new PathBorder();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public PathBorder() {
        initialize();
    }

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel() {

            GeneralPath path;

            protected void paintComponent(Graphics g) {
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
                BasicStroke s = new BasicStroke(
                        5.5f, 
                        BasicStroke.CAP_ROUND, 
                        BasicStroke.JOIN_ROUND);
                g2d.setStroke(s);
                g2d.setColor(Color.BLACK);
                if (path==null) {
                    path = new GeneralPath();
                    path.moveTo(0, 0);
                    path.lineTo(100, 100);
                    path.lineTo(200, 100);
                    path.lineTo(100, 80);
                }
                g2d.draw(path);
                g2d.setStroke(new BasicStroke(3));
                g2d.setColor(Color.YELLOW);
                g2d.draw(path);
            }
        };
        frame.setBackground(Color.CYAN);
        frame.add(panel);
    }
}