Java 矩形边框

发布于 2024-12-11 03:05:08 字数 211 浏览 0 评论 0原文

我有一些由 矩形 类创建的形状,我想用粗边框包围它们。然而,Graphics 类中的方法drawRect 和drawOval 创建了一条细线作为形状的边框。我如何调整它们以便能够操纵边框线的粗细?如果这不可能或非常有效,那么在形状上分配可调整边框的另一种方法是什么?我需要 Rectangle2D 或 Graphics2D 吗?

之后,你知道如何将正方形边框的角度“圆化”,使其不那么尖锐吗?

I have some shapes created by class Rectangle and I want to surround them with a thick border. However the methods drawRect and drawOval form Graphics class create a thin line as the border of the shape. How can I adjust them so as me to able to manipulate the thickness of the border line? If this is not possible or quite effective, what is another way to assign an adjustable border on the shapes? May I need Rectangle2D or Graphics2D?

After that, do you know how I can “round” the angles of the border of a square so as not to be sharp?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七颜 2024-12-18 03:05:08

要使边框更粗,请使用 Graphics2D.setStroke(...)。要绘制“圆角”矩形,请使用 Graphics.drawRoundRect(...)

To make the border thicker, use Graphics2D.setStroke(...). And to draw "rounded" rectangles, use Graphics.drawRoundRect(...).

失而复得 2024-12-18 03:05:08

查看 Graphics2D 笔画:

如果一轮加入你的笔画不够柔软,看看 RoundRectangle2D

Look into Graphics2D strokes:

If a round join in your stroke isn't soft enough, look into RoundRectangle2D.

转瞬即逝 2024-12-18 03:05:08

我实现了图标的自定义圆形。

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

}

I implemented custom rounded shape for icons.

1) The thick border can be painted by :

BasicStroke dashed =new BasicStroke(3.0f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER,10.0f);

2) Rounded shape could be painted by:

Ellipse2D.Double circle = new Ellipse2D.Double(x+1, y+1, 14, 14);
Ellipse2D.Double circleBorder = new Ellipse2D.Double(x, y, 15, 15);

All code is here:

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

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文