How to draw a filled circle in Java?

前端 未结 2 1294
傲寒
傲寒 2020-12-15 21:13

I have a JPanel with a Grid Layout. In the \"cells\" of the grid I can put different elements (for example JButtons). There is no problems with that. But now I want to put a

相关标签:
2条回答
  • 2020-12-15 21:16
    /***Your Code***/
    public void paintComponent(Graphics g){
    /***Your Code***/
        g.setColor(Color.RED);
        g.fillOval(50,50,20,20);
    }
    
    g.fillOval(x-axis,y-axis,width,height);
    
    0 讨论(0)
  • 2020-12-15 21:31
    public void paintComponent(Graphics g) {
       super.paintComponent(g);
       Graphics2D g2d = (Graphics2D)g;
       // Assume x, y, and diameter are instance variables.
       Ellipse2D.Double circle = new Ellipse2D.Double(x, y, diameter, diameter);
       g2d.fill(circle);
       ...
    }
    

    Here are some docs about paintComponent (link).

    You should override that method in your JPanel and do something similar to the code snippet above.

    In your ActionListener you should specify x, y, diameter and call repaint().

    0 讨论(0)
提交回复
热议问题