package king.yang;
import java.util.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.applet.Applet;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* 不想再改了,算了
* @author Administrator
*
*/
public class GraphVersion extends JFrame {
int depth;
int radius;
public static void main(String[] args) {
new GraphVersion();
}
public GraphVersion() {
setSize(600,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
//设置窗口显示位置为屏幕中央
Dimension dm = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(dm.width/2-this.getWidth()/2, dm.height/2-this.getHeight()/2);
float value;
String at;
//根据屏幕的大小,决定饼图的半径和高度
radius = this.getWidth()>this.getHeight() ? this.getHeight()/3 : this.getWidth()/3;
depth = this.getWidth()>this.getHeight() ? this.getHeight()/8 : this.getWidth()/8;
//饼图有几块,是什么颜色
at = "1-red,2-green,3-blue,4-yellow";
//画饼图
PieChartCanvas c = new PieChartCanvas(radius, depth);
// Create Hashtable to map color name (String) to Color type
Hashtable colors = new Hashtable();
colors.put("green", Color.green);
colors.put("red", Color.red);
colors.put("blue", Color.blue);
colors.put("yellow", Color.yellow);
colors.put("magenta", Color.magenta);
colors.put("cyan", Color.cyan);
colors.put("orange", Color.orange);
colors.put("pink", Color.pink);
colors.put("white", Color.white);
colors.put("black", Color.black);
// "value-color,value-color,..."
StringTokenizer t = new StringTokenizer(at, ",");
String s;
float values[] = new float[10];
Color myColor[] = new Color[10];
int j=0;
int count = 0;
int i;
while (t.hasMoreTokens()) {
s = t.nextToken();
i = s.indexOf('-');
value = Float.valueOf(s.substring(0, i)).floatValue();
values[j] = value;
c.addSlice(value, (Color)colors.get(s.substring(i + 1)));
myColor[j] = (Color)colors.get(s.substring(i + 1));
j++;
count++;
}
//System.out.println("coutn"+count);
add(c,"Center");
JPanel jp1 = new JPanel();
JLabel j1 = new JLabel("销量图");
jp1.add(j1);
JPanel jp2 = new JPanel();
add(jp1,"North");
add(jp2,"East");
}
}
class PieChartCanvas extends Canvas implements MouseListener,MouseMotionListener{
/*
**
*/
boolean[] isPaint = new boolean[10];
boolean isDoubleClicked = false;
Robot robot;
static int counter = 0;
int moveX = 0;
int moveY = 0;
int leftExcursion = 40;
int downExcursion = 10;
final double aspectFudge = 2.5;
int radius;
int depth;
int called = 1;
int numSlices = 0;
float total = 0;
float value[] = new float[10];
Color color[] = new Color[10];
int left = getWidth()/10;
int right = getWidth()/10;
int top = getHeight()/5;
int down = getHeight()/5;
int MyWidth = this.getWidth()-left-right;;
int MyHeight = this.getHeight()-top-down;
Graphics2D offGraphics = null;
Image gfxBuff;
// BackDeal bd;
public PieChartCanvas(int radius, int depth) {
this.radius = radius;
gfxBuff = new BufferedImage(400, 400, BufferedImage.TYPE_INT_BGR);
// bd = new BackDeal(gfxBuff, this);
// bd.start();
this.depth = depth;
this.addMouseListener(this);//增加监听器
this.addMouseMotionListener(this);
for(int i=0; i<10; i++) {
isPaint[i] = true;
}
}
//重写update,禁止JVM在擦除背景,在paint中使用双缓冲技术,可以消除闪烁现象
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g) {
// new BackDeal(this).start();
bufferedImageDraw();
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(gfxBuff, 0, 0, null);
}
public void bufferedImageDraw() {
int startAngle;
float angle;
left = getWidth()/10;
right = getWidth()/5;
top = getHeight()/5;
down = getHeight()/5;
RenderingHints qualityHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
MyWidth = this.getWidth()-left-right;;
MyHeight = this.getHeight()-top-down;
Dimension d = getSize();
gfxBuff = createImage(d.width, d.height);
offGraphics = (Graphics2D)gfxBuff.getGraphics();
//offGraphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.8f));
offGraphics.setRenderingHints(qualityHints);
offGraphics.setColor(getBackground());
offGraphics.fillRect(0, 0, d.width, d.height);
int step = this.getHeight()/10;
int blank = this.getHeight()/20;
int myY = this.getHeight()/3;
for(int k=0; k<this.numSlices; k++)
{
offGraphics.setColor(color[k]);
offGraphics.fillRect(this.getWidth()-this.getWidth()/8, myY, blank, blank);
offGraphics.drawString(Integer.toString((int)value[k])+"月份", this.getWidth()-this.getWidth()/12, myY+step/5);
myY += step;
}
// do the 3d effect
for(int x = depth; x >= 1; x--) {
startAngle = -45;
for(int i = 0; i < numSlices; i++) {
offGraphics.setColor(color[i].darker());
angle = Math.round(360 * (value[i] / total));
switch(i)
{
case 0:
if(isPaint[i]) {
offGraphics.fillArc(left, top+x, MyWidth, MyHeight,startAngle, (int)angle);
} else {
offGraphics.fillArc(left+leftExcursion, top+x+downExcursion, MyWidth, MyHeight,startAngle, (int)angle);
}
break;
case 1:
if(isPaint[i]) {
offGraphics.fillArc(left, top+x, MyWidth, MyHeight,startAngle, (int)angle);
} else {
offGraphics.fillArc(left+leftExcursion, top+x-downExcursion, MyWidth, MyHeight,startAngle, (int)angle);
}
break;
case 2:
if(isPaint[i]) {
offGraphics.fillArc(left, top+x, MyWidth, MyHeight,startAngle, (int)angle);
} else {
offGraphics.fillArc(left-leftExcursion, top+x-downExcursion, MyWidth, MyHeight,startAngle, (int)angle);
}
break;
case 3:
if(isPaint[i]) {
offGraphics.fillArc(left, top+x, MyWidth, MyHeight,startAngle, (int)angle);
} else {
offGraphics.fillArc(left-leftExcursion, top+x+downExcursion, MyWidth, MyHeight,startAngle, (int)angle);
}
break;
}
startAngle += angle;
}
}
// draw the pie slice
startAngle = -45;
for(int i = 0; i < numSlices; i++) {
offGraphics.setColor(color[i]);
angle = Math.round(360 * (value[i] / total));
switch(i)
{
case 0:
if(isPaint[i]) {
offGraphics.fillArc(left, top, MyWidth, MyHeight,startAngle, (int)angle);
} else {
offGraphics.fillArc(left+leftExcursion, top+downExcursion, MyWidth, MyHeight,startAngle, (int)angle);
}
break;
case 1:
if(isPaint[i]) {
offGraphics.fillArc(left, top, MyWidth, MyHeight,startAngle, (int)angle);
} else {
offGraphics.fillArc(left+leftExcursion, top-downExcursion, MyWidth, MyHeight,startAngle, (int)angle);
}
break;
case 2:
if(isPaint[i]) {
offGraphics.fillArc(left, top, MyWidth, MyHeight,startAngle, (int)angle);
} else {
offGraphics.