|
1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.util.swing; |
|
27 |
|
28 import java.awt.*; |
|
29 import javax.swing.Icon; |
|
30 |
|
31 public class CroppedPaddedIcon implements Icon { |
|
32 // |
|
33 // Instance data |
|
34 // |
|
35 |
|
36 private Icon icon; |
|
37 private int height = -1; |
|
38 private int width = -1; |
|
39 |
|
40 // |
|
41 // Constructors |
|
42 // |
|
43 |
|
44 public CroppedPaddedIcon(Icon icon) { |
|
45 setIcon(icon); |
|
46 } |
|
47 |
|
48 public CroppedPaddedIcon(Icon icon, int width, int height) { |
|
49 this(icon); |
|
50 setIconWidth(width); |
|
51 setIconHeight(height); |
|
52 } |
|
53 |
|
54 // |
|
55 // Icon methods |
|
56 // |
|
57 |
|
58 @Override |
|
59 public int getIconHeight() { |
|
60 if (height >= 0) { |
|
61 return height; |
|
62 } |
|
63 Icon icon = getIcon(); |
|
64 return icon == null ? 0 : getIcon().getIconHeight(); |
|
65 } |
|
66 |
|
67 @Override |
|
68 public int getIconWidth() { |
|
69 if (width >= 0) { |
|
70 return width; |
|
71 } |
|
72 Icon icon = getIcon(); |
|
73 return icon == null ? 0 : getIcon().getIconWidth(); |
|
74 } |
|
75 |
|
76 @Override |
|
77 public void paintIcon(Component c, Graphics g, int x, int y) { |
|
78 Icon icon = getIcon(); |
|
79 if (icon != null) { |
|
80 g.translate(x, y); |
|
81 |
|
82 int width = getIconWidth(); |
|
83 int height = getIconHeight(); |
|
84 int iWidth = icon.getIconWidth(); |
|
85 int iHeight = icon.getIconHeight(); |
|
86 |
|
87 int newX = (width - iWidth) / 2; |
|
88 int newY = (height - iHeight) / 2; |
|
89 |
|
90 Shape clip = g.getClip(); |
|
91 g.setClip(0, 0, width, height); |
|
92 |
|
93 icon.paintIcon(c, g, newX, newY); |
|
94 |
|
95 g.setClip(clip); |
|
96 g.translate(-x, -y); |
|
97 } |
|
98 } |
|
99 |
|
100 // |
|
101 // CroppedPaddedIcon methods |
|
102 // |
|
103 |
|
104 public Icon getIcon() { |
|
105 return icon; |
|
106 } |
|
107 |
|
108 public void setIcon(Icon icon) { |
|
109 this.icon = icon; |
|
110 } |
|
111 |
|
112 public void setIconHeight(int height) { |
|
113 this.height = height; |
|
114 } |
|
115 |
|
116 public void setIconWidth(int width) { |
|
117 this.width = width; |
|
118 } |
|
119 } |