• 追加された行はこの色です。
  • 削除された行はこの色です。
---
category: swing
folder: WindowShape
title: Windowの形を変更
tags: [JFrame, Shape, TextLayout]
author: aterai
pubdate: 2011-12-19T19:46:54+09:00
description: Windowの形を非矩形図形に変更します。
description: JFrameのタイトルバーなどを非表示にし、Windowの形を非矩形図形に変更します。
image: https://lh4.googleusercontent.com/-f54GogC4jCU/Tu7AbPCJhsI/AAAAAAAABGc/EzG0Tf9ITFI/s800/WindowShape.png
---
* 概要 [#o8807d1b]
`Window`の形を非矩形図形に変更します。
* 概要 [#summary]
`JFrame`のタイトルバーなどを非表示にし、`Window`の形を非矩形図形に変更します。

#download(https://lh4.googleusercontent.com/-f54GogC4jCU/Tu7AbPCJhsI/AAAAAAAABGc/EzG0Tf9ITFI/s800/WindowShape.png)

* サンプルコード [#k7ecdb7d]
* サンプルコード [#sourcecode]
#code(link){{
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()));
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
// JDK 1.6.0: com.sun.awt.AWTUtilities.setWindowShape(frame, shape);
frame.setShape(shape);
frame.setLocationRelativeTo(parent);
frame.setVisible(true);
}}

* 解説 [#u7d38289]
上記のサンプルでは、`com.sun.awt.AWTUtilities.setWindowShape(...)`メソッドを使用して、`JFrame`の形を変更しています。
* 解説 [#explanation]
上記のサンプルでは、`JDK 1.7.0`で導入された`Window#setShape(Shape)`メソッドを使用して`JFrame`の形を変更しています。

- `Java 1.7.0`の場合は、`Window#setShape(Shape)`を使用
- `JDK 1.6.0_10`の場合は`com.sun.awt.AWTUtilities.setWindowShape(...)`を使用する

* 参考リンク [#c00b334c]
- [http://blogs.oracle.com/thejavatutorials/entry/translucent_and_shaped_windows_in Translucent and Shaped Windows in JDK7 (The Java Tutorials' Weblog)]
* 参考リンク [#reference]
- [https://blogs.oracle.com/thejavatutorials/entry/translucent_and_shaped_windows_in Translucent and Shaped Windows in JDK7 (The Java Tutorials' Weblog)]
- [https://docs.oracle.com/javase/jp/8/docs/api/java/awt/Window.html#setShape-java.awt.Shape- Window#setShape(Shape) (Java Platform SE 8)]

* コメント [#l592ec36]
* コメント [#comment]
#comment
#comment