//XYplot.java // // a scribbable plotting java applet // // by SHC HSTX 11/12/97 // import java.applet.Applet; import java.awt.*; //import gjt.*; public class XYplot extends java.applet.Applet { int x, y; // bufferring for paint() int xp,yp; // bufferring for mouseDown and mouseDrag int height,width; Plot p,p2; FunctionPad fb; protected PlotCanvas plotCanvas,plotCanvas2; double xarr[][],yarr[][]; public void init() { x=50; y=50; String att=getParameter("width"); width = (att == null) ? 600 : Integer.valueOf(att).intValue(); att=getParameter("height"); height = (att == null) ? 600 : Integer.valueOf(att).intValue(); xarr=new double[2][12]; yarr=new double[2][12]; for(int i=0;i<12;i++){ xarr[0][i]=(double)i; yarr[0][i]=i*i*0.1; xarr[1][i]= xarr[0][i]; yarr[1][i]=-yarr[0][i]; } p=new Plot(width,height,"X","Y",0.,20.,-10.,10.,10,2,10,2); p.setTitle("This is a test plot"); //plotCanvas=new PlotCanvas(this,p,width,height,xarr,yarr,2,12); plotCanvas=new PlotCanvas(this,p,width,height,xarr,yarr); fb=new FunctionPad(plotCanvas); setLayout(new BorderLayout()); add("Center",plotCanvas); add("South",fb); } public void circle (Graphics c, int x0,int y0,int radius) { int x1,y1,x2,y2; double theta,dtheta; theta=0.0; dtheta=2.0*Math.PI/36.; x1=radius+x0; y1=y0; for(int i=1;i<=36;i++){ theta+=dtheta; x2=(int) (radius*Math.cos(theta)+x0); y2=(int) (radius*Math.sin(theta)+y0); c.drawLine(x1,y1,x2,y2); x1=x2; y1=y2; } } public void box (Graphics c, int x0,int y0,int height,int width) { int x2,y2; x2=x0+width; y2=y0+height; c.drawLine(x0,y0,x2,y0); c.drawLine(x2,y0,x2,y2); c.drawLine(x2,y2,x0,y2); c.drawLine(x0,y2,x0,y0); } } //----------------------------------------------------------------- // // class PlotCanvas() // //----------------------------------------------------------------- class PlotCanvas extends Canvas { XYplot xyplot; Plot p; double xarr[],yarr[]; double xarr2[][],yarr2[][]; int height,width; int x,y; int xp,yp; int sets=1,npts; public PlotCanvas(XYplot xyplot,Plot p,int width,int height, double xarr[],double yarr[],int sets,int npts) { this.xyplot=xyplot; this.p=p; this.width=width; this.height=height; this.xarr=xarr; this.yarr=yarr; this.sets=sets; this.npts=npts; this.x=50; this.y=50; } public PlotCanvas(XYplot xyplot,Plot p,int width,int height, double xarr[],double yarr[],int npts) { this.xyplot=xyplot; this.p=p; this.width=width; this.height=height; this.xarr=xarr; this.yarr=yarr; this.npts=npts; this.x=50; this.y=50; } public PlotCanvas(XYplot xyplot,Plot p,int width,int height, double xarr2[][],double yarr2[][]) { this.xyplot=xyplot; this.p=p; this.width=width; this.height=height; this.xarr2=xarr2; this.yarr2=yarr2; this.sets=0; this.npts=0; this.x=50; this.y=50; } public void paint(Graphics g) { int start=0; int stop=npts-1; p.paint(g); if(npts != 0 && sets != 0){ for(int i=0;ixmax) xmax=x[i]; else if(x[i]ymax) ymax=y[i]; else if(y[i] 0) x=(length+x0-title.length())/2; else x=(length)/2+x0-title.length(); g.drawString(title,x,y-2*gap); } else if(Math.abs(type) == 2){ // draw a vertical axis y1=y0+length; g.drawLine(x0,y0,x0,y1); x=x0; x1=x0+major_tick_length; y=y0; dy=length/major_ticks; x_minor=x0; x1_minor=x0+minor_tick_length; dy_minor=dy/minor_ticks; // // draw major ticks // for(int i=0;i 0) y=(y0+length)/2-title.length(); else y=(length)/2+y0-title.length(); g.drawString(title,x-3*gap,y); } } // // Constructors // public Axis() { min=0.0; max=1.0; major_ticks=1; minor_ticks=1; title=" "; labels=new String[major_ticks+1]; for(int i=0;i<=major_ticks;i++) labels[i]=" "; } public Axis(double ex_min,double ex_max) { min=ex_min; max=ex_max; major_ticks=1; minor_ticks=1; title=" "; labels=new String[major_ticks+1]; for(int i=0;i<=major_ticks;i++) labels[i]=" "; } public Axis(double ex_min,double ex_max, int ex_major_ticks,int ex_minor_ticks) { min=ex_min; max=ex_max; major_ticks=ex_major_ticks; if(major_ticks <= 0) major_ticks=1; minor_ticks=ex_minor_ticks; if(minor_ticks <= 0) minor_ticks=1; title=" "; labels=new String[major_ticks+1]; for(int i=0;i<=major_ticks;i++) labels[i]=" "; } public Axis(Axis ex_axis) { int ex_major_ticks=0,ex_minor_ticks=0; double ex_min=0.0,ex_max=0.0; ex_axis.getParameters(ex_min,ex_max,ex_major_ticks,ex_minor_ticks); min=ex_min; max=ex_max; major_ticks=ex_major_ticks; minor_ticks=ex_minor_ticks; title=" "; labels=new String[major_ticks+1]; for(int i=0;i<=major_ticks;i++) labels[i]=" "; } // // public methods // public void draw(Graphics g,int type,int x0,int y0,int length) { drawTicks(g,type,x0,y0,length); } public void setLabels() { double buf; labels[0]=String.valueOf(min); buf=(max-min)/major_ticks; for(int i=1;i<=major_ticks;i++){ labels[i]=String.valueOf(buf*i+min); } } public void setTitle(String title) { this.title=title; } public void getParameters(double ex_min,double ex_max, int ex_major_ticks,int ex_minor_ticks) { ex_min=min; ex_max=max; ex_major_ticks=major_ticks; ex_minor_ticks=minor_ticks; } }