head 1.1; access; symbols; locks; strict; comment @# @; 1.1 date 97.11.09.01.23.12; author kowallik; state Exp; branches; next ; desc @@ 1.1 log @Initial revision @ text @// phase space plot // for the pendulum project import java.awt.*; import java.util.*; import java.lang.*; class P_Graph extends Panel { Image offscreen; // image for plot Image down_off; // image for x-ticklabels Image labelx_off; Image labely_off; Image left_off; boolean reshaped = false; Vector p=new Vector(); // phase space data double minxp,minyp,maxxp,maxyp; // min-max for plots Panel down, small, tiny, left, labelx, labely; P_Graph() { setBackground(Color.lightGray); setLayout(new BorderLayout()); small=new Panel(); tiny=new Panel(); down = new Panel(); left=new Panel(); labelx=new Panel(); labely=new Panel(); add("West", labely); add("South", labelx); add("Center", small); small.setLayout(new BorderLayout(0,5)); small.add("South", down); small.add("West", left); small.add("Center", tiny); this.show(); } public void exit() { p.removeAllElements(); try { offscreen.flush(); down_off.flush(); left_off.flush(); labelx_off.flush(); labely_off.flush(); }catch(NullPointerException npe) {} } // as soon as we are done void cleanup(boolean rerun) { Graphics g; Dimension d; p.removeAllElements(); if(rerun==false) { minxp=0; minyp=0; maxxp=0; maxyp=0; try { d = left.size(); g = left_off.getGraphics(); g.setColor(Color.lightGray); g.fillRect(0, 0, d.width, d.height); d = down.size(); g = down_off.getGraphics(); g.setColor(Color.lightGray); g.fillRect(0, 0, d.width, d.height); }catch (NullPointerException npe) {} } try { d = tiny.size(); g = offscreen.getGraphics(); g.setColor(Color.white); g.fillRect(0, 0, d.width, d.height); g.setColor(Color.black); paint(g); }catch (NullPointerException npe) {} } void draw(double x, double y) { Graphics g; Dimension d; double off,scx,scy; p.addElement(new Double(x)); p.addElement(new Double(y)); if (offscreen==null) { // make sure there is enough space to label x-axis if(!reshaped) { d = this.size(); labely.reshape( 0, 0, 15, d.height-15); labelx.reshape(55, d.height-15, d.width-50, 15); small.reshape( 15, 0, d.width-15, d.height-15); left.reshape( 0, 0, 40, d.height-30); down.reshape( 40, d.height-30, d.width-55, 15); tiny.reshape( 40, 0, d.width-55, d.height-30); d = down.size(); down_off = this.createImage(d.width, d.height); d = left.size(); left_off = this.createImage(d.width, d.height); d = labelx.size(); labelx_off = this.createImage(d.width, d.height); d = labely.size(); labely_off = this.createImage(d.width, d.height); reshaped=true; } d=tiny.size(); offscreen=this.createImage(d.width, d.height); g = offscreen.getGraphics(); g.setColor(Color.white); g.fillRect(0, 0, d.width, d.height); g.setColor(Color.black); } g = offscreen.getGraphics(); d = tiny.size(); // x or y-range has changed // notice that the y-range is always symmetric around zero if ((x>maxxp) || (xmaxyp)) { if (Math.abs(y)>maxyp) { maxyp=Math.abs(y)*1.1; minyp=-maxyp; } while(x>maxxp) maxxp+=Math.PI; while(x5) { off= (maxxp+minxp)/2; scx=(int) d.width/(maxxp-minxp); scy=(int) d.height/(2*maxyp); Double tmp=(Double) p.elementAt(p.size()-6); double xx=tmp.doubleValue(); tmp=(Double) p.elementAt(p.size()-5); double yy=tmp.doubleValue(); tmp=(Double) p.elementAt(p.size()-4); double xo=tmp.doubleValue(); tmp=(Double) p.elementAt(p.size()-3); double yo=tmp.doubleValue(); // change last element to black g.setColor(Color.black); g.drawLine((int)((xx-off)*scx+d.width/2), (int)(-yy*scy+d.height/2), (int)((xo-off)*scx+d.width/2), (int)(-yo*scy+d.height/2)); // draw latest element in red g.setColor(Color.red); g.drawLine((int)((xo-off)*scx+d.width/2), (int)(-yo*scy+d.height/2), (int)((x-off)*scx+d.width/2), (int)(-y*scy+d.height/2)); } } paint(g); } // redraw the whole phase space information public void drawall() { int i; double x0, y0, xx, yy; Dimension s = tiny.size(); Graphics g = offscreen.getGraphics(); g.setColor(Color.white); g.fillRect(0,0,s.width, s.height); double off= (maxxp+minxp)/2; double scx=(int) s.width/(maxxp-minxp); double scy=(int) s.height/(2*maxyp); g.setColor(Color.black); y0=((Double) p.elementAt(1)).doubleValue(); x0=((Double) p.elementAt(0)).doubleValue(); for (i=3; i