How can I add an elliptical border to a JFrame

拥有回忆 提交于 2020-01-16 11:29:06

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/29505474/how-can-i-add-an-elliptical-border-to-a-jframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!