0

I have an undecorated JFrame in the shape of an ellipse, that I would like to add a border.

I am hoping I don't have to implement the rootPane.paintComponent method, and that I can just do this by adding a border.

Is this possible in Java 7 or 8?

vinnygray
  • 171
  • 1
  • 6

1 Answers1

2

In your implementation of paintComponent(), use setClip() with an Ellipse2D sized to match the image's width and height.

private Ellipse2D.Double border = new Ellipse2D.Double();
…
public void paintComponent(Graphics g) {
    super.paintComponent()
    Graphics2D g2d = (Graphics2D) g;
    …
    int width = getWidth();
    int height = getHeight();
    g2d.setPaint(…);
    g2d.fillRect(0, 0, width, height);
    border.setFrame(0, 0, width, height);
    g2d.setClip(border);
    g2d.drawImage(image, 0, 0, width, height, this);
}

Also override getPreferredSize(), as shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • So there's no way to do it using borders? I was hoping to use the stock selection of bevel and etched, etc – vinnygray Apr 08 '15 at 16:16
  • You can try implementing `Border`, but I see no advantage; see also this [Q&A](http://stackoverflow.com/q/24850292/230513). – trashgod Apr 08 '15 at 21:01