Java – Change the JButton’s shape

java, jbutton, shape, swing

I'm making the game Master Mind, I've filled my matrix up with JButtons so people can click them to change the color.

Now I want to change the shape of my rectangular buttons to circles, is there a way I can change them all at once since I worked with a loop to create all them.

Best Solution

Here are some methods that have to be overwritten to edit the shape of a component. (Including sample code)

protected void paintComponent(Graphics g)   {    if (getModel().isArmed()) {      g.setColor(Color.lightGray);    } else {      g.setColor(getBackground());    }    g.fillOval(0, 0, getSize().width-1,getSize().height-1);    super.paintComponent(g);  }  protected void paintBorder(Graphics g) {    g.setColor(getForeground());    g.drawOval(0, 0, getSize().width-1,     getSize().height-1);  }  Shape shape;  public boolean contains(int x, int y) {    if (shape == null ||       !shape.getBounds().equals(getBounds())) {      shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());    }    return shape.contains(x, y);  }