问题描述
我通过 Graphics2D 绘图在我的 JPanel 上布置了 Line2D 和 Arc2D 对象.你可以看看这个问题的一部分如何制作像素完美的 Line2D in - Graphics2D".现在我想要实现的是,我想为所有 Line2D 和 Arc2D 对象创建两条平行线和弧.视觉上,
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,
当前绘制的法线 Line2D 和 Arc2D,
Normal Line2D and Arc2D drawn currently,
想装饰成这样,
到目前为止我的想法,
我可以通过创建两条不同的线并从我的正常线位置给出偏移量 +gap 和 -gap 来实现这一点.但是,这会产生很多我不想要的对象.
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,
给它们一个边框并从中删除中间位?
有可能实现吗?如果是,请给我一些指导.
Is it possible to achieve this? if yes, May I please have some direction.
感谢您的任何帮助.
推荐答案
使用 BasicStroke
并绘制两次,粗一点和细一点.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!