Java awt 绘制圆形边框

发布于 2024-10-02 02:00:18 字数 562 浏览 1 评论 0原文

使用java awt,如何让代码绘制一个圆的边框。以后的代码会有性能问题,如果只画出轮廓,也许会运行得更快。


g.fillOval(gameLogic.getParticleXCoor(i) - 3, gameLogic.getParticleYCoor(i) - 3, gameLogic.getParticleSize(i) + 6, gameLogic.getParticleSize(i) + 6); g.setColor(gameLogic.getParticleColor(i)); g.fillOval(gameLogic.getParticleXCoor(i), gameLogic.getParticleYCoor(i), gameLogic.getParticleSize(i), gameLogic.getParticleSize(i));

Using java awt, how to make the code draw a border for a circle. The henceforth code has performance issues, and maybe it would run faster if it just painted the outline.


g.fillOval(gameLogic.getParticleXCoor(i) - 3, gameLogic.getParticleYCoor(i) - 3, gameLogic.getParticleSize(i) + 6, gameLogic.getParticleSize(i) + 6); g.setColor(gameLogic.getParticleColor(i)); g.fillOval(gameLogic.getParticleXCoor(i), gameLogic.getParticleYCoor(i), gameLogic.getParticleSize(i), gameLogic.getParticleSize(i));

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

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

发布评论

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

评论(2

云仙小弟 2024-10-09 02:00:19

您可以尝试使用 drawOval 而不是 fillOval

You could try drawOval instead of fillOval.

耳钉梦 2024-10-09 02:00:19

如果你想绘制圆形,请使用Ellipse2D类:

Ellipse2D.Double circleBorder = new Ellipse2D.Double(x, y, 15, 15); 

只需从Graphics2D调用draw()方法后,

g2.draw(circleBorder);

绘制圆形图标的完整代码示例如下:

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

If you want paint circle use Ellipse2D class:

Ellipse2D.Double circleBorder = new Ellipse2D.Double(x, y, 15, 15); 

After just call draw() methid from Graphics2D

g2.draw(circleBorder);

Full code for draw circle icons as example is here:

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