This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Plotting Program - Assistance with Graphics2D Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Plotting Program - Assistance with Graphics2D" Watch "Plotting Program - Assistance with Graphics2D" New topic
Author

Plotting Program - Assistance with Graphics2D

Nick Shultz
Greenhorn

Joined: Aug 26, 2012
Posts: 4
Hi, I am building a program which plots data from an Nx3 array - the first column being X-axis data and the other two being two sets of Y-axis data. The desired output is a JFrame with two curves plotted on a graph - one black and one red. Because the program does not know how large the array is until it is inputted, the plot must be scaled to fit within the JFrame. I am also adding text to the axes, but have not completely implemented this yet.

The problem: I am having trouble getting Graphics2D to display the plots. The way I am making the plots is assigning points from the scaled arrays and utilizing Line2D to connect the points. My Implementation so far - the main class, the class containing paintComponent, and a test suite - are shown below. Please note that the axisScale method and some of the arrays are for text display, and have not yet been implemented in the PlotBuild class.








Any advice about how to proceed from here is greatly appreciated. Thank you in advance!
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Hello Nick, and welcome to the Ranch! Have you considered using JFreeChart? or is this an exercise to reinvent that particular wheel?


luck, db
There are no new questions, but there may be new answers.
Nick Shultz
Greenhorn

Joined: Aug 26, 2012
Posts: 4
Hi Darryl,

I would personally love to use JFreeChart but this is an assignment for a class, and my instructor wants a from-scratch graphing utility. I do not mind scrapping code if there is a better way to do this using built-in classes.

Thanks!
Nick
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41125
    
  45
For inspiration, maybe check out the PlotTest and PlotSineCurve examples in http://www.coderanch.com/how-to/java/CodeBarnIntermediateJava


Ping & DNS - my free Android networking tools app
Nick Shultz
Greenhorn

Joined: Aug 26, 2012
Posts: 4
Thank you for the help so far. I rewrote the code into one class and took a lot of inspiration from the Plot Test example. I'm still having trouble getting the graph to display using the test data defined in the assignVals method, although I can see the axes. Right now I am only plotting the x-data vs. y1-data.



Any thoughts as to where I could be going wrong?

Thanks!
Nick

Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5


I think your scaling factors are not correct.


The quieter you are, the more you are able to hear.
Nick Shultz
Greenhorn

Joined: Aug 26, 2012
Posts: 4
Hi Kemal, you were right! I did get a plot to display, but only sometimes - other times it would just draw straight horizontal/vertical lines. I did rewrite the code so that it makes more sense, but for whatever reason I am having trouble getting the x-Axis results to display. Would anyone mind taking a look?

Note that the teacher assigned an analysis problem to replace the test data. The results should now look like two curves.

Thank you again!
Nick

Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

I just ran your code through debugger and I noticed that here:

you are dividing by zero, so pointX has the value of Infinity.
 
jQuery in Action, 2nd edition
 
subject: Plotting Program - Assistance with Graphics2D
 
Similar Threads
How to plot a graph in Java ?
Image Resizing Woes
Drawing graphs
"Drawing" Points on screen
jPanel ,nullpointer exception