1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import javax.swing.Icon;
import javax.swing.JLabel;
public class VerticalLabel extends JLabel {
public final static int ROTATE_RIGHT = 1;
public final static int DONT_ROTATE = 0;
public final static int ROTATE_LEFT = -1;
private int rotation = DONT_ROTATE;
private boolean painting = false;
public VerticalLabel() {
super();
}
public VerticalLabel(Icon icon) {
super(icon);
}
public VerticalLabel(String text) {
super(text);
}
public VerticalLabel(String text, Icon icon, int horizontalAlignment) {
super(text, icon, horizontalAlignment);
}
public VerticalLabel(String text, int horizontalAlignment) {
super(text, horizontalAlignment);
}
public VerticalLabel(Icon image, int horizontalAlignment) {
super(image, horizontalAlignment);
}
public int getRotation() {
return rotation;
}
public void setRotation(int rotation) {
this.rotation = rotation;
}
public boolean isRotated() {
return rotation != DONT_ROTATE;
}
@Override
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
if (isRotated()) {
g2d.rotate(Math.toRadians(90 * rotation));
}
if (rotation == ROTATE_RIGHT) {
g2d.translate(0, -this.getWidth());
} else if (rotation == ROTATE_LEFT) {
g2d.translate(-this.getHeight(), 0);
}
painting = true;
super.paint(g2d);
painting = false;
if (isRotated()) {
g2d.rotate(-Math.toRadians(90 * rotation));
}
if (rotation == ROTATE_RIGHT) {
g2d.translate(-this.getWidth(), 0);
} else if (rotation == ROTATE_LEFT) {
g2d.translate(0, -this.getHeight());
}
}
@Override
public Insets getInsets(Insets insets) {
insets = super.getInsets(insets);
if (painting) {
if (rotation == ROTATE_LEFT) {
int temp = insets.bottom;
insets.bottom = insets.left;
insets.left = insets.top;
insets.top = insets.right;
insets.right = temp;
} else if (rotation == ROTATE_RIGHT) {
int temp = insets.bottom;
insets.bottom = insets.right;
insets.right = insets.top;
insets.top = insets.left;
insets.left = temp;
}
}
return insets;
}
@Override
public Insets getInsets() {
Insets insets = super.getInsets();
if (painting) {
if (rotation == ROTATE_LEFT) {
int temp = insets.bottom;
insets.bottom = insets.left;
insets.left = insets.top;
insets.top = insets.right;
insets.right = temp;
} else if (rotation == ROTATE_RIGHT) {
int temp = insets.bottom;
insets.bottom = insets.right;
insets.right = insets.top;
insets.top = insets.left;
insets.left = temp;
}
}
return insets;
}
@Override
public int getWidth() {
if ((painting) && (isRotated())) {
return super.getHeight();
}
return super.getWidth();
}
@Override
public int getHeight() {
if ((painting) && (isRotated())) {
return super.getWidth();
}
return super.getHeight();
}
@Override
public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
if (isRotated()) {
int width = d.width;
d.width = d.height;
d.height = width;
}
return d;
}
@Override
public Dimension getMinimumSize() {
Dimension d = super.getMinimumSize();
if (isRotated()) {
int width = d.width;
d.width = d.height;
d.height = width;
}
return d;
}
@Override
public Dimension getMaximumSize() {
Dimension d = super.getMaximumSize();
if (isRotated()) {
int width = d.width;
d.width = d.height;
d.height = width;
}
return d;
}
} |