• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Plotting Program - Assistance with Graphics2D

 
Nick Shultz
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Nick, and welcome to the Ranch! Have you considered using JFreeChart? or is this an exercise to reinvent that particular wheel?
 
Nick Shultz
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For inspiration, maybe check out the PlotTest and PlotSineCurve examples in http://www.coderanch.com/how-to/java/CodeBarnIntermediateJava
 
Nick Shultz
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I think your scaling factors are not correct.
 
Nick Shultz
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just ran your code through debugger and I noticed that here:

you are dividing by zero, so pointX has the value of Infinity.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic