Line2D装饰提示需要 - Graphics2D [英] Line2D decoration tips needed - Graphics2D

查看:201
本文介绍了Line2D装饰提示需要 - Graphics2D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过Graphics2D绘图在我的JPanel上布置了Line2D和Arc2D对象。你可以看看这个问题的一部分如何使像素完美的Line2D in - Graphics2D 。现在我想实现的是,我想为所有Line2D和Arc2D对象创建两条平行线和弧线。视觉上,

正常Line2D和Arc2D当前绘制,



想要像这样装饰它,





到目前为止,我的想法



我可以通过创建两条不同的线来实现此目的,并给出偏移+我的正常位置。但是,这会使很多我不想要的对象。



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



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



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



谢谢你的帮助。

方案

使用 BasicStroke 并绘制两次,更粗更细。 src =https://i.stack.imgur.com/vC2RW.pngalt =一行绘制两次>

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

import javax.imageio.ImageIO;
import java.io.File;
$ b $ 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)));
}
});
}
}


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,

Want to decorate it like this,

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,

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.

解决方案

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

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

这篇关于Line2D装饰提示需要 - Graphics2D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆