Java Swing O Reilly (Web hosting) } return -1; }
Java Swing O Reilly } return -1; } public void paint(Graphics g, JComponent c) { Dimension size = c.getSize(); originX = size.width / 2; originY = size.height / 2; int diameter = (originX < originY ? size.width - 40 : size.height - 40); radius = (diameter / 2) + 1; int cornerX = (originX - (diameter / 2)); int cornerY = (originY - (diameter / 2)); int startAngle = 0; int arcAngle = 0; for (int i = 0; i < values.length; i++) { arcAngle = (int)(i < values.length - 1 ? Math.round(values[i] * 360) : 360 - startAngle); g.setColor(colors[i % colors.length]); g.fillArc(cornerX, cornerY, diameter, diameter, startAngle, arcAngle); drawLabel(g, labels[i], startAngle + (arcAngle / 2)); startAngle += arcAngle; } g.setColor(Color.black); g.drawOval(cornerX, cornerY, diameter, diameter); // cap the circle } public void drawLabel(Graphics g, String text, double angle) { g.setFont(textFont); g.setColor(textColor); double radians = angle * d2r; int x = (int) ((radius + 5) * Math.cos(radians)); int y = (int) ((radius + 5) * Math.sin(radians)); if (x < 0) { x -= SwingUtilities.computeStringWidth(g.getFontMetrics(), text); } if (y < 0) { y -= g.getFontMetrics().getHeight(); } g.drawString(text, x + originX, originY - y); } public static ComponentUI createUI(JComponent c) { return chartUI; } } There's nothing really complex here; it's just a lot of trigonometry and a little bit of simple AWT drawing. paint() is called with a graphics context and a JComponent as arguments; the JComponent allows you to figure out the size of the area we have to work with. And here's the code for the popup containing the chart: // - 465 -
In case you need quality webspace to host and run your web applications, try our personal web hosting services.