New Window in JFrame/Graphing in a JPanel
I'm writing a program that takes some data from a JFrame window, crunches the numbers, then outputs the linear regression line in a JScrollPane in JOptionPane window. This part works fine, because the only output so far is text, and that is easy to display in a JOptionPane. The part I am having trouble with, however, is displaying a graph. I know what functions etc. (i.e. the math for drawing the graph) I need to create the graph, but I don't know how to create the surface on which to graph it. Either 1) a new window showing the graph or 2) a JPanel in the main JFrame window would be acceptable, but I have run into trouble with both. With option 1) I don't know how to create a new window. I have searched around on Sun's site, but when I try to use their methods, I get errors that are above my novice level. Part of the problem is that I haven't seen any code examples of this in the context of a full program. With option 2) I am having trouble because I am not sure how to include the JPanel in the GridBagLayout that I am using. Another problem is that I don't know how to Draw in the JPanel. If anyone has suggestions about which option to select (I would prefer 1) if it feasible, even if it is more complex), how to go about implementing these option, or has any code samples that might be of help, I would be much obliged.
To help give you an idea of what I am doing, here is a code sample:
public void calc( String names[], double earnings[], double overhead[] )
{
double sumx = 0;
double sumy = 0;
double xbar = 0;
double ybar = 0;
double slope = 0;
double intercept = 0;
for ( int n=0; n<25; n++ )
{
sumx += overhead[n];
sumy += earnings[n];
}
xbar = sumx / numdocs;
ybar = sumy / numdocs;
double sumnum1 = 0;
double sumnum2 = 0;
double sumnum3 = 0;
double sumnum = 0;
for ( int n=0; n<25; n++ )
sumnum1 += ( overhead[n] * earnings[n] );
sumnum1 = numdocs * sumnum1;
for ( int n=0; n<25; n++ )
{
sumnum2 += overhead[n];
sumnum3 += earnings[n];
}
sumnum = sumnum1 - ( sumnum2 * sumnum3 );
double sumden1 = 0;
double sumden2 = 0;
double sumden = 0;
for ( int n=0; n<25; n++ )
sumden1 += Math.pow( overhead[n], 2 );
sumden1 = numdocs * sumden1;
for ( int n=0; n<25; n++ )
sumden2 += overhead[n];
sumden2 = Math.pow( sumden2, 2 );
sumden = sumden1 - sumden2;
slope = sumnum / sumden;
intercept = ybar - ( slope * xbar );
slope = round( slope );
intercept = round( intercept );
double index[] = new double[25];
for ( int n=0; n<25; n++ )
index[n] = (earnings[n] - ((slope * overhead[n]) + intercept));
double sumsquares = 0;
for ( int n=0; n<numdocs; n++ )
sumsquares += Math.pow( index[n], 2 );
sumsquares = round( sumsquares );
double hold = 0;
String holdn = "";
for ( int n=0; n><numdocs; n++ )
{
for ( int i=0; i><(numdocs-1); i++ )
{
if ( index > index[i+1] )
{
hold = index;
index = index[i+1];
index[i+1] = hold;
holdn = names;
names = names[i+1];
names[i+1] = holdn;
}
}
}
display( names, index, slope, intercept, sumsquares, earnings, overhead );
}
public void display( String names[], double index[], double slope, double intercept, double sumsquares, double earnings[], double overhead[] )
{
String output = "";
JTextArea outputArea = new JTextArea( 30, 30 );
JScrollPane scroller = new JScrollPane( outputArea );
output += "Regression line where Y is the predicted\nearnings and X is the overhead:\nY = " + slope + " X + " + intercept + "\n\nFairness Index (Sum of the Errors Squared): " + sumsquares + "\n\n";
for ( int n=0; n<numdocs; n++ )
output += names[n] + "\t\t" + round(index[n]) + "\n";
outputArea.setText( output );
JOptionPane.showMessageDialog( null, scroller, "Fairness Table", JOptionPane.INFORMATION_MESSAGE );
}
public double round( double x )
{
return Math.floor( x * 100 + .5 ) / 100;
}
public static void main( String args[] )
{
Fairness application = new Fairness();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
Everythings before that sets up the JFrame, and gets the values for the arrays. That is the meat of the calculations, and the part of the display that I have set up. My problem is that I want to set up a method to display the graph (see previous post).>
Wow - big topic. Here are some starting points:
> Either 1) a new window
> showing the graphJDialog dialog = new JDialog(application, "The graph");
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
JPanel graph = new JPanel();
dialog.getContentPane().add(graph, "Center");
dialog.setVisible(true);
> or 2) a JPanel in the main JFrame
> window would be acceptableJPanel graph = new JPanel();
application.getContentPane().add(graph, "Center");
> I am having trouble because I am not sure how to
> include the JPanel in the GridBagLayout that I am
> using.
You can add any component to a GridBagLayout, including a JPanel. You might want to set the GridBagConstraints weightX and weightY to 1 for the graph so it expands to fill the space available.
> Another problem is that I don't know how to
> Draw in the JPanel.
Subclass your JPanel so you can override the paintComponent method(). Put your drawing code in there...public class Graph extends JPanel {
public void paintComponent(Graphics g) {
g.draw ...// Look up the class Graphics in the Javadocs to see what operations you have available.
}
}
Hope that helps!
Regards,
Tim