Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

java - Line2D decoration tips needed - Graphics2D

I have Line2D and Arc2D objects laid out on my JPanel by Graphics2D drawing. You can have a look a part of it on this question " How to make pixel perfect Line2D in - Graphics2D ". Now what I want to achieve is, I want to create two parallel lines and arcs for all Line2D and Arc2D objects. Visually,

Normal Line2D and Arc2D drawn currently,

enter image description here

Want to decorate it like this,

enter image description here

My Thoughts so far,

I might be able to achieve this by creating two different line and give an offset +gap and -gap from my normal line position. However This will make lots of objects which I don't want to.

Now, is it possible to make my normal line thicker like this,

enter image description here

and give them a border and delete middle bit from it?

Is it possible to achieve this? if yes, May I please have some direction.

Thank you for any kind of help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use a BasicStroke and draw it twice, thicker and thinner.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...