• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Plotting Program - Assistance with Graphics2D

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For inspiration, maybe check out the PlotTest and PlotSineCurve examples in https://coderanch.com/how-to/java/CodeBarnIntermediateJava
 
Nick Shultz
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think your scaling factors are not correct.
 
Nick Shultz
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You can't expect to wield supreme executive power just because
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic