简体   繁体   中英

Java awt draw circle border

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

您可以尝试用drawOval代替fillOval

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM