import java.awt.*; import java.awt.event.*; /**============================================================== * Creates a Display: Ploting area + 4 coordinates inputs *==============================================================*/ public class Display extends Panel implements NotifyParent { private AxisValue Xmin, Xmax, Ymin, Ymax; private Plot2D plot; private NotifyParent parent; PlotList data; private Label Xname, Yname; private Panel legends; public static final int WHITE = 0; public static final int RED = 1; public static final int BLUE = 2; public static final int GREEN = 3; public double getXmin() {return Xmin.getValue();} public double getXmax() {return Xmax.getValue();} public double getYmin() {return Ymin.getValue();} public double getYmax() {return Ymax.getValue();} public void setXmin(double v) {Xmin.setManual(v);} public void setXmax(double v) {Xmax.setManual(v);} public void setYmin(double v) {Ymin.setManual(v);} public void setYmax(double v) {Ymax.setManual(v);} public void setXname(String name) {Xname.setText(name);} public void setYname(String name) {Yname.setText(name);} private PlotList FindData(int ID) {if(data == null) { data = new PlotList(); data.b = new Button("ROOT"); legends.add(data.b); data.next = null; data.ID = ID; return data; } PlotList tmp = data; while(tmp != null && tmp.ID != ID) tmp = tmp.next; if(tmp != null) return tmp; tmp = new PlotList(); tmp.b = new Button("NEXT"); legends.add(tmp.b); tmp.ID = ID; tmp.next = data; data = tmp; return data; } public void AddData(String name, int ID, int col, int length, double[] X, double [] Y) { double lxmin, lxmax, lymin, lymax; lxmin = lxmax = X[0]; lymin = lymax = Y[0]; for(int i=1;i lxmax) lxmax = X[i]; if(Y[i] < lymin) lymin = Y[i]; if(Y[i] > lymax) lymax = Y[i]; } PlotList curData = FindData(ID); curData.color = col; curData.size = length; curData.X = new double[length]; for(int i=0;i lxmax) lxmax = tmp.Xmax; if(tmp.Ymin < lymin) lymin = tmp.Ymin; if(tmp.Ymax > lymax) lymax = tmp.Ymax; tmp = tmp.next; } Xmin.setAuto(lxmin); Xmax.setAuto(lxmax); Ymin.setAuto(lymin); Ymax.setAuto(lymax); } Display(NotifyParent parent, String xname, String yname) { this.parent = parent; setBackground(new Color(0.7F,0.7F,0.8F)); // Creates Default Layout GridBagLayout ly = new GridBagLayout(); setLayout(ly); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.BOTH; c.gridx = 1; c.gridy = 0; c.gridwidth = 5; c.gridheight=5; c.weightx = c.weighty = 1.0; plot = new Plot2D(this); ly.setConstraints(plot,c); add(plot); c.fill = GridBagConstraints.NONE; c.gridx = 0; c.gridy = 0; c.gridwidth = 1; c.gridheight=1; c.weightx = c.weighty = 0.0; Ymax = new AxisValue(this, 1.0); ly.setConstraints(Ymax,c); add(Ymax); c.gridx = 0; c.gridy = 1; c.gridwidth = 1; c.gridheight=3; c.weightx = c.weighty = 0.0; Panel dm = new Panel(); Label Yname = new Label(yname, Label.CENTER); dm.add(Yname); ly.setConstraints(dm,c); add(dm); c.gridx = 0; c.gridy = 4; c.gridwidth = 1; c.gridheight=1; c.weightx = c.weighty = 0.0; c.anchor = GridBagConstraints.SOUTH; Ymin = new AxisValue(this, 0.0); ly.setConstraints(Ymin,c); add(Ymin); c.gridx = 0; c.gridy = 5; c.gridwidth = 1; c.gridheight=1; c.weightx = c.weighty = 0.0; Panel dm1 = new Panel(); ly.setConstraints(dm1,c); add(dm1); c.gridx = 1; c.gridy = 5; c.gridwidth = 1; c.gridheight=1; Xmin = new AxisValue(this, 0.0); ly.setConstraints(Xmin,c); add(Xmin); c.gridx = 2; c.gridy = 5; c.gridwidth = 3; c.gridheight=1; c.weightx = c.weighty = 0.0; Panel dm2 = new Panel(); Label Xname = new Label(xname, Label.CENTER); dm2.add(Xname); ly.setConstraints(dm2,c); add(dm2); c.gridx = 5; c.gridy = 5; c.gridwidth = 1; c.gridheight=1; c.weightx = c.weighty = 0.0; c.anchor = GridBagConstraints.EAST; Xmax = new AxisValue(this, 1.0); ly.setConstraints(Xmax,c); add(Xmax); c.anchor = GridBagConstraints.NORTH; c.gridx = 6; c.gridy = 0; c.gridwidth = 1; c.gridheight=6; legends = new Panel(); ly.setConstraints(legends,c); legends.setLayout(new GridLayout(0,1)); add(legends); data = null; } public Insets getInsets() {return new Insets(10,10,10,10);} public void update() {parent.update();} public void redraw() { plot.repaint();} } /**============================================================= * *=============================================================*/ class Plot2D extends Canvas { Display parent; private String mouseVal; int size; double[] x; double[] y; double xmin, xmax, ymin, ymax; int width, height; int colorSize = 4; public static final Color colorList[] = {Color.white, Color.red, Color.blue, Color.green}; void Data(int lg, double[] X, double Y[]) { size = lg; x = X; y = Y; } double clean(double x) { return(Math.floor(x * 100.)/100.);} double clean(double x, int digits) { double precision = Math.pow(10.,digits); return(Math.floor(x * precision) / precision);} void eraseMouse(Graphics g) {if(mouseVal == null) return; g.setColor(Color.black); g.drawString(mouseVal,10,10); } void drawMouse(Graphics g, int xm, int ym) {double xmouse = clean(xmin + (xmax - xmin) * (double)xm/(double)width); double ymouse = clean(ymax + (ymin - ymax) * (double)ym/(double)height); g.setColor(Color.white); mouseVal = String.valueOf(xmouse) + ", " + String.valueOf(ymouse); g.drawString(mouseVal,10,10); } Plot2D(Display p) {parent = p; size = 0; setBackground(Color.black); setForeground(Color.white); width = height = 0; addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent e) {Graphics g = getGraphics(); eraseMouse(g); } public void mousePressed(MouseEvent e) {Graphics g = getGraphics(); drawMouse(g,e.getX(),e.getY()); } }); addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) {if(width == 0 || height == 0) return; Graphics g = getGraphics(); eraseMouse(g); drawMouse(g,e.getX(),e.getY()); } }); } public void paint(Graphics g) { Dimension sz = getSize(); width = sz.width; height = sz.height; xmin = parent.getXmin(); xmax = parent.getXmax(); ymin = parent.getYmin(); ymax = parent.getYmax(); int x1, y1, x2, y2; // X axis g.setColor(Color.gray); Scale s = new Scale(xmin, xmax); for(double x = s.getMin(); x <= s.getMax(); x += s.getStep()) {x1 = (int)(width * (x - xmin) / (xmax-xmin)); g.drawLine(x1,0,x1,height-10); g.drawString(String.valueOf(clean(x,4)),x1+2,height-2); } // Y axis s.setMinMax(ymin, ymax); for(double y = s.getMin(); y <= s.getMax(); y += s.getStep()) {y1 = (int)(height * (ymax - y) / (ymax-ymin)); g.drawLine(0,y1,width,y1); g.drawString(String.valueOf(clean(y,4)),5,y1-2); } PlotList tmp = parent.data; while(tmp != null) { g.setColor(colorList[tmp.color % colorSize]); //Plot Data x1 = (int)(width * (tmp.X[0] - xmin) / (xmax-xmin)); y1 = (int)(height * (ymax - tmp.Y[0]) / (ymax - ymin)); for(int i=1; i 0. && ymin < 0.) { y1 = (int)(height * ymax / (ymax - ymin)); g.drawLine(0,y1,width,y1); } tmp = tmp.next; } } } //============================================================== // //============================================================== class PlotList { int ID; int size; int color; Button b; double[] X; double[] Y; double Xmin, Xmax, Ymin, Ymax; PlotList next = null; }