【问题标题】:Line2D decoration tips needed - Graphics2D需要 Line2D 装饰技巧 - Graphics2D
【发布时间】:2011-09-08 04:00:35
【问题描述】:

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

当前绘制的法线 Line2D 和 Arc2D,

想这样装饰,

到目前为止我的想法,

我可以通过创建两条不同的线并从我的正常线位置给出偏移 +gap 和 -gap 来实现这一点。但是,这会产生很多我不想做的对象。

现在,可以像这样让我的法线变粗吗,

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

有可能实现吗?如果是,请给我一些指导。

感谢您的任何帮助。

【问题讨论】:

  • “这会产生很多对象”“第二行”(或其中的 100 个,对于一百个主要行)在内存中并不多。跨度>

标签: java graphics java-2d graphics2d


【解决方案1】:

使用BasicStroke 绘制两次,越粗越细。

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)));
            }
        });
    }
}

【讨论】:

  • @dacwe 你期待什么颜色?你建议什么颜色?
【解决方案2】:

可以实现Stroke接口创建CompositeStroke,如图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);
            }
        });
    }
}

【讨论】:

  • 我在代码和屏幕截图之前投票赞成!我可以再投票一次吗?很好的抗锯齿功能。
  • 非常感谢,我希望 StackOverFlow 让用户选择他们想要授予多少声誉 :) 就像我面前有两个答案,它们都很好。
猜你喜欢
  • 1970-01-01
  • 2011-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-17
相关资源
最近更新 更多