被破坏的数据值 确认在非同步化的方法中是否使用实例变量。 记住:除非显示地实现SingleThreadMode,否则 servlet的单个实例就可以被 多个线程运行,如果是这样的话, 任何实例变量都是由所有线程共享的。 给出一个将数据转换成图表的小程序(由两个类文件组成,也可以当小应用程序) import java.awt.*; import java.awt.event.*; import java.applet.*; public class MainApp extends Applet { ControlsApp controls; CanvasApp canvas; public void init() { setLayout(new BorderLayout()); canvas = new CanvasApp(); add("Center", canvas); add("South", controls = new ControlsApp(canvas)); } public void destroy() { remove(controls); remove(canvas); } public void start() { controls.setEnabled(true); } public void stop() { controls.setEnabled(false); } public void processEvent(AWTEvent e) { if (e.getID() == Event.WINDOW_DESTROY) { System.exit(0); } } public static void main(String args[]) { Frame f = new Frame("TestApp"); MainApp mainApp = new MainApp(); mainApp.init(); mainApp.start(); f.add("Center", mainApp); f.setSize(600, 600); f.show(); } public String getAppletInfo() { return "An interactive test of the Graphics routines."; } } *************第二个文件******* import java.awt.*; import java.awt.event.*; import java.applet.*; import java.io.*; class CanvasApp extends Canvas { String[][] datastr1; int[][] data1; int gti1; String filestr="datenum.txt"; Font font = new java.awt.Font("Courier", Font.BOLD, 12); public void paint(Graphics g) { try{ File file=new File(filestr); FileReader in=new FileReader(file); int filesize=(int)file.length(); char[] data=new char[filesize]; int charsreaded=in.read(data); String datastr=new String(data); String[] dataall=datastr.split("\n"); datastr1=new String[dataall.length][2]; data1=new int[dataall.length][2]; gti1=dataall.length; for(int i=0;i<dataall.length;i++){ datastr1[i]=dataall[i].split(" "); int adate=Integer.valueOf(datastr1[i][0].trim()).intValue(); data1[i][0]=adate; int anum=Integer.valueOf(datastr1[i][1].trim()).intValue(); data1[i][1]=anum; } } catch (IOException e){ System.out.print("can't read : "+e); } Rectangle r = getBounds(); int h = r.height / 20; int v = r.width / 20; g.setColor(Color.pink); for (int i = 1; i <= 20; i++) { g.drawLine(0, i * h, r.width, i * h); } for (int i = 1; i <= 20; i++) { g.drawLine(i * v, 0, i * v, r.height); } g.setColor(Color.blue); for(int i=1;i<20;i++){ g.drawString(Integer.toString(i),v*i,19*h+h*2/3); g.drawString(Integer.toString(i*10-10),v/4,20*h-i*h); } g.drawString("Num",v,h); g.drawString("Date",19*v,19*h); g.setColor(Color.black); g.setFont(font); g.drawLine(v, h, v, 19* h); g.drawLine(v, 19*h, r.width - v, 19* h); for(int i=0;i<gti1-1;i++){ g.drawLine((data1[i][0])*v,19*h-(data1[i][1])*h/10,(data1[i+1][0])*v,19*h-(data1[i+1][1])*h/10); } } public void redraw(boolean cgo,String cfile) { this.filestr=cfile; repaint(); } } class ControlsApp extends Panel implements ActionListener { TextField tfile; CanvasApp canvas; Button b = null; Frame myFrame=new Frame(); FileDialog myDialog=new FileDialog(myFrame,"gogogo"); public ControlsApp(CanvasApp acanvas) { this.canvas = acanvas; add(tfile = new TextField("Clink the Go button to input the data files")); b = new Button("Go"); b.addActionListener(this); add(b); } public void actionPerformed(ActionEvent ev) { myDialog.setMode(myDialog.LOAD); myDialog.setVisible(true); String label = ev.getActionCommand(); String fstr=myDialog.getFile(); if(fstr==null) fstr="datenum.txt"; canvas.redraw(label.equals("Go"),fstr); tfile.setText(fstr); } } datanum.txt等文件为默认数据文件,所以必须,运行时请自行创建,如 1 123 2 94 3 108 4 96 5 115 6 105 7 68 8 70 9 102 10 77 在主窗口中点击 go选择数据文件,数据文件格式为 x y x1 y1 x2 y2 .... .... 其中日期为1-20,人数为1-200; (责任编辑:laiquliu) |