Java awt绘制圆边框

时间:2010-11-12 15:18:31

标签: java awt

使用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));

2 个答案:

答案 0 :(得分:1)

您可以尝试使用drawOval代替fillOval

答案 1 :(得分:0)

如果要绘制圆圈,请使用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);
}