Solved

Calling a graphing method

Posted on 2006-11-26
1
259 Views
Last Modified: 2010-03-31
Assuming that i have the following code and it works to make just a basic graph show up without any graphing calculations yet....how do I call the graph from the MAIN METHOD to make it show up?

import java.util.Scanner;
import java.text.DecimalFormat;

public class Poly
{
     public static void main (String[] args)
          {
double c4,c3,c2,c1,c0,x,interval,xvalue;
int ptnumber,counter;
String result;

DecimalFormat fmt = new DecimalFormat ("0.##");

Scanner scan = new Scanner (System.in);

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();


Polynomial pn = new Polynomial(c4, c3, c2, c1, c0);

System.out.println("f(x)="+pn);

System.out.println("x     | f(x)");
System.out.println("----------");
counter = 0;
while (counter < ptnumber)
{

System.out.println(x          +"     | "+fmt.format(pn.evaluate(x)));

x = (x + interval);
counter++;
}

     }
}


POLY CLASS

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


import java.text.DecimalFormat;
public class Polynomial
{
   double coeff4, coeff3, coeff2, coeff1, constant;
     String sign4,sign3,sign2,sign1,sign0;
   public Polynomial(double c4, double c3, double c2, double c1, double c0)
     {
          coeff4 = c4;
          coeff3 = c3;
          coeff2 = c2;
          coeff1 = c1;
          constant = c0;
     
     }
     
     public double evaluate(double x)
     {
               
          double xsq = x*x;
         return (coeff4*xsq*xsq + coeff3*xsq*x + coeff2*xsq + coeff1*x + constant);
     
     }
     
     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;
           
  }

     
  public void draw(Graphics page, int xPixels, int yPixels)
{
    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
1 Comment
 
LVL 10

Accepted Solution

by:
ADSLMark earned 250 total points
ID: 18013935
You have to create a class which extends from JFrame. You create an instance of this class in your main method. Next you add an extension of a JPanel with the graph drawing code to the frame and voila.

sth like:

import javax.swing.*;
class MyGraphExample extends JFrame
{
    public MyGraphExample()
    {
        this.setTitle("My graph");
        this.setSize(800,600);
        this.setLayout(new BorderLayout());
        this.add(new MyGraphPanel(), BorderLayout.CENTER);
    }
    public static void main(String[] args)
    {
        MyGraphExample mge = new MyGraphExample();
        mge.setVisible(true);
    }
}

import javax.swing.*;
class MyGraphPanel extends JPanel
{
    public MyGraphPanel()
    {
    }

    public void paintComponent(Graphics g)
    {
        ... perform drawing here ...
    }
}

Good luck.
Mark
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to monitor the number of connections/threads being utilized on Tomcat 6.0 8 71
eclipse shortcuts 9 55
javap not working 8 45
hibernate example using maven 12 42
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

777 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