<< 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 mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
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