<< 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

how does Neisseria cause meningitis
Nyibol Reply
what is microbiologist
Muhammad Reply
what is errata
Muhammad
is the branch of biology that deals with the study of microorganisms.
Ntefuni Reply
What is microbiology
Mercy Reply
studies of microbes
Louisiaste
when we takee the specimen which lumbar,spin,
Ziyad Reply
How bacteria create energy to survive?
Muhamad Reply
Bacteria doesn't produce energy they are dependent upon their substrate in case of lack of nutrients they are able to make spores which helps them to sustain in harsh environments
_Adnan
But not all bacteria make spores, l mean Eukaryotic cells have Mitochondria which acts as powerhouse for them, since bacteria don't have it, what is the substitution for it?
Muhamad
they make spores
Louisiaste
what is sporadic nd endemic, epidemic
Aminu Reply
the significance of food webs for disease transmission
Abreham
food webs brings about an infection as an individual depends on number of diseased foods or carriers dully.
Mark
explain assimilatory nitrate reduction
Esinniobiwa Reply
Assimilatory nitrate reduction is a process that occurs in some microorganisms, such as bacteria and archaea, in which nitrate (NO3-) is reduced to nitrite (NO2-), and then further reduced to ammonia (NH3).
Elkana
This process is called assimilatory nitrate reduction because the nitrogen that is produced is incorporated in the cells of microorganisms where it can be used in the synthesis of amino acids and other nitrogen products
Elkana
Examples of thermophilic organisms
Shu Reply
Give Examples of thermophilic organisms
Shu
advantages of normal Flora to the host
Micheal Reply
Prevent foreign microbes to the host
Abubakar
they provide healthier benefits to their hosts
ayesha
They are friends to host only when Host immune system is strong and become enemies when the host immune system is weakened . very bad relationship!
Mark
what is cell
faisal Reply
cell is the smallest unit of life
Fauziya
cell is the smallest unit of life
Akanni
ok
Innocent
cell is the structural and functional unit of life
Hasan
is the fundamental units of Life
Musa
what are emergency diseases
Micheal Reply
There are nothing like emergency disease but there are some common medical emergency which can occur simultaneously like Bleeding,heart attack,Breathing difficulties,severe pain heart stock.Hope you will get my point .Have a nice day ❣️
_Adnan
define infection ,prevention and control
Innocent
I think infection prevention and control is the avoidance of all things we do that gives out break of infections and promotion of health practices that promote life
Lubega
Heyy Lubega hussein where are u from?
_Adnan
en français
Adama
which site have a normal flora
ESTHER Reply
Many sites of the body have it Skin Nasal cavity Oral cavity Gastro intestinal tract
Safaa
skin
Asiina
skin,Oral,Nasal,GIt
Sadik
How can Commensal can Bacteria change into pathogen?
Sadik
How can Commensal Bacteria change into pathogen?
Sadik
all
Tesfaye
by fussion
Asiina
what are the advantages of normal Flora to the host
Micheal
what are the ways of control and prevention of nosocomial infection in the hospital
Micheal
what is inflammation
Shelly Reply
part of a tissue or an organ being wounded or bruised.
Wilfred
what term is used to name and classify microorganisms?
Micheal Reply
Binomial nomenclature
adeolu
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