?
Solved

Need help with paintComponent for a graph

Posted on 2006-11-26
9
Medium Priority
?
232 Views
Last Modified: 2010-03-31
I wrote a program that could take the values of a polynomial and calculate f(x). Im trying to get these values to graph,but when JFrame is called it just gives me a blank frame. How come none of my tick marks or my toString method isn't getting 'painted' onto the panel? I made a seperate class just like i was told to do on a previous question,but yet it just makes a blank panel. Any help would be greatly appreciated.

My code is as follows

MAIN

import java.util.Scanner;
import java.text.DecimalFormat;
import javax.swing.*;
import java.awt.*;

public class Poly
{
      public static void main (String[] args)
            {

//Sets up variables
double c4,c3,c2,c1,c0,x,interval,xvalue;
int ptnumber,counter;
String result;
//Sets up deciamal formatting so that only 3 significant digits are displayed
DecimalFormat fmt = new DecimalFormat ("0.##");

Scanner scan = new Scanner (System.in);
//Asks the user to input 4 coefficients,a constant,number of values to calculate,and the starting point of x
System.out.print ("Enter the coefficient of x^4(0.0 if none:");
c4 = scan.nextDouble();

System.out.print ("Enter the coefficient of x^3(0.0 if none:");
c3 = scan.nextDouble();

System.out.print ("Enter the coefficient of x^2(0.0 if none:");
c2 = scan.nextDouble();

System.out.print ("Enter the coefficient of x^1(0.0 if none:");
c1 = scan.nextDouble();

System.out.print ("Enter the constant:");
c0 = scan.nextDouble();

System.out.print ("Enter the starting value of x:");
x = scan.nextDouble();

System.out.print ("Enter the number of points to be graphed(Integer):");
ptnumber = scan.nextInt();

System.out.print ("Enter the interval between points:");
interval = scan.nextDouble();

//Sets up an polynomial object with data in it
Polynomial pn = new Polynomial(c4, c3, c2, c1, c0);
//Calls the toString method to print out the formula with proper formatting
System.out.println("f(x)="+pn);
//Prints the lables for the table
System.out.println("x      | f(x)");
System.out.println("----------");
//Initiates Counter
counter = 0;
//Starts a while loop that loops according to how many values are to be printed
while (counter < ptnumber)
{
//Calls the evaluate method to calculate f(x) from x and then prints it so many times
System.out.println(fmt.format(x)            +"      | "+fmt.format(pn.evaluate(x)));
//Calculates the x intervals so that f(x) can be calculated appropriately
x = (x + interval);
//Adds one to variable counter for each loop
counter++;


}
JFrame frame = new JFrame("Polynomial");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new PolynomialPanel());
frame.pack();
frame.setVisible(true);
       }
}
 
POLYNOMIALPANEL CLASS

import javax.swing.*;
import java.awt.*;
import java.text.DecimalFormat;

public class PolynomialPanel extends JPanel

{

public PolynomialPanel()

{
      
setPreferredSize (new Dimension(600,600));

}

     
 public void paintComponent(Graphics page, int xPixels, int yPixels)
{
      super.paintComponent (page);
    final int NUM = 11;
    final double INTERVAL = 0.5;
    final double XSTART = 0.0;

    final int XMARGIN = 70;
    final int XUSABLEPIXELS = xPixels - 2*XMARGIN;
    final int XINTERVALPIXELS = XUSABLEPIXELS/(NUM - 1);
    final int YMARGIN =40;
    final int YUSABLEPIXELS = yPixels - 2*YMARGIN;
     
       // Label the graph
    page.drawString("Graph of f(x) = " + toString(), XMARGIN + 20, YMARGIN);

    // Draw the x axis and put in hash marks
    page.drawLine(XMARGIN, yPixels - YMARGIN, xPixels - XMARGIN, yPixels - YMARGIN);
    for ( int i = 0; i < NUM; i++ )
        page.drawLine(XMARGIN + i * XINTERVALPIXELS, yPixels - YMARGIN, XMARGIN + i * XINTERVALPIXELS, yPixels - YMARGIN + 5);
           
      DecimalFormat twoPlaces = new DecimalFormat("0.00");

    // Label the x axis
    for (int i = 0; i < NUM; i++)
    {
        page.drawString( twoPlaces.format(XSTART + i * INTERVAL), 3 * XMARGIN / 4 + i * XINTERVALPIXELS, yPixels - YMARGIN/2);
    }

    // Draw the y axis
    page.drawLine(XMARGIN, YMARGIN, XMARGIN, yPixels - YMARGIN);

    // Label the y axis and put in hash marks
    final double YMIN = 0.0;
    final double YINTERVAL = 100.0;
    final int YINTERVALPIXELS = YUSABLEPIXELS / 10;

    for (int i = 0; i < NUM; i++)
    {
        page.drawString(twoPlaces.format(YMIN + i * YINTERVAL), XMARGIN / 4, yPixels - i * YINTERVALPIXELS - YMARGIN);
        page.drawLine(XMARGIN, yPixels - YMARGIN - i * YINTERVALPIXELS, XMARGIN - 5, yPixels - YMARGIN - i * YINTERVALPIXELS);
    }

   
   }
}
0
Comment
Question by:lusius188
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
9 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 18015191
The method prototype needs to be not

>>public void paintComponent(Graphics page, int xPixels, int yPixels)

but

public void paintComponent(Graphics page)

or it won't be called
0
 

Author Comment

by:lusius188
ID: 18015266
I changed it like CEHJ said to ,but then my variables XPixels and YPixels had to be declaired and initialized. I initialized them both to 0 just for a test trial and it worked!!! except for the fact that my tick marks are in a 1 by 1 square up at the top all bunched up and my toString () method is just printing garbage even though when it is called from any of my other classes it reports  a properly formatted string.,
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18015275
You need to check your geometry. Please post toString
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:lusius188
ID: 18015344
public String toString()

  {
        String result = "";

           if (coeff4 != 0)
        {
               result += coeff4+"x^4";
           }
          if (coeff3 != 0)
        {
            if (coeff3 > 0)
                result += "+";
             
               result += coeff3+"x^3";
           }
          if (coeff2 != 0)
        {
            if (coeff2 > 0)
                result += "+";
             
                result += coeff2+"x^2";
           }
           if (coeff1 != 0)
        {
            if (coeff1 > 0)
                result += "+";
             
               result += coeff1+"x";
           }
       if(constant!=0)
     {
      if(constant>0)
          result+="+";

         result += constant;
      }

          return result;
           
  }

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18015431
Ah that rings a bell ;-) I've already posted a working example of that
0
 
LVL 92

Assisted Solution

by:objects
objects earned 1000 total points
ID: 18017541
you're not calling that toString() method from your paintComponent() method, your calling the PolynomialPanel 's toString() method. You panel needs to call the method on the object that conatins your coeffs
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month8 days, 16 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question