我有一些由Rectangle类创建的形状,我想用粗边框包围它们。但是方法drawRect和drawOval形式的Graphics类创建一条细线作为形状的边框。我怎样才能调整它们以便能够操纵边界线的厚度?如果这不可能或非常有效,那么在形状上分配可调边框的另一种方法是什么?我可能需要Rectangle2D或Graphics2D吗?
在那之后,你知道我怎么能“围绕”正方形边界的角度,以免尖锐吗?
答案 0 :(得分:3)
要使边框更粗,请使用Graphics2D.setStroke(...)
。要绘制“圆角”矩形,请使用Graphics.drawRoundRect(...)
。
答案 1 :(得分:3)
答案 2 :(得分:0)
我为图标实现了自定义的圆形。
1)粗边框可以通过以下方式绘制:
BasicStroke dashed =new BasicStroke(3.0f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER,10.0f);
2)可以通过以下方式绘制圆形:
Ellipse2D.Double circle = new Ellipse2D.Double(x+1, y+1, 14, 14);
Ellipse2D.Double circleBorder = new Ellipse2D.Double(x, y, 15, 15);
所有代码都在这里:
public class ColorIcon implements Icon {
private Color color = Color.WHITE;
/**
* Constructor for implement custom colored icon
* @param color - custom parameter for creating colored icon.
*/
public ColorIcon(@Nonnull Color color) {
this.color = color;
}
/**
* Default constructor for implement default icon.
*/
public ColorIcon() {
}
@Override
public void paintIcon(@Nonnull Component c, @Nonnull Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D) g;
RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
BasicStroke dashed =new BasicStroke(3.0f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER,10.0f);
Ellipse2D.Double circle = new Ellipse2D.Double(x+1, y+1, 14, 14);
Ellipse2D.Double circleBorder = new Ellipse2D.Double(x, y, 15, 15);
g2.setColor(getColor());
g2.setRenderingHints(hints);
g2.fill(circle);
Composite oldComposite=g2.getComposite();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0));
g2.setColor(new Color(1,1,1,1));
g2.setStroke(dashed);
g2.draw(circleBorder);
g2.setComposite(oldComposite);
}
@Override
public int getIconWidth() {
return 15;
}
@Override
public int getIconHeight() {
return 15;
}
public Color getColor() {
return color;
}
public void setColor(@Nonnull Color color) {
this.color = color;
}
}