import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jfree.chart.*;
import org.jfree.chart.axis.CategoryAxis3D;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis3D;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;


public class UpdateServer extends Thread
{
	private SeverList serverlist_list = null;
	private SeverList serverlist = null;
	private File file =null;
	
	public UpdateServer()
	{
		this.serverlist = main_program.serverlist_head;	
		this.serverlist_list = this.serverlist;
	}
	
	public void run() 
	{
		//恢复内存
		try {
			//进入死循环前的初始化
			file = new File("e:\\a.png");
			//折线图数据
	        DefaultCategoryDataset lineDataset = new DefaultCategoryDataset();
	      //柱状图数据
	         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
	         //生成的柱状图
	        JFreeChart chart = ChartFactory.createBarChart3D(
	                   "虚拟服务器任务调度情况",
	                   "虚拟服务器节点",//X轴的标签 
	                   "内存使用率",//Y轴的标签 
	                   dataset, //图标显示的数据集合
	                   PlotOrientation.VERTICAL, //图像的显示形式（水平或者垂直）
	                   false,//是否显示子标题 
	                   false,//是否生成提示的标签 
	                   false); //是否生成URL链接

	            /*
	             * 处理图形上的乱码
	             */

	            //处理主标题的乱码
	            chart.getTitle().setFont(new Font("黑体",Font.BOLD,18));

	            //获取图表区域对象
	            CategoryPlot categoryPlot = (CategoryPlot)chart.getPlot();

	            //获取X轴的对象
	            CategoryAxis3D categoryAxis3D = (CategoryAxis3D)categoryPlot.getDomainAxis();

	            //获取Y轴的对象
	            NumberAxis3D numberAxis3D = (NumberAxis3D)categoryPlot.getRangeAxis();

	            //处理X轴上的乱码
	            categoryAxis3D.setTickLabelFont(new Font("黑体",Font.BOLD,10));

	            //处理X轴外的乱码
	            categoryAxis3D.setLabelFont(new Font("黑体",Font.BOLD,10));

	            //处理Y轴上的乱码
	            numberAxis3D.setTickLabelFont(new Font("黑体",Font.BOLD,10));

	            //处理Y轴外的乱码
	            numberAxis3D.setLabelFont(new Font("黑体",Font.BOLD,10));

	            //自定义Y轴上显示的刻度，以10作为1格
	            numberAxis3D.setAutoTickUnitSelection(false);
	            NumberTickUnit unit = new NumberTickUnit(10); 
	            numberAxis3D.setTickUnit(unit);

	            //获取绘图区域对象
	            BarRenderer3D barRenderer3D = (BarRenderer3D)categoryPlot.getRenderer();

	            //设置柱形图的宽度
	            barRenderer3D.setMaximumBarWidth(0.07);

	            //在图形上显示数字
	            barRenderer3D.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
	            barRenderer3D.setBaseItemLabelsVisible(true);
	            barRenderer3D.setBaseItemLabelFont(new Font("宋体",Font.BOLD,10));

	            /*
	             * 放折线图数据
	             */
	            categoryPlot.setDataset(1, lineDataset);
	            //设置折线
	            LineAndShapeRenderer lineandshaperenderer = new LineAndShapeRenderer();
	            lineandshaperenderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
	            categoryPlot.setRenderer(1, lineandshaperenderer);
	            // 柱状图和纵轴紧靠
	            categoryAxis3D.setLowerMargin(0.0); 

	            categoryAxis3D.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
	            //折线在柱面前面显示
	            categoryPlot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);

	            //生成GUI
	            JFrame newFrame=new JFrame("funBox");       
	            newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   //定义JFrame关闭时的操作（必需），有效避免不能关闭后台当前框体进程的问题    
	            newFrame.setSize(1050, 850);      //定义JFrame的相关属性
	            newFrame.setLocation(200, 200);
	            newFrame.setVisible(true); 
	            
	            //初始化保存图片缓冲
	            BufferedImage image = null;
	          
			while(! AllocateTask.task_finish)
			{
				 lineDataset.clear();
				 dataset.clear();
		         while(serverlist_list.getNext() != null)
		         {
		        	 serverlist_list = serverlist_list.getNext();
		        	 //第一个参数是数量,第二个参数是图片显示的文字,第三个参数是横坐标,这是控制折线图
		            lineDataset.addValue(100, "", serverlist_list.get_host_name()+":"+serverlist_list.exec_task);
		            //添加数据，控制柱状图
		            dataset.addValue(serverlist_list.print_rate(), "", serverlist_list.get_host_name()+":"+serverlist_list.exec_task);
		            //实时打印
		           // System.out.println(serverlist_list.get_host_name()+":"+serverlist_list.print_rate()+"%");
		         }
		         
		         //保存图片  
		          image = chart.createBufferedImage(1000, 800,BufferedImage.TYPE_INT_RGB, null);  
		         
		            
		           //更新图片
		            ImagePanel mypanel = new ImagePanel();
		            mypanel.setImage(image);
		            Container con = newFrame.getContentPane(); 
		            con.add(mypanel, BorderLayout.CENTER); 
		            //一定要重新更新完要显示
		            newFrame.setVisible(true); 
		            
				//恢复server_list为开头节点
		          this.serverlist_list = this.serverlist;
		         
				  Thread.sleep(10);
				  image.flush();
				  newFrame.getContentPane().remove(mypanel);
			}
			
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	

}
