import java.awt.*; import java.lang.*; import java.util.*; import java.io.*; import corejava.*; import java.net.*; import java.applet.*; import java.awt.image.*; // ScreenCanvas used for graphical display // public class ScreenCanvas extends Canvas { static final int OFFSET = 40; Graph graph = null; Insets edge = new Insets(10,10,10,10); Dimension area=this.size(); // current size of the object on screen // border box = new border(); Scale scales = new Scale(); header head = new header(); footer foot = new footer(); Image image = null; // background image // Graphics imageGraphics = null; // resource to draw on the background image // Thread engine =null; // thread // int waveHeight; // height of this object on screen // int waveWidth; // width of this object on screen // boolean SizeChanged = false; boolean soundon=false; double default_height = 20.0; // default height = 20. Assume the max. value of the data is smaller // than 20// double NOTDEFINED = 0.0/0.0; public ScreenCanvas() // constructor // { this.resize(450,130); this.setBackground(Color.black); start(); } // initialize the object // public void init() { // System.out.println("Init is Called"); } // Called when the screen start to run. It creates the background image object. // public void start() { //System.out.println("Start is Called"); //System.out.println("waveHeight="+this.size().height); //System.out.println("waveWidth="+this.size().width); if (size().height == 0) { waveHeight = 250; waveWidth =400; } waveHeight = this.size().height; waveWidth = this.size().width; // System.out.println("waveHeight="+waveHeight); // System.out.println("waveWidth="+waveWidth); image = createImage(waveWidth, waveHeight); if (image!=null) imageGraphics = image.getGraphics(); if (imageGraphics == null) System.out.println("ERR: Can't create graphics"); /* if (engine==null) { engine = new Thread(this); engine.start(); } */ } /* public void stop() { if (engine!=null) { engine.stop(); engine=null; } } */ /* public void run() { while(true) { // System.out.println("Run is Called"); if ( graph != null) { // System.out.println("Graph is Ready Now"); repaint(); if (false){ //if (soundon){ int ox=0; double oy=0.0; //System.out.println("Sound="+soundon); for (int i = 0; i < 2; i++) { oy = graph.ypoints[0]; for (int x=0; x<400; x++) { if (x==400-1) graph.ypoints[x]=oy; else { graph.ypoints[x] = graph.ypoints[x+1]; } } } } } //else System.out.println("Graph is Not Ready"); try { engine.wait(); //System.out.println("sleep is called"); } catch(Exception e) { System.out.println("Exception: "+e); } } } */ // redraw the screen when the user asserts new command // // new_g = current graphics resource, on = determine if sound command is enable// public void redraw(Graph new_g, boolean on) { soundon = on; graph = new_g; repaint(); // if (soundon) System.out.println("Sound is +++++++"); // else System.out.println("Sound is ooooooooo"); // System.out.println("Redraw is called"); } // update screen. Redraw the screen for any new user command // public void update(Graphics g) { if (waveHeight != size().height || waveWidth!=size().width) SizeChanged = true; System.out.println("update is called"); paint(g); // specifies what to redraw // } // Transform a datum to coordinates of the screen // // x = value of x , y = value of y , ymax = default max of y, off = avg or the offset// Point plotPoint( double x, double y, double ymax, double off) { double offset = OFFSET; double height = waveHeight - 2*offset; // original area.height -... double width = waveWidth - 2*offset; double xpos = offset; double ypos = height; double xscale = width/(graph.xmax()-graph.xmin()); double yscale = height/default_height; // System.out.println("Plot Point is called"); off = (graph.ymax()+graph.ymin())/2; int xd = (int) Math.round((x-graph.xmin())*xscale+xpos); int yd = (int) Math.round((waveHeight/2)-yscale*(y - off)); // center the plot // return new Point(xd, yd); } // what to be redraw when function redraw is called. The function, the border, the header, and the footer //will redrawn. // // g = current graphic resource // public void paint(Graphics g) { if (graph!=null){ // System.out.println("**waveHeight="+this.size().height); // System.out.println("**waveWidth="+this.size().width); // System.out.println("**Graph is Ready HI"); if (waveHeight == 0) { waveHeight = 250; waveWidth =400; } } else return; //System.out.println("Paint is called"); if (SizeChanged) { //System.out.println("Size is changed"); waveHeight = size().height; waveWidth = size().width; image = createImage(waveWidth, waveHeight); if ( image != null) imageGraphics = image.getGraphics(); SizeChanged = false; } imageGraphics.setColor(Color.black); // set background to black // imageGraphics.fillRect(0,0,waveWidth, waveHeight); // fill up the image with black color // imageGraphics.setColor(Color.yellow); // set yellow as the color of the function // //System.out.println("Set Color to Yellow"); Polygon poly = new Polygon(); default_height =20.0; double h = graph.ymax()-graph.ymin(); System.out.println("ymax="+graph.ymax()+"ymin="+graph.ymin()); // Default maximum amplitude is 20. When the maximum amplitude for a function is greater the //default height. Default height = new max. height + 4 // if ( h > 20.0 ) default_height= graph.ymax() - graph.ymin() + 4.0; //System.out.println("default_height="+default_height); //System.out.println("ymax="+graph.ymax()); // plot the function on the background buffer. Any undefined value, for example a positive Infinity, will //be ignored; it will not show on the plot. // for (int i=0; i < graph.numPoints(); ++i) { Point p = plotPoint(graph.X(i), graph.Y(i), default_height, graph.avg() ); if ( Math.abs(graph.Y(i)) >= Double.POSITIVE_INFINITY ) { //System.out.println("STEP1"); if (poly!=null) imageGraphics.drawPolygon(poly); poly=null; // System.out.println("STEP3"); } else if (Math.abs(graph.Y(i)) < Double.POSITIVE_INFINITY ) { if (poly==null) poly = new Polygon(); poly.addPoint(p.x, p.y); //System.out.println("Y = "+p.y); } } if (poly!=null) imageGraphics.drawPolygon(poly); //System.out.println("paint is called"); box.paintborder(imageGraphics,this,OFFSET); head.drawheader(imageGraphics,this,OFFSET, graph.ymax(), graph.ymin()); foot.drawfooter(imageGraphics,this,OFFSET); //scales.drawscale(imageGraphics,this,OFFSET); g.drawImage(image,0,0,null); //System.out.println("Paint is done"); } } // This class responsible for drawing the value of ymax and value of ymin on screen // class header { FontMetrics fm; Font f; int fontsize; int x1, x2, y1, y2; int height; int width; // function that draws the header // //function, ymax = max. value of y, ymin= min. value of y // public void drawheader(Graphics g, ScreenCanvas screen, int OFFSET, double ymax, double ymin) { if (screen.size().height >100 && screen.size().width > 100) fontsize = 14; else fontsize = 10; width = screen.size().width; f = new Font("Helvetica", Font.BOLD,12); fm = g.getFontMetrics(f); height = OFFSET; String s1 = new String("ymax = "+ ymax+" ymin = "+ymin); int w1 = fm.stringWidth(s1); int cx = (width - w1)/2; int cy = (height)/2; g.setColor(Color.green); g.setFont(f); g.drawString(s1,cx,cy); } } // class responsible for the x labels // class footer { FontMetrics fm; Font f; int fontsize; int x1, x2, y1, y2; int height; int width; // function that draws the labels for the x axis // public void drawfooter(Graphics g, ScreenCanvas screen, int OFFSET) { if (screen.size().height >100 && screen.size().width > 100) fontsize = 12; else fontsize = 7; width = screen.size().width; height = screen.size().height; f = new Font("Helvetica", Font.BOLD,11); fm = g.getFontMetrics(f); g.setColor(Color.orange); g.setFont(f); // create the letters // String s1 = new String("0"); String s2 = new String("1/2pi"); String s3 = new String("pi"); String s4 = new String("3/2pi"); String s5 = new String("2pi"); int w1 = fm.stringWidth(s1); int w2 = fm.stringWidth(s2); int w3 = fm.stringWidth(s3); int w4 = fm.stringWidth(s4); int w5 = fm.stringWidth(s5); //locate the characters on the screen // int cy = (height-OFFSET+height)/2; int cx = OFFSET; g.drawString(s1,cx,cy); cx = OFFSET+((width-2*OFFSET)/4 - w2/2); g.drawString(s2,cx,cy); cx = (width - w3)/2; g.drawString(s3,cx,cy); cx = OFFSET+((width-2*OFFSET)*3/4 - w4/2); g.drawString(s4,cx,cy); cx = width-OFFSET - w5/2; g.drawString(s5,cx,cy); } } // this class responsible for drawing the border // class border { private int x1,x2,y1,y2; private String title, xlabel, ylabel; private int offset; private int ypos, xpos; private int xscale,yscale; // scales // private FontMetrics fm; ScreenCanvas scr; // function that draw the border // void paintborder(Graphics g, ScreenCanvas screen, int OFFSET) { scr = screen; x1 = y1= offset = OFFSET; x2= screen.size().width - offset; y2= screen.size().height - offset; ypos = y2; xpos=x2; g.setColor(Color.white); DrawRect(g); } void DrawRect(Graphics g) { g.setColor(Color.white); g.drawRect(x1,y1,x2-x1,y2-y1); } } // something need to fix // // draws the scale. // class Scale { int x1,x2,y1,y2,slen; // Canvas scr; void drawscale(Graphics g, ScreenCanvas screen, int OFFSET) { x1=x2=y1=y2=slen=0; g.setColor(Color.white); //System.out.println("Painting Border"); int width = screen.size().width - OFFSET; int height = screen.size().height - OFFSET; int xscale = (int)(width/10); int yscale = (int)(height/10); //draw xscale // slen = (int)(xscale / 20 ); x1=x2=OFFSET; y1=OFFSET; y2 = screen.size().height - OFFSET; for(int i=0;i<10;i++) { g.drawLine(x1,y1,x1,y1+slen); g.drawLine(x2,y2,x2,y2-slen); x1=x1+xscale; x2=x2+xscale; } //draw yscale// y1 = y2=OFFSET; x1 = OFFSET; x2 = screen.size().width - OFFSET; for(int i=0;i<10;i++) { g.drawLine(x1,y1,x1+slen,y1); g.drawLine(x2,y2,x2-slen,y2); y1=y1+yscale; y2=y2+yscale; } } }