Results 1 to 7 of 7
  1. #1
    ttown is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default Can't get my Java Program to Paint. Extends JFrame

    Hi, I'm new to Java. We have an assignment due on Friday that asks us to input a equation, x bounds, and y bounds. Then the program draws it.

    I don't get how to get it to draw. I'm following the book's manual.

    Here is what I have so far:
    /**
    * @(#)ExpressionPlot.java
    *
    * ExpressionPlot application
    *
    * @author
    * @version 1.00 2011/4/26
    */


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;

    public class ExpressionPlot extends JFrame {
    private String stringFunction;

    public void setStringFunction(String x){
    stringFunction = x;
    }
    public String getStringFunction(){
    return stringFunction;
    }

    public ExpressionPlot(){
    add(new ExpressionPlot());
    //What do I put here?
    // add(new JPanel());
    }

    public static void main(String[] args) {
    if(args.length != 3){
    System.out.println("Usage: java ExpressionPlot [Function] [X bound] [Y bound]");
    }

    String stringFunction = args[0];
    String xBoundString = args[1];
    String yBoundString = args[2];

    System.out.println("Function: "+args[0]+"\nX Bound: "+args[1]+"\nY Bound: "+args[2]);

    ExpressionPlot frame = new ExpressionPlot();
    frame.setStringFunction(args[0]);
    frame.setTitle("ExpressionPlot");
    frame.setSize(1000,1000);
    frame.setLocationRelativeTo(null);


    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setVisible(true);
    }

    protected void paintComponent(Graphics g){
    int xorigin = 50;
    int yorigin = 950;
    //String stringFunction = s;
    String test=getStringFunction();
    //BEGIN GRAPH
    int x[] = {xorigin,xorigin,yorigin};
    int y[] = {xorigin,yorigin,yorigin};
    g.drawPolyline(x,y,x.length);

    //TEST FUNCTION
    //attempt to graph y = x*2
    int bound = 9;
    int xpoly[] = new int[bound]; //declare points to be made
    for(int i = 0; i<bound; i++){ //read in command line function here
    xpoly[i] = i*xorigin + xorigin;
    }
    int ypoly[] = new int[bound]; //declare points to be made
    for(int i = 0; i<bound; i++){
    ypoly[i] = yorigin - ((xpoly[i]*2)-2*xorigin); //read in command line function here
    }
    g.drawPolyline(xpoly,ypoly,xpoly.length);
    }


    }
    If I remove the constructor for ExpressionPlot, then it works fine, but doesn't graph anything. If I put new ExpressionPlot() in the constructor, I get a loop of StackOverflow.

    I'm so confused!!

    Can anyone shed some light on how to get it to graph? I'm sure im making it harder than it needs to be

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You'd better re-read the chapter as you're making several basic mistakes, the major one being that you're drawing in a class that extends the JFrame and not a JPanel or JComponent. Again, re-read your chapter and it will likely show you how to do this correctly.

  3. #3
    ttown is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    okay thanks. that's what i've been trying. i read that you can extend JFrame and still do it, the example it shows is only the basic code. I'm trying to fill the rest in myself. I figured out that this works better.

    If I do my code like this:
    /**
    * @(#)ExpressionPlot.java
    *
    * ExpressionPlot application
    *
    * @author
    * @version 1.00 2011/4/26
    */


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;

    public class ExpressionPlot extends JFrame {
    private String stringFunction;

    public void setStringFunction(String x){
    stringFunction = x;
    }
    public String getStringFunction(){
    return stringFunction;
    }

    public ExpressionPlot(){
    //add(new ExpressionPlot());
    //What do I put here?
    add(new JFrame());
    }

    public static void main(String[] args) {
    if(args.length != 3){
    System.out.println("Usage: java ExpressionPlot [Function] [X bound] [Y bound]");
    }

    String stringFunction = args[0];
    String xBoundString = args[1];
    String yBoundString = args[2];

    System.out.println("Function: "+args[0]+"\nX Bound: "+args[1]+"\nY Bound: "+args[2]);

    ExpressionPlot frame = new ExpressionPlot();
    frame.setStringFunction(args[0]);
    frame.setTitle("ExpressionPlot");
    frame.setSize(1000,1000);
    frame.setLocationRelativeTo(null);


    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setVisible(true);
    }


    protected void paintComponent(Graphics g){

    int[] x_points = null;
    int[] y_points = null;
    double[] coefs = null;
    double x_min, x_max, y_min, y_max;

    x_points = new int[100];
    y_points = new int[100];
    x_min = -100;
    x_max = 100;
    y_min = -100;
    y_max = 100;
    Color myblue = new Color(10,10,100);

    Dimension size = getSize();
    for (int i=0; i<x_points.length; i++)
    {
    double x = i * (x_max - x_min) / x_points.length + x_min;
    double y = 0.0;
    for (int k=0; k<coefs.length; k++)
    {
    y = y*x + coefs[k];
    }
    x_points[i] = (int) (size.width * (x-x_min) / (x_max-x_min));
    y_points[i] = (int) (size.height * (y-y_max) / (y_min-y_max));
    }

    g.drawPolyline(x_points, y_points, x_points.length);
    }


    }
    In the class constructor, I'm adding a new JFrame and am trying to use that.

    It doesnt have a stack error, but doesnt draw anything. I think I have to use get or set container in my main function to show whats being done in paintcomponent, right? I've tried a combination but am confused as none work.

    "frame.getContentPane().add(new PlotExpression());" is what it says I have to use to get it to graph the graph, but it says I'm adding a window to a container. What's the easiest way to go about fixing this.
    Last edited by ttown; 04-27-2011 at 05:07 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ttown View Post
    okay thanks. that's what i've been trying. i read that you can extend JFrame and still do it, the example it shows is only the basic code. I'm trying to fill the rest in myself. I figured out that this works better.

    If I do my code like this:
    Again, I recommend that you draw in JPanel or JComponent. It's up to you to do as you choose, but I suggest you go with my recommendation if you want this project to succeed.


    It doesnt have a stack error, but doesnt draw anything.
    I think I have to use get or set container in my main function to show whats being done in paintcomponent, right? I've tried a combination but am confused as none work.
    Because you're not doing what we recommend. But again, up to you.


    "frame.getContentPane().add(new PlotExpression());" is what it says I have to use to get it to graph the graph, but it says I'm adding a window to a container. What's the easiest way to go about fixing this.
    You're trying to add a JFrame to a JFrame. Again, please paint in a JPanel.
    Last edited by Fubarable; 04-27-2011 at 05:19 AM.

  5. #5
    ttown is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    sorry! I'm just really confused. I'm supposed to give this to my partner to finish tonight (theres another part) and im trying to head to bed. Want to send him something working.

    if I want to extend Jpanel, is there a way I can add the container to the class even though it extends JPanel and not JFrame? Just trying to find a quick example? ..if that's okay. If not I'll just send him what I have and continue when he gives it back. Just wanted to give him something working.

    EDIT: (I Did it!!)
    /**
    * @(#)ExpressionPlot.java
    *
    * ExpressionPlot application
    *
    * @author
    * @version 1.00 2011/4/26
    */


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;

    public class ExpressionPlot extends JPanel {
    private String stringFunction;

    public void setStringFunction(String x){
    stringFunction = x;
    }
    public String getStringFunction(){
    return stringFunction;
    }

    public ExpressionPlot(){
    //add(new ExpressionPlot());
    //What do I put here?
    //add(new JFrame());
    }

    public static void main(String[] args) {
    if(args.length != 3){
    System.out.println("Usage: java ExpressionPlot [Function] [X bound] [Y bound]");
    }

    String stringFunction = args[0];
    String xBoundString = args[1];
    String yBoundString = args[2];

    System.out.println("Function: "+args[0]+"\nX Bound: "+args[1]+"\nY Bound: "+args[2]);

    //ExpressionPlot frame = new ExpressionPlot();
    //frame.setStringFunction(args[0]);
    //frame.setTitle("ExpressionPlot");
    JFrame frame = new JFrame();
    frame.setSize(1000,1000);
    frame.setLocationRelativeTo(null);
    frame.getContentPane().add(new ExpressionPlot());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setVisible(true);
    }


    protected void paintComponent(Graphics g){

    int xorigin = 50;
    int yorigin = 950;
    //String stringFunction = s;
    String test=getStringFunction();
    //BEGIN GRAPH
    int x[] = {xorigin,xorigin,yorigin};
    int y[] = {xorigin,yorigin,yorigin};
    g.drawPolyline(x,y,x.length);

    //TEST FUNCTION
    //attempt to graph y = x*2
    int bound = 9;
    int xpoly[] = new int[bound]; //declare points to be made
    for(int i = 0; i<bound; i++){ //read in command line function here
    xpoly[i] = i*xorigin + xorigin;
    }
    int ypoly[] = new int[bound]; //declare points to be made
    for(int i = 0; i<bound; i++){
    ypoly[i] = yorigin - ((xpoly[i]*2)-2*xorigin); //read in command line function here
    }
    g.drawPolyline(xpoly,ypoly,xpoly.length);
    }


    }
    now I just have to figure out how to read in the equation in getStringFunction and plot using that. You've already pointed me where to look for the first part.

    Any tips on where to begin on that?
    Last edited by ttown; 04-27-2011 at 05:21 AM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ttown View Post
    if I want to extend Jpanel, is there a way I can add the container to the class even though it extends JPanel and not JFrame? Just trying to find a quick example? ..if that's okay. If not I'll just send him what I have and continue when he gives it back. Just wanted to give him something working.
    I don't understand your statement, "is there a way I can add the container to the class even though it extends JPanel and not JFrame?" Please clarify.

  7. #7
    ttown is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    I kind of solved it.

    I had gone about solving this the wrong way. I had extended JFrame so that I could access my variables inside paintComponent, but I went away from that approach and just did it the way in the book.

    The last code quote is the working code. But I'm trying to read in an equation "x^2+4*x" and store it into StringFunction.

    Is there a way I can convert that string into a real mathematical function using a java function?

Similar Threads

  1. JFrame cannot paint?
    By pandaman0212 in forum AWT / Swing
    Replies: 7
    Last Post: 05-04-2013, 07:19 AM
  2. Replies: 2
    Last Post: 02-23-2011, 05:14 PM
  3. Java Paint Program problem (JPanel)
    By KilKidd in forum Advanced Java
    Replies: 6
    Last Post: 11-20-2010, 04:31 AM
  4. Class that extends JFrame help
    By javaman1 in forum New To Java
    Replies: 5
    Last Post: 11-10-2010, 02:29 AM
  5. Implements MyClass extends JFrame
    By coco in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 03:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •