<< 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 do you get the 2/50
Abba Reply
number of sport play by 50 student construct discrete data
Aminu Reply
width of the frangebany leaves on how to write a introduction
Theresa Reply
Solve the mean of variance
Veronica Reply
Step 1: Find the mean. To find the mean, add up all the scores, then divide them by the number of scores. ... Step 2: Find each score's deviation from the mean. ... Step 3: Square each deviation from the mean. ... Step 4: Find the sum of squares. ... Step 5: Divide the sum of squares by n – 1 or N.
kenneth
what is error
Yakuba Reply
Is mistake done to something
Vutshila
Hy
anas
hy
What is the life teble
anas
hy
Jibrin
statistics is the analyzing of data
Tajudeen Reply
what is statics?
Zelalem Reply
how do you calculate mean
Gloria Reply
diveving the sum if all values
Shaynaynay
let A1,A2 and A3 events be independent,show that (A1)^c, (A2)^c and (A3)^c are independent?
Fisaye Reply
what is statistics
Akhisani Reply
data collected all over the world
Shaynaynay
construct a less than and more than table
Imad Reply
The sample of 16 students is taken. The average age in the sample was 22 years with astandard deviation of 6 years. Construct a 95% confidence interval for the age of the population.
Aschalew Reply
Bhartdarshan' is an internet-based travel agency wherein customer can see videos of the cities they plant to visit. The number of hits daily is a normally distributed random variable with a mean of 10,000 and a standard deviation of 2,400 a. what is the probability of getting more than 12,000 hits? b. what is the probability of getting fewer than 9,000 hits?
Akshay Reply
Bhartdarshan'is an internet-based travel agency wherein customer can see videos of the cities they plan to visit. The number of hits daily is a normally distributed random variable with a mean of 10,000 and a standard deviation of 2,400. a. What is the probability of getting more than 12,000 hits
Akshay
1
Bright
Sorry i want to learn more about this question
Bright
Someone help
Bright
a= 0.20233 b=0.3384
Sufiyan
a
Shaynaynay
How do I interpret level of significance?
Mohd Reply
It depends on your business problem or in Machine Learning you could use ROC- AUC cruve to decide the threshold value
Shivam
how skewness and kurtosis are used in statistics
Owen Reply
yes what is it
Taneeya
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