• title: Windowの形を変更 tags: [JFrame, Shape, TextLayout] author: aterai pubdate: 2011-12-19T19:46:54+09:00 description: Windowの形を非矩形図形に変更します。

概要

Windowの形を非矩形図形に変更します。

サンプルコード

JFrame frame = new JFrame();
frame.setUndecorated(true);
frame.setAlwaysOnTop(true);
frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
frame.getContentPane().add(label);
frame.getContentPane().setBackground(Color.GREEN);
frame.pack();

String str = textField.getText().trim();
//label.setText(str);
TextLayout tl = new TextLayout(str, font, frc);
Rectangle2D b = tl.getBounds();
Shape shape = tl.getOutline(AffineTransform.getTranslateInstance(-b.getX(),-b.getY()));

frame.setBounds(shape.getBounds());
//frame.setSize(shape.getBounds().width, shape.getBounds().height);
com.sun.awt.AWTUtilities.setWindowShape(frame, shape);
//frame.setShape(shape); // 1.7.0
frame.setLocationRelativeTo(parent);
frame.setVisible(true);
View in GitHub: Java, Kotlin

解説

上記のサンプルでは、com.sun.awt.AWTUtilities.setWindowShape(...)メソッドを使用して、JFrameの形を変更しています。

  • Java 1.7.0の場合は、Window#setShape(Shape)を使用

参考リンク

コメント