0

Hello I am fairly new to java and have been stuck on this problem for awhile so hopefully someone will be able to save me. Basically I am creating a program that can graph an equation and right now I'm testing out x^2 between -10 and 10. I can get the points to graph in the right spots but I can't figure out how to fill in the spots in between the points so it looks like a real graph.

Here's my code:

import java.util.Scanner;
import javax.swing.JFrame;
import java.awt.*;

class PlotGraph extends JFrame{


public void paint(Graphics l){

    l.drawLine(50, 300, 550, 300); //x axis
    l.drawLine(300, 550, 300, 50); //y axis
    //Orignin x = 300 y = 300

    int xmin, xmax, y, tmin, tmax;
    xmin =(-10);
    xmax = 10;
    int x_bet, y_bet;

    while(xmin<=xmax){
        y = 300-(xmin*xmin);
        l.drawLine(xmin+300, y, xmin+300, y);

        //while(x_bet>xmin){
        //l.drawLine(, , , );
        //}

        xmin++;
    }



}

public static void main(String [] args) {

    PlotGraph graph = new PlotGraph();
    graph.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    graph.setSize(600, 600);
    graph.setVisible(true); 
    graph.setTitle("PlotGraph");

}


}
Frank
  • 16,476
  • 7
  • 38
  • 51
CodeNewbie
  • 57
  • 2
  • 8

3 Answers3

2

Another way to do it is to create a GeneralPath, like this.

import javax.swing.JFrame;
import java.awt.*;
import java.awt.geom.*;

class PlotGraph extends JFrame{

    public void paint(Graphics l){

        l.drawLine(50, 300, 550, 300); //x axis
        l.drawLine(300, 550, 300, 50); //y axis

        int xmin, xmax, y, tmin, tmax;
        xmin =(-10);
        xmax = 10;
        int x_bet, y_bet;
        GeneralPath gp = new GeneralPath();

        y = 300-(xmin*xmin);
        gp.moveTo((double)xmin+300, (double)y);
        while(xmin<=xmax){
            y = 300-(xmin*xmin);
            gp.lineTo((double)xmin+300, (double)y);

            xmin++;
        }

        Graphics2D g2 = (Graphics2D)l;
        g2.setColor(Color.RED);
        g2.draw(gp);
    }

    public static void main(String [] args) {
        PlotGraph graph = new PlotGraph();
        graph.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        graph.setSize(600, 600);
        graph.setVisible(true);
        graph.setTitle("PlotGraph");
    }
}

This source still has problems though:

  1. GUI updates should be done on the EDT.
  2. Custom painting is either best done in a JPanel/JComponent or a BufferedImage displayed in a JLabel.
  3. The 'graph component' should declare a preferred size, rather than setting a size for the frame..
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

Maybe try this:

int x = xmin;
int last_y = 300-(x*x);
for (x = xmin+1; x<=xmax; x++);
    y = 300-(x*x);
    l.drawLine(x-1, last_y, x, y);
    last_y = y;
}

You want the line to be drawn between previous x and y coordinates and the current ones. That's what last_y is for.

angelatlarge
  • 4,086
  • 2
  • 19
  • 36
  • Man thanks so much that worked perfectly. I do have another question though, if I wanted to graph sine and cosine functions what library do I have to import? – CodeNewbie Mar 10 '13 at 06:03
  • 1
    `java.Math` has `sin()` and `cos()`. Just remember that these functions take radians, not degrees. Javadoc for the Math package [here](http://docs.oracle.com/javase/6/docs/api/index.html?java/lang/Math.html) – angelatlarge Mar 10 '13 at 06:05
0

There are several ways to do this. If this is an assignment then I'm guessing that your professor is looking to see what methods you come up with. At the most basic level you just draw a line from the last point to the current point. How else could you accomplish this? Maybe a polyline in a different color? Maybe use a little math to draw larger circles a then run a line through them?

Here is one simple way to address the specific problem. I wouldn't recommend turning this in (if it's an assignment) but it shows you the basic principle.

import java.util.Scanner;
import javax.swing.JFrame;
import java.awt.*;
class PlotGraph extends JFrame{
public void paint(Graphics l){

l.drawLine(50, 300, 550, 300); //x axis
l.drawLine(300, 550, 300, 50); //y axis

int xmin, xmax, y, tmin, tmax;
int z = 0;
xmin =(-10);
xmax = 10;
int x_bet, y_bet;
while(xmin<=xmax){
    y = 300-(xmin*xmin);
    l.drawLine(xmin+300, y, xmin+300, y);
    if(z!=0)
    l.drawLine(xmin+300, y, xmin+300, z);
    z=y;
    xmin++;
}
}

public static void main(String [] args) {
PlotGraph graph = new PlotGraph();
graph.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
graph.setSize(600, 600);
graph.setVisible(true); 
graph.setTitle("PlotGraph");
}
}
grauwulf
  • 376
  • 2
  • 13