需要Line2D装饰技巧 - Graphics2D

时间:2011-09-08 04:00:35

标签: java graphics java-2d graphics2d

我通过Graphics2D绘图在我的JPanel上放置了Line2D和Arc2D对象。您可以在此问题“How to make pixel perfect Line2D in - Graphics2D”上查看其中的一部分。现在我想要实现的是,我想为所有Line2D和Arc2D对象创建两条平行线和弧。在视觉上,

当前正常绘制Line2D和Arc2D,

enter image description here

想要像这样装饰它,

enter image description here

到目前为止我的想法

我或许可以通过创建两条不同的线来实现这一点,并从我的法线位置给出偏移+间隙和-gap。然而,这将产生许多我不想要的对象。

现在,是否可以像我这样使我的法线更粗,

enter image description here

并给它们一个边框并从中删除中间位?

有可能实现这一目标吗?如果是的话,我可以请一些方向。

感谢您提供任何帮助。

2 个答案:

答案 0 :(得分:3)

使用BasicStroke并绘制两次,更厚更薄。

One line drawn twice

import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.swing.*;

import javax.imageio.ImageIO;
import java.io.File;

class PaintThick {

    public static void main(String[] args) throws Exception {
        int size = 150;
        final BufferedImage bi = new BufferedImage(
            size,size,BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();

        double pad = 20;
        Line2D.Double line1 = new Line2D.Double(
            pad,pad,(double)(size-pad),(double)(size-pad));
        int cap = BasicStroke.CAP_BUTT;
        int join = BasicStroke.JOIN_MITER;
        BasicStroke thick = new BasicStroke(15,cap,join);
        BasicStroke thinner = new BasicStroke(13,cap,join);

        g.setColor(Color.WHITE);
        g.fillRect(0,0,size,size);

        g.setColor(Color.BLACK);
        g.setStroke(thick);
        g.draw(line1);

        g.setColor(Color.WHITE);
        g.setStroke(thinner);
        g.draw(line1);

        ImageIO.write(bi,"png",new File("img.png"));
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JOptionPane.showMessageDialog(
                    null, new JLabel(new ImageIcon(bi)));
            }
        });
    }
}

答案 1 :(得分:3)

您可以实施Stroke界面来创建CompositeStroke,如图here所示。

enter image description here

import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.swing.*;

/**
 * @see http://www.jhlabs.com/java/java2d/strokes/
 * @see http://stackoverflow.com/questions/7342979
 */
class StrokeTest {

    private static final int SIZE = 200;
    private static final double PAD = 20d;

    private static class CompositeStroke implements Stroke {

        private Stroke stroke1, stroke2;

        public CompositeStroke(Stroke stroke1, Stroke stroke2) {
            this.stroke1 = stroke1;
            this.stroke2 = stroke2;
        }

        @Override
        public Shape createStrokedShape(Shape shape) {
            return stroke2.createStrokedShape(
                stroke1.createStrokedShape(shape));
        }
    }

    public static void main(String[] args) throws Exception {
        final BufferedImage bi = new BufferedImage(
            SIZE, SIZE, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();
        g.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        Arc2D.Double shape = new Arc2D.Double(PAD, 2 * PAD,
            (SIZE - 2 * PAD), (SIZE - 2 * PAD), 0, 180d, Arc2D.OPEN);
        g.setColor(Color.white);
        g.fillRect(0, 0, SIZE, SIZE);
        BasicStroke s1 = new BasicStroke(16f);
        BasicStroke s2 = new BasicStroke(1f);
        g.setStroke(new CompositeStroke(s1, s2));
        g.setColor(Color.black);
        g.draw(shape);

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.add(new JLabel(new ImageIcon(bi)));
                f.pack();
                f.setVisible(true);
            }
        });
    }
}