java 在Java中生成矩形边框的问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2027389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problem in generating the border of a rectangle in Java?
提问by Yatendra Goel
I am using java.awt.geom.Rectangle2D.Doubleclass to generate a rectangle. I want to generate a rectangle which is filled with a color (say green) and have a border (outline).
我正在使用java.awt.geom.Rectangle2D.Double类来生成一个矩形。我想生成一个填充颜色(比如绿色)并有边框(轮廓)的矩形。
Now the problem is if I call
现在的问题是如果我打电话
g2.draw(new Rectangle2D.Double(....)); // g2 is an instance of Graphics2D
then it doesn't fill the rectangle and when I call
然后它不会填充矩形,当我打电话时
g2.fill(new Rectangle2D.Double(....)); // g2 is an instance of Graphics2D
then id doesn't generate border.
那么 id 不会生成边框。
采纳答案by Dan Dyer
How about doing both? Draw the filled rectangle first and then draw the outline one over the top.
两个都做怎么样?先绘制实心矩形,然后在顶部绘制轮廓。
回答by Samuel Sj?berg
To do this, render the rectangle twice, first the fill and then the border (draw).
为此,将矩形渲染两次,首先是填充,然后是边框(绘制)。
Rectangle2D rect = new Rectangle2D.Double(...);
g2.setColor(Color.white);
g2.fill(rect);
g2.setColor(Color.black);
g2.draw(rect);

