<< Chapter < Page
  Digital signal processing - dsp     Page 25 / 25
Chapter >> Page >
Listing 39. Graph01.java.
/* File Graph01.java Copyright 2002, R.G.BaldwinNote: This program requires access to the interface named GraphIntfc01.This is a plotting program. It is designed to access a class file, whichimplements GraphIntfc01, and to plot up to five functions defined in that classfile. The plotting surface is divided into the required number of equallysized plotting areas, and one function is plotted on Cartesian coordinates ineach area. The methods corresponding to thefunctions are named f1, f2, f3, f4, and f5.The class containing the functions must also define a static method namedgetNmbr(), which takes no parameters and returns the number of functions tobe plotted. If this method returns a value greater than 5, aNoSuchMethodException will be thrown. Note that the constructor for the classthat implements GraphIntfc01 must not require any parameters due to theuse of the newInstance method of the Class class to instantiate an objectof that class. If the number of functions is lessthan 5, then the absent method names must begin with f5 and work down towardf1. For example, if the number of functions is 3, then the program willexpect to call methods named f1, f2, and f3. It is OK for the absentmethods to be defined in the class. They simply won't be invoked.The plotting areas have alternating white and gray backgrounds to make themeasy to separate visually. All curves are plotted in black. ACartesian coordinate system with axes, tic marks, and labels is drawn in redin each plotting area. The Cartesian coordinate system in eachplotting area has the same horizontal and vertical scale, as well as thesame tic marks and labels on the axes. The labels displayed on the axes,correspond to the values of the extreme edges of the plotting area.The program also compiles a sample class named junk, which contains fivemethods and the method named getNmbr. This makes it easy to compile and testthis program in a stand-alone mode. At runtime, the name of the class thatimplements the GraphIntfc01 interface must be provided as a command-lineparameter. If this parameter is missing, the program instantiates anobject from the internal class named junk and plots the data provided bythat class. Thus, you can test the program by running it with nocommand-line parameter. This program provides the followingtext fields for user input, along with a button labeled Graph. This allowsthe user to adjust the parameters and replot the graph as many times with asmany plotting scales as needed: xMin = minimum x-axis valuexMax = maximum x-axis value yMin = minimum y-axis valueyMax = maximum y-axis value xTicInt = tic interval on x-axisyTicInt = tic interval on y-axis xCalcInc = calculation intervalThe user can modify any of these parameters and then click the Graphbutton to cause the five functions to be re-plotted according to thenew parameters. Whenever the Graph button is clicked,the event handler instantiates a new object of the class that implementsthe GraphIntfc01 interface. Depending on the nature of that class, this maybe redundant in some cases. However, it is useful in those cases where itis necessary to refresh the values of instance variables defined in theclass (such as a counter, for example). Tested using JDK 1.8 under Win 7.This program uses constants that were first defined in the Color class ofv1.4.0. Therefore, the program requires v1.4.0 or later to compile andrun correctly. **************************************/import java.awt.*; import java.awt.event.*;import java.awt.geom.*; import javax.swing.*;import javax.swing.border.*; class Graph01{public static void main( String[]args) throws NoSuchMethodException,ClassNotFoundException, InstantiationException,IllegalAccessException{ if(args.length == 1){//pass command-line parameter new GUI(args[0]); }else{//no command-line parameter given new GUI(null);}//end else }// end main}//end class Graph01 definition //===================================//class GUI extends JFrame implements ActionListener{//Define plotting parameters and // their default values.double xMin = 0.0; double xMax = 400.0;double yMin = -100.0; double yMax = 100.0;//Tic mark intervals double xTicInt = 20.0;double yTicInt = 20.0; //Tic mark lengths. If too small// on x-axis, a default value is // used later.double xTicLen = (yMax-yMin)/50; double yTicLen = (xMax-xMin)/50;//Calculation interval along x-axis double xCalcInc = 1.0;//Text fields for plotting parameters JTextField xMinTxt =new JTextField("" + xMin); JTextField xMaxTxt =new JTextField("" + xMax); JTextField yMinTxt =new JTextField("" + yMin); JTextField yMaxTxt =new JTextField("" + yMax); JTextField xTicIntTxt =new JTextField("" + xTicInt); JTextField yTicIntTxt =new JTextField("" + yTicInt); JTextField xCalcIncTxt =new JTextField("" + xCalcInc); //Panels to contain a label and a// text field JPanel pan0 = new JPanel();JPanel pan1 = new JPanel(); JPanel pan2 = new JPanel();JPanel pan3 = new JPanel(); JPanel pan4 = new JPanel();JPanel pan5 = new JPanel(); JPanel pan6 = new JPanel();//Misc instance variables int frmWidth = 408;int frmHeight = 430; int width;int height; int number;GraphIntfc01 data; String args = null;//Plots are drawn on the canvases // in this array.Canvas[] canvases;//Constructor GUI(String args)throwsNoSuchMethodException, ClassNotFoundException,InstantiationException, IllegalAccessException{if(args != null){ //Save for use later in the// ActionEvent handler this.args = args;//Instantiate an object of the // target class using the String// name of the class. data = (GraphIntfc01)Class.forName(args). newInstance();}else{ //Instantiate an object of the// test class named junk. data = new junk();}//end else //Create array to hold correct// number of Canvas objects. canvases =new Canvas[data.getNmbr()];//Throw exception if number of // functions is greater than 5.number = data.getNmbr(); if(number>5){ throw new NoSuchMethodException("Too many functions. " + "Only 5 allowed.");}//end if //Create the control panel and// give it a border for cosmetics. JPanel ctlPnl = new JPanel(); ctlPnl.setLayout(//?rows x 4 colsnew GridLayout(0,4)); ctlPnl.setBorder(new EtchedBorder()); //Button for replotting the graphJButton graphBtn = new JButton("Graph");graphBtn.addActionListener(this); //Populate each panel with a label// and a text field. Will place // these panels in a grid on the// control panel later. pan0.add(new JLabel("xMin"));pan0.add(xMinTxt); pan1.add(new JLabel("xMax"));pan1.add(xMaxTxt); pan2.add(new JLabel("yMin"));pan2.add(yMinTxt); pan3.add(new JLabel("yMax"));pan3.add(yMaxTxt); pan4.add(new JLabel("xTicInt"));pan4.add(xTicIntTxt); pan5.add(new JLabel("yTicInt"));pan5.add(yTicIntTxt); pan6.add(new JLabel("xCalcInc"));pan6.add(xCalcIncTxt); //Add the populated panels and the// button to the control panel with // a grid layout.ctlPnl.add(pan0); ctlPnl.add(pan1);ctlPnl.add(pan2); ctlPnl.add(pan3);ctlPnl.add(pan4); ctlPnl.add(pan5);ctlPnl.add(pan6); ctlPnl.add(graphBtn);//Create a panel to contain the // Canvas objects. They will be// displayed in a one-column grid. JPanel canvasPanel = new JPanel();canvasPanel.setLayout(//?rows,1 col new GridLayout(0,1));//Create a custom Canvas object for // each function to be plotted and// add them to the one-column grid. // Make background colors alternate// between white and gray. for(int cnt = 0;cnt<number; cnt++){ switch(cnt){case 0 : canvases[cnt]= new MyCanvas(cnt);canvases[cnt].setBackground(Color.WHITE); break;case 1 : canvases[cnt]= new MyCanvas(cnt);canvases[cnt].setBackground(Color.LIGHT_GRAY); break;case 2 : canvases[cnt]= new MyCanvas(cnt);canvases[cnt].setBackground(Color.WHITE); break;case 3 : canvases[cnt]= new MyCanvas(cnt);canvases[cnt].setBackground(Color.LIGHT_GRAY); break;case 4 : canvases[cnt]= new MyCanvas(cnt);canvases[cnt].setBackground(Color.WHITE); }//end switch//Add the object to the grid. canvasPanel.add(canvases[cnt]); }//end for loop//Add the sub-assemblies to the // frame. Set its location, size,// and title, and make it visible. getContentPane().add(ctlPnl,"South"); getContentPane().add(canvasPanel,"Center"); setBounds(0,0,frmWidth,frmHeight);setTitle("Graph01, " + "Copyright 2002, " +"Richard G. Baldwin"); setVisible(true);//Set to exit on X-button click setDefaultCloseOperation(EXIT_ON_CLOSE); //Get and save the size of the// plotting surface width = canvases[0].getWidth(); height = canvases[0].getHeight(); //Guarantee a repaint on startup.for(int cnt = 0; cnt<number; cnt++){ canvases[cnt].repaint(); }//end for loop}//end constructor //---------------------------------////This event handler is registered // on the JButton to cause the// functions to be replotted. public void actionPerformed(ActionEvent evt){ //Re-instantiate the object that// provides the data try{if(args != null){ data = (GraphIntfc01)Class.forName(args).newInstance(); }else{data = new junk(); }//end else}catch(Exception e){ //Known to be safe at this point.// Otherwise would have aborted // earlier.}//end catch //Set plotting parameters using// data from the text fields. xMin = Double.parseDouble(xMinTxt.getText()); xMax = Double.parseDouble(xMaxTxt.getText()); yMin = Double.parseDouble(yMinTxt.getText()); yMax = Double.parseDouble(yMaxTxt.getText()); xTicInt = Double.parseDouble(xTicIntTxt.getText()); yTicInt = Double.parseDouble(yTicIntTxt.getText()); xCalcInc = Double.parseDouble(xCalcIncTxt.getText()); //Calculate new values for the// length of the tic marks on the // axes. If too small on x-axis,// a default value is used later. xTicLen = (yMax-yMin)/50;yTicLen = (xMax-xMin)/50; //Repaint the plotting areasfor(int cnt = 0; cnt<number; cnt++){ canvases[cnt].repaint(); }//end for loop}//end actionPerformed //---------------------------------////This is an inner class, which is used // to override the paint method on the// plotting surface. class MyCanvas extends Canvas{int cnt;//object number //Factors to convert from double// values to integer pixel locations. double xScale;double yScale; MyCanvas(int cnt){//save obj numberthis.cnt = cnt; }//end constructor//Override the paint method public void paint(Graphics g){//Calculate the scale factors xScale = width/(xMax-xMin);yScale = height/(yMax-yMin); //Set the origin based on the// minimum values in x and y g.translate((int)((0-xMin)*xScale),(int)((0-yMin)*yScale)); drawAxes(g);//Draw the axesg.setColor(Color.BLACK); //Get initial data valuesdouble xVal = xMin; int oldX = getTheX(xVal);int oldY = 0; //Use the Canvas obj number to// determine which method to // invoke to get the value for y.switch(cnt){ case 0 :oldY = getTheY(data.f1(xVal)); break;case 1 : oldY = getTheY(data.f2(xVal));break; case 2 :oldY = getTheY(data.f3(xVal)); break;case 3 : oldY = getTheY(data.f4(xVal));break; case 4 :oldY = getTheY(data.f5(xVal)); }//end switch//Now loop and plot the points while(xVal<xMax){ int yVal = 0;//Get next data value. Use the // Canvas obj number to// determine which method to // invoke to get the value for y.switch(cnt){ case 0 :yVal = getTheY(data.f1(xVal));break; case 1 :yVal = getTheY(data.f2(xVal));break; case 2 :yVal = getTheY(data.f3(xVal));break; case 3 :yVal = getTheY(data.f4(xVal));break; case 4 :yVal = getTheY(data.f5(xVal));}//end switch1 //Convert the x-value to an int// and draw the next line segment int x = getTheX(xVal);g.drawLine(oldX,oldY,x,yVal); //Increment along the x-axisxVal += xCalcInc; //Save end point to use as start// point for next line segment. oldX = x;oldY = yVal; }//end while loop}//end overridden paint method //---------------------------------////Method to draw axes with tic marks // and labels in the color REDvoid drawAxes(Graphics g){ g.setColor(Color.RED);//Label left x-axis and bottom // y-axis. These are the easy// ones. Separate the labels from // the ends of the tic marks by// two pixels. g.drawString("" + (int)xMin,getTheX(xMin), getTheY(xTicLen/2)-2);g.drawString("" + (int)yMin, getTheX(yTicLen/2)+2,getTheY(yMin)); //Label the right x-axis and the// top y-axis. These are the hard // ones because the position must// be adjusted by the font size and // the number of characters.//Get the width of the string for// right end of x-axis and the // height of the string for top of// y-axis //Create a string that is an// integer representation of the // label for the right end of the// x-axis. Then get a character // array that represents the// string. int xMaxInt = (int)xMax;String xMaxStr = "" + xMaxInt; char[]array = xMaxStr. toCharArray();//Get a FontMetrics object that can // be used to get the size of the// string in pixels. FontMetrics fontMetrics =g.getFontMetrics(); //Get a bounding rectangle for the// string Rectangle2D r2d =fontMetrics.getStringBounds( array,0,array.length,g);//Get the width and the height of // the bounding rectangle. The// width is the width of the label // at the right end of the// x-axis. The height applies to // all the labels, but is needed// specifically for the label at // the top end of the y-axis.int labWidth = (int)(r2d.getWidth());int labHeight = (int)(r2d.getHeight());//Label the positive x-axis and the // positive y-axis using the width// and height from above to // position the labels. These// labels apply to the very ends of // the axes at the edge of the// plotting surface. g.drawString("" + (int)xMax,getTheX(xMax)-labWidth, getTheY(xTicLen/2)-2);g.drawString("" + (int)yMax, getTheX(yTicLen/2)+2,getTheY(yMax)+labHeight); //Draw the axesg.drawLine(getTheX(xMin), getTheY(0.0),getTheX(xMax), getTheY(0.0));g.drawLine(getTheX(0.0), getTheY(yMin),getTheX(0.0), getTheY(yMax));//Draw the tic marks on axes xTics(g);yTics(g); }//end drawAxes//---------------------------------// //Method to draw tic marks on x-axisvoid xTics(Graphics g){ double xDoub = 0;int x = 0; //Get the ends of the tic marks.int topEnd = getTheY(xTicLen/2); int bottomEnd =getTheY(-xTicLen/2); //If the vertical size of the// plotting area is small, the // calculated tic size may be too// small. In that case, set it to // 10 pixels.if(topEnd<5){ topEnd = 5;bottomEnd = -5; }//end if//Loop and draw a series of short // lines to serve as tic marks.// Begin with the positive x-axis // moving to the right from zero.while(xDoub<xMax){ x = getTheX(xDoub);g.drawLine(x,topEnd,x,bottomEnd); xDoub += xTicInt;}//end while //Now do the negative x-axis moving// to the left from zero xDoub = 0;while(xDoub>xMin){ x = getTheX(xDoub);g.drawLine(x,topEnd,x,bottomEnd); xDoub -= xTicInt;}//end while }//end xTics//---------------------------------// //Method to draw tic marks on y-axisvoid yTics(Graphics g){ double yDoub = 0;int y = 0; int rightEnd = getTheX(yTicLen/2);int leftEnd = getTheX(-yTicLen/2); //Loop and draw a series of short// lines to serve as tic marks. // Begin with the positive y-axis// moving up from zero. while(yDoub<yMax){ y = getTheY(yDoub);g.drawLine(rightEnd,y,leftEnd,y); yDoub += yTicInt;}//end while //Now do the negative y-axis moving// down from zero. yDoub = 0;while(yDoub>yMin){ y = getTheY(yDoub);g.drawLine(rightEnd,y,leftEnd,y); yDoub -= yTicInt;}//end while }//end yTics//---------------------------------// //This method translates and scales// a double y value to plot properly // in the integer coordinate system.// In addition to scaling, it causes // the positive direction of the// y-axis to be from bottom to top. int getTheY(double y){double yDoub = (yMax+yMin)-y; int yInt = (int)(yDoub*yScale);return yInt; }//end getTheY//---------------------------------// //This method scales a double x value// to plot properly in the integer // coordinate system.int getTheX(double x){ return (int)(x*xScale);}//end getTheX //---------------------------------//}//end inner class MyCanvas //===================================//}//end class GUI //===================================////Sample test class. Required for // compilation and stand-alone// testing. class junk implements GraphIntfc01{public int getNmbr(){ //Return number of functions to// process. Must not exceed 5. return 4;}//end getNmbr public double f1(double x){return (x*x*x)/200.0; }//end f1public double f2(double x){ return -(x*x*x)/200.0;}//end f2 public double f3(double x){return (x*x)/200.0; }//end f3public double f4(double x){ return 50*Math.cos(x/10.0);}//end f4 public double f5(double x){return 100*Math.sin(x/20.0); }//end f5}//end sample class junk
Listing 40. Graph02.java.
/* File Graph02.java Copyright 2002, R.G.BaldwinNote: This program requires access to the interface named GraphIntfc01.This is a modified version of the program named Graph01. That programplots up to five separate curves in separate plotting areas. This programsuperimposes up to five separate curves in different colors in the sameplotting area. This is a plotting program. It isdesigned to access a class file, which implements GraphIntfc01, and to plot upto five functions defined in that class file.The methods corresponding to the functions are named f1, f2, f3, f4,and f5. The class containing the functions mustalso define a static method named getNmbr(), which takes no parametersand returns the number of functions to be plotted. If this method returns avalue greater than 5, a NoSuchMethodException will be thrown.Note that the constructor for the class that implements GraphIntfc01 must notrequire any parameters due to the use of the newInstance method of theClass class to instantiate an object of that class.If the number of functions is less than 5, then the absent method namesmust begin with f5 and work down toward f1. For example, if the number offunctions is 3, then the program will expect to call methods named f1, f2,and f3. It is OK for the absent methods to be defined in the class.They simply won't be invoked. Each curve is plotted in a differentcolor. The correspondence between colors and function calls is asfollows: f1: BLACKf2: BLUE f3: REDf4: MAGENTA f5: CYANA Cartesian coordinate system with axes, tic marks, and labels is drawn ingreen. The labels displayed on the axes,correspond to the values of the extreme edges of the plotting area.The program also compiles a sample class named junk, which contains fivemethods and the method named getNmbr. This makes it easy to compile and testthis program in a stand-alone mode. At runtime, the name of the class thatimplements the GraphIntfc01 interface must be provided as a command-lineparameter. If this parameter is missing, the program instantiates anobject from the internal class named junk and plots the data provided bythat class. Thus, you can test the program by running it with nocommand-line parameter. This program provides the followingtext fields for user input, along with a button labeled Graph. This allowsthe user to adjust the parameters and replot the graph as many times with asmany plotting scales as needed: xMin = minimum x-axis valuexMax = maximum x-axis value yMin = minimum y-axis valueyMax = maximum y-axis valuexTicInt = tic interval on x-axis yTicInt = tic interval on y-axisxCalcInc = calculation interval The user can modify any of theseparameters and then click the Graph button to cause the five functionsto be re-plotted according to the new parameters.Whenever the Graph button is clicked, the event handler instantiates a newobject of the class that implements the GraphIntfc01 interface. Dependingon the nature of that class, this may be redundant in some cases. However,it is useful in those cases where it is necessary to refresh the values ofinstance variables defined in the class (such as a counter, for example).Tested using JDK 1.8 under Win 7. This program uses constants that werefirst defined in the Color class of v1.4.0. Therefore, the programrequires v1.4.0 or later to compile and run correctly.**************************************/ import java.awt.*;import java.awt.event.*; import java.awt.geom.*;import javax.swing.*; import javax.swing.border.*;class Graph02{ public static void main(String[] args)throws NoSuchMethodException, ClassNotFoundException,InstantiationException, IllegalAccessException{if(args.length == 1){ //pass command-line parameternew GUI(args[0]);}else{ //no command-line parameter givennew GUI(null); }//end else}// end main }//end class Graph02 definition//===================================// class GUI extends JFrameimplements ActionListener{ //Define plotting parameters and// their default values. double xMin = 0.0;double xMax = 400.0; double yMin = -100.0;double yMax = 100.0; //Tic mark intervalsdouble xTicInt = 20.0; double yTicInt = 20.0;//Tic mark lengths. If too small // on x-axis, a default value is// used later. double xTicLen = (yMax-yMin)/50;double yTicLen = (xMax-xMin)/50; //Calculation interval along x-axisdouble xCalcInc = 1.0; //Text fields for plotting parametersJTextField xMinTxt = new JTextField("" + xMin);JTextField xMaxTxt = new JTextField("" + xMax);JTextField yMinTxt = new JTextField("" + yMin);JTextField yMaxTxt = new JTextField("" + yMax);JTextField xTicIntTxt = new JTextField("" + xTicInt);JTextField yTicIntTxt = new JTextField("" + yTicInt);JTextField xCalcIncTxt = new JTextField("" + xCalcInc);//Panels to contain a label and a // text fieldJPanel pan0 = new JPanel(); JPanel pan1 = new JPanel();JPanel pan2 = new JPanel(); JPanel pan3 = new JPanel();JPanel pan4 = new JPanel(); JPanel pan5 = new JPanel();JPanel pan6 = new JPanel(); //Misc instance variablesint frmWidth = 408; int frmHeight = 430;int width; int height;int number; GraphIntfc01 data;String args = null; //Plots are drawn on theCanvasCanvas theCanvas; //ConstructorGUI(String args)throws NoSuchMethodException,ClassNotFoundException, InstantiationException,IllegalAccessException{ if(args != null){//Save for use later in the // ActionEvent handlerthis.args = args; //Instantiate an object of the// target class using the String // name of the class.data = (GraphIntfc01) Class.forName(args).newInstance(); }else{//Instantiate an object of the // test class named junk.data = new junk(); }//end else//Throw exception if number of // functions is greater than 5.number = data.getNmbr(); if(number>5){ throw new NoSuchMethodException("Too many functions. " + "Only 5 allowed.");}//end if //Create the control panel and// give it a border for cosmetics. JPanel ctlPnl = new JPanel();ctlPnl.setLayout(//?rows x 4 cols new GridLayout(0,4));ctlPnl.setBorder( new EtchedBorder());//Button for replotting the graph JButton graphBtn =new JButton("Graph"); graphBtn.addActionListener(this);//Populate each panel with a label // and a text field. Will place// these panels in a grid on the // control panel later.pan0.add(new JLabel("xMin")); pan0.add(xMinTxt);pan1.add(new JLabel("xMax")); pan1.add(xMaxTxt);pan2.add(new JLabel("yMin")); pan2.add(yMinTxt);pan3.add(new JLabel("yMax")); pan3.add(yMaxTxt);pan4.add(new JLabel("xTicInt")); pan4.add(xTicIntTxt);pan5.add(new JLabel("yTicInt")); pan5.add(yTicIntTxt);pan6.add(new JLabel("xCalcInc")); pan6.add(xCalcIncTxt);//Add the populated panels and the // button to the control panel with// a grid layout. ctlPnl.add(pan0);ctlPnl.add(pan1); ctlPnl.add(pan2);ctlPnl.add(pan3); ctlPnl.add(pan4);ctlPnl.add(pan5); ctlPnl.add(pan6);ctlPnl.add(graphBtn); //Create a custom Canvas object for// all functions to be plotted on. theCanvas = new MyCanvas();theCanvas.setBackground( Color.WHITE);//Add the sub-assemblies to the // frame. Set its location, size,// and title, and make it visible. getContentPane().add(ctlPnl,"South"); getContentPane().add(theCanvas,"Center"); setBounds(0,0,frmWidth,frmHeight);setTitle("Graph02, " + "Copyright 2002, " +"Richard G. Baldwin"); setVisible(true);//Set to exit on X-button click setDefaultCloseOperation(EXIT_ON_CLOSE); //Get and save the size of the// plotting surface width = theCanvas.getWidth();height = theCanvas.getHeight(); //Guarantee a repaint on startup.theCanvas.repaint(); }//end constructor//---------------------------------// //This event handler is registered// on the JButton to cause the // functions to be replotted.public void actionPerformed( ActionEvent evt){//Re-instantiate the object that // provides the datatry{ if(args != null){data = (GraphIntfc01)Class. forName(args).newInstance();}else{ data = new junk();}//end else }catch(Exception e){//Known to be safe at this point. // Otherwise would have aborted// earlier. }//end catch//Set plotting parameters using // data from the text fields.xMin = Double.parseDouble( xMinTxt.getText());xMax = Double.parseDouble( xMaxTxt.getText());yMin = Double.parseDouble( yMinTxt.getText());yMax = Double.parseDouble( yMaxTxt.getText());xTicInt = Double.parseDouble( xTicIntTxt.getText());yTicInt = Double.parseDouble( yTicIntTxt.getText());xCalcInc = Double.parseDouble( xCalcIncTxt.getText());//Calculate new values for the // length of the tic marks on the// axes. If too small on x-axis, // a default value is used later.xTicLen = (yMax-yMin)/50; yTicLen = (xMax-xMin)/50;//Repaint the plotting area theCanvas.repaint();}//end actionPerformed //---------------------------------////This is an inner class, which is used // to override the paint method on the// plotting surface. class MyCanvas extends Canvas{//Factors to convert from double // values to integer pixel locations.double xScale; double yScale;//Override the paint method public void paint(Graphics g){//Calculate the scale factors xScale = width/(xMax-xMin);yScale = height/(yMax-yMin); //Set the origin based on the// minimum values in x and y g.translate((int)((0-xMin)*xScale),(int)((0-yMin)*yScale)); drawAxes(g);//Draw the axes//Draw each curve in a different // color.for(int cnt=0; cnt<number;cnt++){ //Get initial data valuesdouble xVal = xMin; int oldX = getTheX(xVal);int oldY = 0; //Use the curve number to// determine which method to // invoke to get the value for y.switch(cnt){ case 0 :oldY= getTheY(data.f1(xVal)); g.setColor(Color.BLACK);break; case 1 :oldY= getTheY(data.f2(xVal)); g.setColor(Color.BLUE);break; case 2 :oldY= getTheY(data.f3(xVal)); g.setColor(Color.RED);break; case 3 :oldY= getTheY(data.f4(xVal)); g.setColor(Color.MAGENTA);break; case 4 :oldY= getTheY(data.f5(xVal)); g.setColor(Color.CYAN);}//end switch //Now loop and plot the pointswhile(xVal<xMax){ int yVal = 0;//Get next data value. Use the // curve number to determine// which method to invoke to // get the value for y.switch(cnt){ case 0 :yVal = getTheY( data.f1(xVal));break; case 1 :yVal = getTheY( data.f2(xVal));break; case 2 :yVal = getTheY( data.f3(xVal));break; case 3 :yVal = getTheY( data.f4(xVal));break; case 4 :yVal = getTheY( data.f5(xVal));}//end switch1 //Convert the x-value to an int// and draw the next line // segmentint x = getTheX(xVal); g.drawLine(oldX,oldY,x,yVal);//Increment along the x-axis xVal += xCalcInc;//Save end point to use as // start point for next line// segment. oldX = x;oldY = yVal; }//end while loop}//end for loop }//end overridden paint method//---------------------------------// //Method to draw axes with tic marks// and labels in the color GREEN void drawAxes(Graphics g){g.setColor(Color.GREEN); //Label left x-axis and bottom// y-axis. These are the easy // ones. Separate the labels from// the ends of the tic marks by // two pixels.g.drawString("" + (int)xMin, getTheX(xMin),getTheY(xTicLen/2)-2); g.drawString("" + (int)yMin,getTheX(yTicLen/2)+2, getTheY(yMin));//Label the right x-axis and the // top y-axis. These are the hard// ones because the position must // be adjusted by the font size and// the number of characters. //Get the width of the string for// right end of x-axis and the // height of the string for top of// y-axis //Create a string that is an// integer representation of the // label for the right end of the// x-axis. Then get a character // array that represents the// string. int xMaxInt = (int)xMax;String xMaxStr = "" + xMaxInt; char[]array = xMaxStr. toCharArray();//Get a FontMetrics object that can // be used to get the size of the// string in pixels. FontMetrics fontMetrics =g.getFontMetrics(); //Get a bounding rectangle for the// string Rectangle2D r2d =fontMetrics.getStringBounds( array,0,array.length,g);//Get the width and the height of // the bounding rectangle. The// width is the width of the label // at the right end of the// x-axis. The height applies to // all the labels, but is needed// specifically for the label at // the top end of the y-axis.int labWidth = (int)(r2d.getWidth());int labHeight = (int)(r2d.getHeight());//Label the positive x-axis and the // positive y-axis using the width// and height from above to // position the labels. These// labels apply to the very ends of // the axes at the edge of the// plotting surface. g.drawString("" + (int)xMax,getTheX(xMax)-labWidth, getTheY(xTicLen/2)-2);g.drawString("" + (int)yMax, getTheX(yTicLen/2)+2,getTheY(yMax)+labHeight); //Draw the axesg.drawLine(getTheX(xMin), getTheY(0.0),getTheX(xMax), getTheY(0.0));g.drawLine(getTheX(0.0), getTheY(yMin),getTheX(0.0), getTheY(yMax));//Draw the tic marks on axes xTics(g);yTics(g); }//end drawAxes//---------------------------------// //Method to draw tic marks on x-axisvoid xTics(Graphics g){ double xDoub = 0;int x = 0; //Get the ends of the tic marks.int topEnd = getTheY(xTicLen/2); int bottomEnd =getTheY(-xTicLen/2); //If the vertical size of the// plotting area is small, the // calculated tic size may be too// small. In that case, set it to // 10 pixels.if(topEnd<5){ topEnd = 5;bottomEnd = -5; }//end if//Loop and draw a series of short // lines to serve as tic marks.// Begin with the positive x-axis // moving to the right from zero.while(xDoub<xMax){ x = getTheX(xDoub);g.drawLine(x,topEnd,x,bottomEnd); xDoub += xTicInt;}//end while //Now do the negative x-axis moving// to the left from zero xDoub = 0;while(xDoub>xMin){ x = getTheX(xDoub);g.drawLine(x,topEnd,x,bottomEnd); xDoub -= xTicInt;}//end while }//end xTics//---------------------------------// //Method to draw tic marks on y-axisvoid yTics(Graphics g){ double yDoub = 0;int y = 0; int rightEnd = getTheX(yTicLen/2);int leftEnd = getTheX(-yTicLen/2); //Loop and draw a series of short// lines to serve as tic marks. // Begin with the positive y-axis// moving up from zero. while(yDoub<yMax){ y = getTheY(yDoub);g.drawLine(rightEnd,y,leftEnd,y); yDoub += yTicInt;}//end while //Now do the negative y-axis moving// down from zero. yDoub = 0;while(yDoub>yMin){ y = getTheY(yDoub);g.drawLine(rightEnd,y,leftEnd,y); yDoub -= yTicInt;}//end while }//end yTics//---------------------------------// //This method translates and scales// a double y value to plot properly // in the integer coordinate system.// In addition to scaling, it causes // the positive direction of the// y-axis to be from bottom to top. int getTheY(double y){double yDoub = (yMax+yMin)-y; int yInt = (int)(yDoub*yScale);return yInt; }//end getTheY//---------------------------------// //This method scales a double x value// to plot properly in the integer // coordinate system.int getTheX(double x){ return (int)(x*xScale);}//end getTheX //---------------------------------//}//end inner class MyCanvas //===================================//}//end class GUI //===================================////Sample test class. Required for // compilation and stand-alone// testing. class junk implements GraphIntfc01{public int getNmbr(){ //Return number of functions to// process. Must not exceed 5. return 5;}//end getNmbr public double f1(double x){return (x*x*x)/200.0; }//end f1public double f2(double x){ return -(x*x*x)/200.0;}//end f2 public double f3(double x){return (x*x)/200.0; }//end f3public double f4(double x){ return 50*Math.cos(x/10.0);}//end f4 public double f5(double x){return 100*Math.sin(x/20.0); }//end f5}//end sample class junk

Miscellaneous

This section contains a variety of miscellaneous information.

Housekeeping material
  • Module name: Java1468-Plotting Engineering and Scientific Data using Java
  • File: Java1468.htm
  • Published: 04/17/02

Baldwin shows you how write a generalized plotting program that can be used to plot engineering and scientific data produced by any object that implements a very simple interface.

Disclaimers:

Financial : Although the Connexions site makes it possible for you to download a PDF file for thismodule at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should beaware that some of the HTML elements in this module may not translate well into PDF.

I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.

In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. Ineither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please beaware that it is a copy of a module that is freely available on cnx.org and that it was made and published withoutmy prior knowledge.

Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.

-end-

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Digital signal processing - dsp. OpenStax CNX. Jan 06, 2016 Download for free at https://legacy.cnx.org/content/col11642/1.38
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Digital signal processing - dsp' conversation and receive update notifications?

Ask