Swing/RotatedIcon のバックアップの現在との差分(No.1)
TITLE:Iconを回転する
Posted by aterai at 2012-06-11
Iconを回転する
画像ファイルから90, 180, 270度回転したIconを作成します。- category: swing folder: RotatedIcon title: Iconを回転する tags: [Icon, AffineTransform] author: aterai pubdate: 2012-06-11T15:18:47+09:00 description: 画像ファイルから90、180、270度回転したIconを作成します。 image:
概要
画像ファイルから90
、180
、270
度回転したIcon
を作成します。
- &jnlp;
- &jar;
- &zip;
Screenshot
Advertisement
サンプルコード
#spanend
#spanadd
* サンプルコード [#sourcecode]
#spanend
#spanadd
#code(link){{
#spanend
class RotateIcon implements Icon {
private int width, height;
private Image image;
private final Dimension d = new Dimension();
private final Image image;
private AffineTransform trans;
public RotateIcon(Icon icon, int rotate) {
image = new BufferedImage(
icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
protected RotateIcon(Icon icon, int rotate) {
if (rotate % 90 != 0) {
throw new IllegalArgumentException(
rotate + ": Rotate must be (rotate % 90 == 0)");
}
d.setSize(icon.getIconWidth(), icon.getIconHeight());
image = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_ARGB);
Graphics g = image.getGraphics();
icon.paintIcon(null, g, 0, 0);
g.dispose();
width = icon.getIconWidth();
height = icon.getIconHeight();
if((rotate+360)%360==90) {
trans = AffineTransform.getTranslateInstance(height, 0);
trans.rotate(Math.toRadians(90));
}else if((rotate+360)%360==270) {
trans = AffineTransform.getTranslateInstance(0, width);
trans.rotate(Math.toRadians(270));
}else{ // if((rotate+360)%360==180) {
//trans = AffineTransform.getTranslateInstance(width, height);
//trans.rotate(Math.toRadians(180));
trans = AffineTransform.getScaleInstance(1.0, -1.0);
trans.translate(0, -height);
width = icon.getIconHeight();
height = icon.getIconWidth();
int numquadrants = (rotate / 90) % 4;
if (numquadrants == 1 || numquadrants == -3) {
trans = AffineTransform.getTranslateInstance(d.height, 0);
int v = d.width;
d.width = d.height;
d.height = v;
} else if (numquadrants == -1 || numquadrants == 3) {
trans = AffineTransform.getTranslateInstance(0, d.width);
int v = d.width;
d.width = d.height;
d.height = v;
} else if (Math.abs(numquadrants) == 2) {
trans = AffineTransform.getTranslateInstance(d.width, d.height);
} else {
trans = AffineTransform.getTranslateInstance(0, 0);
}
trans.rotate(Math.toRadians(90d * numquadrants));
// trans.quadrantRotate(numquadrants);
}
#spanadd
#spanend
@Override public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D)g.create();
Graphics2D g2 = (Graphics2D) g.create();
g2.translate(x, y);
g2.drawImage(image, trans, c);
g2.translate(-x, -y);
g2.dispose();
}
@Override public int getIconWidth() {
return height;
#spanadd
#spanend
@Override public int getIconWidth() {
return d.width;
}
#spanadd
#spanend
@Override public int getIconHeight() {
return width;
return d.height;
}
}
解説
- Default
- 幅高さ: 83x100
- Rotate: 180
- 幅高さ: 83x100
- 上下反転: AffineTransformOpで画像を反転する
- Rotate: 90
- 幅高さ: 100x83(元画像の幅高さを入れ替え)
- 左上を原点に90度回転し、元画像の高さだけX軸プラス方向に移動
- Rotate: -90
- 幅高さ: 100x83(元画像の幅高さを入れ替え)
- 左上を原点に270度回転し、元画像の場だけY軸プラス方向に移動
解説
-
Default
- 幅高さ:
83x100
- 幅高さ:
-
Rotate
:180
- 幅高さ:
83x100
- 上下反転と同等: AffineTransformOpで画像を反転する
- 幅高さ:
-
Rotate
:90
(時計回りに90
度)- 幅高さ:
100x83
(元画像の幅高さを入れ替え) - 左上を原点に
90
度回転し、元画像の高さだけX
軸プラス方向に移動
- 幅高さ:
-
Rotate
:-90
(反時計回りに90
度)- 幅高さ:
100x83
(元画像の幅高さを入れ替え) - 左上を原点に
270
度回転し、元画像の幅だけY
軸プラス方向に移動
- 幅高さ:
参考リンク
- Duke Images: iconSized
- -
-
AffineTransform.getQuadrantRotateInstance(...)
メソッドを使用するサンプルはIconを指定した数の四分円だけ回転して表示するに移動
参考リンク
-
Duke Images: iconSized - Iconを指定した数の四分円だけ回転して表示する
- AffineTransformOpで画像を反転する
- Mouseで画像を移動、回転
- JTabbedPaneのタブタイトル文字列を回転して縦組表示する
- Animated Gifを回転して表示する