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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
| package test;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.awt.font.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class TestPie extends JPanel implements ComponentListener, MouseInputListener {
private static final double PI_2 = 2 * Math.PI;
/** Shared color array.
*/
private static final Color[] TABLE_COLOR = new Color[12];
/** Static initializer.
*/
static {
TABLE_COLOR[0] = new Color(100);
TABLE_COLOR[1] = new Color(500);
TABLE_COLOR[2] = new Color(105);
TABLE_COLOR[3] = new Color(150);
TABLE_COLOR[4] = new Color(440);
TABLE_COLOR[5] = new Color(777);
TABLE_COLOR[6] = new Color(268);
TABLE_COLOR[7] = new Color(631);
TABLE_COLOR[8] = new Color(237);
TABLE_COLOR[9] = new Color(001);
TABLE_COLOR[10] = new Color(010);
TABLE_COLOR[11] = new Color(070);
}
/** Shared stroke.
*/
private static final Stroke DEFAULT_STROKE = new BasicStroke(1f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
/** Shared stroke.
*/
private static final Stroke SELECTED_STROKE = new BasicStroke(3f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
private static final Font DEFAULT_FONT = new Font("Dialog", Font.PLAIN, 10);
/** List of values.
*/
private java.util.List<Number> valueList;
/** Sum of values.
*/
private double sum = 0;
private Arc2D selectedArc = null;
private java.util.List<Arc2D> shapeList;
private Map<Arc2D, Number> arcToValueMap = new HashMap<Arc2D, Number>();
/** Creates a new instance.
* @param values Initial values.
*/
public TestPie(Number ...values) {
super();
valueList = Arrays.asList(values);
//Calculate sum.
for (Number value : values) {
sum += value.doubleValue();
}
shapeList = new ArrayList<Arc2D>(values.length);
for (int i = 0; i < values.length; i++) {
Arc2D arc = new Arc2D.Double();
arc.setArcType(Arc2D.PIE);
shapeList.add(arc);
arcToValueMap.put(arc, values[i]);
}
addComponentListener(this);
addMouseListener(this);
addMouseMotionListener(this);
}
private void calculateShape() {
Dimension size = getSize();
Insets insets = getInsets();
// Get past (optional) panel border.
int width = size.width - (insets.left + insets.right);
int height = size.height - (insets.top + insets.bottom);
double diameter = Math.min(width, height) - 5;
// Don't bother if too small to draw.
if (diameter > 0) {
double theta = 0;
double extend = 0;
for (int i = 0; i < valueList.size(); i++) {
Arc2D arc = shapeList.get(i);
Number value = valueList.get(i);
arc.setFrame(insets.left + (width - diameter) / 2.0, insets.top + (height - diameter) / 2.0, diameter, diameter);
extend = 360 * value.doubleValue() / (double) sum;
arc.setAngleStart(theta);
arc.setAngleExtent(extend);
theta += extend;
}
}
}
/**
* {@inheritDoc}
*/
@Override protected void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension size = getSize();
Insets insets = getInsets();
// Get past (optional) panel border.
int width = size.width - (insets.left + insets.right);
int height = size.height - (insets.top + insets.bottom);
double diameter = Math.min(width, height) - 5;
// Don't bother if too small to draw.
if (diameter > 0) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
// XOR does not like antialiased text.
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
for (int i = 0; i < valueList.size(); i++) {
Arc2D arc = shapeList.get(i);
Color color = TABLE_COLOR[i];
g2d.setPaint(color);
g2d.fill(arc);
if (arc != selectedArc) {
g2d.setStroke(DEFAULT_STROKE);
g2d.setPaint(getForeground());
g2d.draw(arc);
}
}
// Draw selection.
if (selectedArc != null) {
g2d.setStroke(SELECTED_STROKE);
g2d.setPaint(getForeground().brighter().brighter());
g2d.draw(selectedArc);
}
// Draw labels.
double labelPosition = 2 / 3.0 * diameter / 2.0;
g2d.setPaint(getForeground());
for (int i = 0; i < valueList.size(); i++) {
Arc2D arc = shapeList.get(i);
Number value = valueList.get(i);
double theta = arc.getAngleStart() + arc.getAngleExtent() / 2.0;
theta = PI_2 * theta / 360.0;
double x = insets.left + width / 2.0 + labelPosition * Math.cos(theta);
double y = insets.top + height / 2.0 + labelPosition * Math.sin( -theta);
Font font = DEFAULT_FONT;
if (arc == selectedArc) {
font = font.deriveFont(14f);
}
String label = String.valueOf(value) + " (" + i + ")";
Rectangle2D stringBounds = font.getStringBounds(label, g2d.getFontRenderContext());
LineMetrics lineMetrics = font.getLineMetrics(label, g2d.getFontRenderContext());
g2d.setFont(font);
g2d.setXORMode(Color.WHITE);
g2d.drawString(label, (float) (x - stringBounds.getWidth() / 2.0), (float) (y + lineMetrics.getAscent() / 2.0));
g2d.setPaintMode();
}
}
}
/**{@inheritDoc}
*/
public void componentResized(ComponentEvent event) {
calculateShape();
repaint();
}
/**{@inheritDoc}
*/
public void componentMoved(ComponentEvent event) {
}
/**{@inheritDoc}
*/
public void componentShown(ComponentEvent event) {
}
/**{@inheritDoc}
*/
public void componentHidden(ComponentEvent event) {
}
/**{@inheritDoc}
*/
public void mouseClicked(MouseEvent event) {
}
/**{@inheritDoc}
*/
public void mousePressed(MouseEvent event) {
}
/**{@inheritDoc}
*/
public void mouseReleased(MouseEvent event) {
}
/**{@inheritDoc}
*/
public void mouseEntered(MouseEvent event) {
}
/**{@inheritDoc}
*/
public void mouseExited(MouseEvent event) {
}
/**{@inheritDoc}
*/
public void mouseDragged(MouseEvent event) {
}
/**{@inheritDoc}
*/
public void mouseMoved(MouseEvent event) {
selectedArc = null;
String tooltip = null;
for (Arc2D arc : shapeList) {
if (arc.contains(event.getX(), event.getY())) {
selectedArc = arc;
Number value = arcToValueMap.get(arc);
tooltip = String.valueOf(value);
break;
}
}
setToolTipText(tooltip);
repaint();
}
/** Self-testing main.
* @param args Arguments from the command-line.
*/
public static void main(String ...args) {
// Start at EDT.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TestPie testPie = new TestPie(20, 15, 50, 35, 5, 20);
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(testPie, BorderLayout.CENTER);
frame.setSize(320, 200);
frame.setVisible(true);
}
});
}
} |
Partager