• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger

How to draw values stored in 2 arrays on a graph using Swing

Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm almost finished with this, I just can't figure out the last part, which is to graph values of pi stored in the arrays ypts, and ypts1.

Everything is working as it should, and I get the graph to show up, but I'm stumped as to how to draw the lines indicating the values stored in the arrays.

I'm pretty sure the missing logic should go somewhere in in my , but so far nothing I have tried is working.

Neither the Professor nor TA  are available to assist.

If someone could take a look and help me figure out where where my for loop to iterate over the fields ypts, and ypts1 should go/be, I would appreciate it.

Here are the instructions:


In the same package write another program that graphs (draws) the results from invoking each series with 1 to 5 terms.

This program should extend JComponent and have one field that is an instance of MadhavaAlgorithms.

Add a constructor for this program that instantiates an instance of the MadhavaAlgorithms field with 5 data points for each algorithm.

Add a paintComponent method.

Instantiate a JFrame in the main method of this program.

Your program should draw the MadhavaAlgorithms field from its paintComponent method by invoking its draw method.

But first we will have to add some features to MadhavaAlgorithms.

Make these additional modifications to MadhavaAlgorithms.


Add a second constructor that takes n as a parameter and initializes the fields ypts and ypts1 with n data points.

Add a draw method that takes a Graphics2D parameter. Here is the beginning of the draw method, which will produce the graph framework. Use a separate for loop to iterate over the fields ypts (green) and ypts1 (blue) to draw the graphs:

Here is my code:

Saloon Keeper
Posts: 12012
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim,

You'd just put the loop at the end of your draw() method. Iterate through an array of data points and draw a line from the previous data point to the next one. Then do the same in a second loop for the second array.

As a side note, the fields of the MadhavaAlgorithms class seem pretty useless. When plotting the data points, you can just calculate them on the fly by calling either of the two approximation functions directly. Make the functions static, they don't depend on any object state. You then also don't need constructors.

Draw is a pretty poor name for a component. You'd want to call it GraphComponent or GraphPanel or something instead. If you let it extend JPanel and call super.paintComponent(g) in the first line of your paintComponent() method, you can make it look like a normal component and then draw lines on top of it.
Posts: 3959
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And to add to what Stephan said: your program would benefit enormously if you use UserCoordinates for your drawingPanels. For instance, if your panel measures 500x500, you can set it to represent (0, 0) left bottom, to (6.0, 4) topright. Then you can draw directly to your panel wit, say, g2.drawLine(3, mPI(3)) to (4, mPI(4)). Makes life a lot easier.
Roses are red, violets are blue. Some poems rhyme and some don't. And some poems are a tiny ad.
Devious Experiments for a Truly Passive Greenhouse!
    Bookmark Topic Watch Topic
  • New Topic