applet画流量图(Line Chart) 流量数据由servert从服务器文件读取

LineChart.java(applet)

public class LineChart extends JApplet implements Runnable { int width = 500, height = 400; int plotWidth = 400, plotHeight = 300; ChartData indt = new ChartData("纵坐标:流量 单位:M", "横坐标:时间 单位:s", "网络流量图", 12); ChartData outdt = new ChartData("流量|单位:M", "时间单位:s", "网络流量图", 12); Image offImage; Graphics2D offGraphics; int dataLength = 0; Thread timer; String data="null"; String serverUrl="null"; String[] indata; String[] outdata; String[] temp; int index=0; byte tom[]=new byte[100000]; public void init() { offImage = createImage(width, height); offGraphics = (Graphics2D) offImage.getGraphics(); offGraphics.setColor(Color.white); offGraphics.fillRect(0, 0, width, height); //for test aim //ChartData cd = new ChartData("flow", "time", "network flow statistic chart", 12); //ChartData("flow","time","network flow statistic chart", 12); for (int i = 0; i < 12; i++) { //cd.addvalue(plotHeight - (65 + (Math.random() * 220)), i); indt.addvalue(plotHeight - (65 + (int) (Math.random() * 220)), i); } // this.indt = cd; for (int i = 0; i < 12; i++) { //cd.addvalue(plotHeight - (32 + (Math.random() * 210)), i); outdt.addvalue(plotHeight - (32 + (int) (Math.random() * 210)), i); } // this.outdt = cd; serverUrl = getParameter("serverUrl"); //serverUrl = "http://192.168.1.103:8080/MeishanMobile"; System.out.println("serverUrl:"+serverUrl); setLayout(new BorderLayout()); JButton saveButton = new JButton("保存流量图"); saveButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { // Overrides the exit method to not // call System.exit, but stop the // applet instead. Frame f=new fileDialog("File Dialog"); f.setVisible(false); f.setSize(100,100); FileDialog fd=new FileDialog(f,"File Dialog",FileDialog.SAVE); fd.setVisible(true); File f1 = new File(fd.getDirectory(),fd.getFile()); FileOutputStream writefile = new FileOutputStream(f1); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ImageIO.write((RenderedImage)offImage, "jpg", bos); writefile.write(bos.toByteArray(),0,bos.toByteArray().length); writefile.close(); } catch (Exception e1) { e1.printStackTrace(); } } }); add(saveButton, BorderLayout.SOUTH); } public void start() { timer = new Thread(this); timer.start(); } public void stop() { timer = null; } public void run() { Thread me = Thread.currentThread(); while (timer == me) { try { Thread.currentThread().sleep(5000); } catch (InterruptedException e) { } try{ if(index%11==0) { URL url = new URL(getCodeBase(), serverUrl+"/servlet/GetChartDataServerlet"); URLConnection con = url.openConnection(); con.setUseCaches(false); InputStream in = con.getInputStream(); DataInputStream textStream; textStream = new DataInputStream(in); data = textStream.readLine(); temp=data.split("&"); indata=temp[0].split(","); outdata=temp[1].split(","); index=0; } indt.addIndata(Integer.parseInt(indata[index])); outdt.addIndata(Integer.parseInt(outdata[index])); index++; }catch(Exception e){data=e.getMessage(); } repaint(); } } public void drawBackground() { int shadowOffset = 5; offGraphics.setColor(Color.BLACK); offGraphics.setFont(new Font("汉真广标", Font.PLAIN, 35)); offGraphics.drawString("演示流量图", 10, 40); offGraphics.setColor(Color.white); offGraphics.fillRect(0, 0, width, height); offGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); GradientPaint grayGP = new GradientPaint(0, 0, Color.GRAY, width, height, new Color(218, 214, 212), false); offGraphics.setPaint(grayGP); RoundRectangle2D.Float bgRR = new RoundRectangle2D.Float(shadowOffset, shadowOffset, width - shadowOffset, height - shadowOffset, 50, 50); offGraphics.fill(bgRR); GradientPaint blueGP = new GradientPaint(0, 0, new Color(93, 117, 151), 0, height, new Color(90, 114, 150), false); offGraphics.setPaint(blueGP); offGraphics.fillRoundRect(0, 0, width - shadowOffset, height - shadowOffset, 50, 50); BasicStroke bs = new BasicStroke(2.0f); offGraphics.setStroke(bs); offGraphics.setPaint(new Color(55, 71, 105)); offGraphics.drawRoundRect(0, 0, width - shadowOffset, height - shadowOffset, 50, 50); Rectangle2D.Float drawArea = new Rectangle2D.Float(83, 33, plotWidth, plotHeight); offGraphics.setPaint(new Color(27, 60, 105)); offGraphics.fill(drawArea); GradientPaint plotGP = new GradientPaint(120, 60, new Color(215, 230, 252), 120, 300, Color.WHITE, false); offGraphics.setPaint(plotGP); drawArea = new Rectangle2D.Float(80, 30, plotWidth, plotHeight); offGraphics.fill(drawArea); bs = new BasicStroke(1.2f); offGraphics.setStroke(bs); offGraphics.setPaint(Color.BLACK); offGraphics.draw(drawArea); offGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); } public void drawTitle() { offGraphics.setFont(new Font("宋体", Font.PLAIN, 18)); int titleLength = offGraphics.getFontMetrics().stringWidth(indt.gettitle()); offGraphics.setColor(Color.WHITE); offGraphics.drawString(indt.gettitle(), (width - titleLength) / 2, 22); } public void drawChart() { AffineTransform old = offGraphics.getTransform(); offGraphics.translate(80, 30); float[] dashes = {3.f}; BasicStroke bs = new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 10, dashes, 0); offGraphics.setStroke(bs); offGraphics.setFont(new Font("Courier New", Font.PLAIN, 12)); String str = ""; int stringLength = 0, stringHeight = 0; int xTickCounter = 12; int plotMargin = 10; int hSpace = (plotWidth - 2 * plotMargin) / (xTickCounter - 1); stringHeight = offGraphics.getFontMetrics().getAscent(); for (int i = 0; i < xTickCounter; i++) { offGraphics.setColor(new Color(103, 115, 133)); offGraphics.drawLine(plotMargin + i * hSpace, 0, plotMargin + i * hSpace, plotHeight); str += i + 1; stringLength = offGraphics.getFontMetrics().stringWidth(str); offGraphics.setColor(Color.YELLOW); if (i % 2 == 0) { offGraphics.drawString(str, plotMargin + i * hSpace - stringLength / 2, plotHeight + stringHeight + 5); } str = ""; } str = ""; int vTickCounter = 10; int vSpace = plotHeight / vTickCounter; for (int i = 0; i < vTickCounter; i++) { offGraphics.setColor(new Color(103, 115, 133)); offGraphics.drawLine(0, i * vSpace, plotWidth, i * vSpace); str += (plotHeight - i * vSpace) / 10; stringLength = offGraphics.getFontMetrics().stringWidth(str); offGraphics.setColor(Color.YELLOW); offGraphics.drawString(str, - 5 - stringLength, i * vSpace + stringHeight / 2); str = ""; } String[] flowTitle = {"输入流量", "输出流量"}; Color[] flowColor = {Color.RED, Color.ORANGE}; int[] flows = new int[xTickCounter]; int[] time = new int[xTickCounter]; offGraphics.setFont(new Font("Courier New", Font.PLAIN, 12)); int i = 0; while (i < 2) { int j = 0; if (i == 0) { for (j = 0; j < flows.length; j++) { flows[j] = indt.getvalue(j); time[j] = plotMargin + j * hSpace; } } else { for (j = 0; j < flows.length; j++) { flows[j] = outdt.getvalue(j); time[j] = plotMargin + j * hSpace; } } offGraphics.setStroke(new BasicStroke(5.0f)); offGraphics.setColor(flowColor[i]); offGraphics.drawPolyline(time, flows, flows.length); i++; } offGraphics.setTransform(old); drawLegend(flowTitle, flowColor); } public void drawLegend(String[] legendItems, Color[] color) { offGraphics.setStroke(new BasicStroke()); offGraphics.setFont(new Font("宋体", Font.PLAIN, 12)); offGraphics.setPaint(Color.WHITE); int itemHeight = offGraphics.getFontMetrics().getAscent(); int itemWidth[] = new int[legendItems.length]; int legendWidth = 20; for (int i = 0; i < legendItems.length; i++) { itemWidth[i] = offGraphics.getFontMetrics().stringWidth(legendItems[i]); if (i != legendItems.length - 1) { legendWidth += itemWidth[i] + 20; } else { legendWidth += itemWidth[i] + 15; } } offGraphics.fillRect((width - legendWidth) / 2, height - 25, legendWidth, itemHeight + 5); offGraphics.setColor(Color.BLACK); offGraphics.drawRect((width - legendWidth) / 2, height - 25, legendWidth, itemHeight + 5); int itemAnchor = (width - legendWidth) / 2 + 10; offGraphics.setColor(Color.BLACK); for (int i = 0; i < legendItems.length; i++) { offGraphics.setColor(color[i]); offGraphics.fillRect(itemAnchor, height - 20, 10, 10); offGraphics.setColor(Color.BLACK); offGraphics.drawString(legendItems[i], itemAnchor + 15, height - 12); itemAnchor += itemWidth[i] + 20; } } public void drawAxisLabel() { offGraphics.setFont(new Font("楷体_GB2312", Font.BOLD, 18)); offGraphics.setPaint(new Color(255, 224, 193)); int labelHeight = offGraphics.getFontMetrics().getAscent(); int labelLength = offGraphics.getFontMetrics().stringWidth(indt.getytitle()); AffineTransform old = offGraphics.getTransform(); offGraphics.translate(40, (height + labelLength) / 2); offGraphics.rotate(-Math.PI / 2); offGraphics.drawString(indt.getytitle(), 0, 0); offGraphics.setTransform(old); labelLength = offGraphics.getFontMetrics().stringWidth(indt.getxtitle()); offGraphics.drawString(indt.getxtitle(), (width - labelLength) / 2, 365); } public void paint(Graphics g) { int i = 0;/* boolean flag = true; while (flag) { try { java.lang.Thread.sleep(1000); } catch (Exception e) { } i++; update(g); indt.changevalue(); outdt.changevalue2(); }*/ this.drawBackground(); this.drawTitle(); this.drawChart(); this.drawAxisLabel(); g.drawImage(offImage, 0, 0, null); } public void update(Graphics g) { paint(g); } } //LineChart end class fileDialog extends Frame { fileDialog(String title) { super(title); MyWindowAdapter adapter = new MyWindowAdapter(this); addWindowListener(adapter); } } class MyWindowAdapter extends WindowAdapter { fileDialog sf; public MyWindowAdapter(fileDialog sfr) { this.sf=sfr; } public void windowClosing(WindowEvent we) { sf.setVisible(false); } }

 ChartData.java(流量数据存储类)

public class ChartData { String title; String xtitle; String ytitle; int value[]; public ChartData() {} public ChartData(String title, String xtitle, String ytitle, int n) { this.title = title; this.xtitle = xtitle; this.ytitle = ytitle; this.value = new int[n]; } public void addvalue(int data, int index) { value[index] = data; } public String gettitle() { return this.title; } public String getxtitle() { return this.xtitle; } public String getytitle() { return this.ytitle; } public int getvalue(int index) { return value[index]; } public void changevalue() { if(value.length>0) { for(int i=0;i<value.length-1;i++) { value[i]=value[i+1]; } value[value.length-1]=300-(32 + (int)(Math.random() * 220)); } } public void changevalue2() { if(value.length>0) { for(int i=0;i<value.length-1;i++) { value[i]=value[i+1]; } value[value.length-1]=300-(32 + (int)(Math.random() * 210)); } } public void addIndata(int point) { if(value.length>0) { for(int i=0;i<value.length-1;i++) { value[i]=value[i+1]; } value[value.length-1]=point; } } }

GetChartDataServerlet.java(serverlet)

public class GetChartDataServerlet extends HttpServlet { /** * Constructor of the object. */ public GetChartDataServerlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String data="&";int inpoint,outpoint; for(int i=0;i<12;i++) { inpoint=65 + (int) (Math.random() * 220); outpoint=32 + (int) (Math.random() * 210); if(i==0) { data+=outpoint; data=inpoint+data; } else{ data+=","+outpoint; data=inpoint+","+data; } } out.println(data); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out .println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }

页面:

<% String serverPath = request.getContextPath(); String serverletPath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + serverPath; %> <APPLET CODE="com.mobile.service.LineChart.class" name="chart" WIDTH="500" HEIGHT="450" ARCHIVE ="chart.jar"> <param name="serverUrl" value="<%=serverletPath%>"> </APPLET>  

 注意:流量图保存功能需要applet访问本地文件,这里要用到java的数字签名。具体操作见http://blog.csdn.net/hanzengyi/archive/2009/08/31/4505031.aspx

 

 

你可能感兴趣的:(thread,exception,String,服务器,applet,initialization)