<< Chapter < Page
  Digital signal processing - dsp     Page 20 / 25
Chapter >> Page >

A simple constructor

The code in Listing 29 also defines the constructor, whose only purpose is to save an integer identifying the position of this object in the vertical stack of MyCanvas objects.

Beginning of the overridden paint method

The beginning of the overridden paint method for the MyCanvas class is shown in Listing 30 .

Listing 30. Beginning of the overridden paint method.
public void paint(Graphics g){ //Calculate the scale factorsxScale = width/(xMax-xMin); yScale = height/(yMax-yMin);//Set the origin based on the // minimum values in x and yg.translate((int)((0-xMin)*xScale), (int)((0-yMin)*yScale));drawAxes(g);//Draw the axes g.setColor(Color.BLACK);

The code in Listing 30 :

  • Calculates and saves the scale factors for converting from double coordinate values to integer values inpixels.
  • Moves the plotting origin to the correct location.
  • Calls a method to draw the axes (in red) on the MyCanvas object.
  • Sets the color to black for the remainder of the plotting activity on the object.

Get old coordinate values

The plotting process consists of drawing straight line segment between pairs ofpoints. For each line segment, one of the points is defined by a pair of old coordinate values. The other point is defined by a pair of new coordinate values.

The code in Listing 31 initializes the beginning point for the plot. The initial value for the x-coordinate is the left edge of the plotting area.

Listing 31. Get old coordinate values.
//Get initial data values double xVal = xMin;int oldX = getTheX(xVal); int oldY = 0;//Use the Canvas obj number to // determine which method to// call 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

The initial y-coordinate value

The initial value for the y-coordinate depends on which function is being plotted on the MyCanvas object. Recall that each MyCanvas object contains an instance variable that identifies its position in the vertical stack of MyCanvas objects. The switch statement in Listing 31 uses that information to call one of the five methods named f1 through f5 . This gets the correct value for the y-coordinate based on the value of the x-coordinatefor each MyCanvas object.

The methods named getTheX and getTheY called by the code in Listing 31 convert the coordinate values from type double to integer values in pixels.

The method named getTheY also changes the sign on the data so that positive y-values go up the screen rather than down the screen.

(By default, positive vertical coordinate values go down the screen from top to bottom in Java.)

Plot the points

The remainder of the overridden paint method is shown in Listing 32 .

Plot the points.
//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

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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