Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Default Unable to re-plot the graph!

    Java Code:
    package graphmap;
    
    import java.io.*;
    
    import lexer.*;
    import parser.*;
    
    import javax.swing.JFrame;
    
    import java.awt.Graphics;
    import java.awt.Color;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class JPanel00 extends GraphPaper {
    
        private boolean flag = false;
        private char c[];
        public static double x = -1080;
        private int x_origin = 640, y_origin = 320;
        private double x_scale = 1, y_scale = 100;
    
    
        public JPanel00() {
            initComponents();
    
            jButton2.addActionListener(
                    new ActionListener() {
    
                        public void actionPerformed(ActionEvent event) {
                            if (event.getSource() == jButton2) {
                               [COLOR="red"] c = new char[jTextArea1.getText().length()];
                                jTextArea1.getText().getChars(0, jTextArea1.getText().length(), c, 0);
                                flag = true;
                                repaint();[/COLOR]
                            }
                        }
                    });
    
        }
    
        @Override
        public void paintComponent(final Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.BLACK);
    
            /*X and Y axes are plotted with origin centered at the coordinates specified by the user*/
    
            g.drawLine(x_origin, 0, x_origin, 770);
            g.drawLine(0, y_origin, 1280, y_origin);
    
    [COLOR="SeaGreen"]
    if (flag == true) {
                try {
                    double y = 0;
                    double x_lookback = x, y_lookback = new Parser(new Lexer(new CharArrayReader(c))).E1();
    
                    for (x += 0.05; x <= 1080; x += 0.05) {
    
                        y = new Parser(new Lexer(new CharArrayReader(c))).E1();
    
                        /*Geometric Transformations :: Reflection, Translation, and Scaling are done at single place*/
                        g.drawLine((int) (x_scale * x_lookback) + x_origin, (int) (-y_scale * y_lookback) + y_origin, (int) (x_scale * x) + x_origin, (int) (-y_scale * y) + y_origin);
    
                        x_lookback = x;
                        y_lookback = y;
                    }
                } catch (IOException io) {
                    io.printStackTrace();
                }
            }[/COLOR]
        }
             
    
        public static void main(String args[]) throws IOException {
            JFrame frame = new JFrame("Two-Dimensional Graph Sketcher");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new JPanel00());
            frame.setSize(1280, 770);
            frame.setVisible(true);
        }
    }


    the code in red is of interest..

    I am actually working on my final year project-"Two-Dimensional Graph Sketcher".

    Here the problem is that for the first time the paintComponent() method is called the global variable "flag" is set to false so the code in green is never run unless the button is pressed the first time. when the button is pressed the corresponding actionListener sets "flag" to true and the paintComponent() method is called. this time the code in green is run. ok . thats f9. and the graph is plotted.

    but the problem is that when I press the button second time instead of replotting the graph the already plotted graph disappears..
    plz help
    thnx for the reply.

    [If you didn't understand the problem plz let me know.]

  2. #2
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Default

    Obviously this is not the complete code .. I cut it short so that it is easily understood.

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

    Default

    Suggestions to help us help you:

    1) Simplify your code so we can see the problem but don't have to wade through miles of unrelated code.
    2) Part of this simplification includes getting rid of any NetBeans-generated code.
    3) Make sure that your posted code is compilable and runnable by us and has no external dependencies that will mess us up. For instance your class above extends a class that we have no access to, and this will prevent us from being able to run your code and see your problem.
    Please see this link for more details: Short, Self Contained, Correct Example

    Much luck!

  4. #4
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Default

    shall I post the complete project ..If you say.??

  5. #5
    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 Y. Progammer View Post
    Obviously this is not the complete code .. I cut it short so that it is easily understood.
    See the SSCCE link to show you how to do this in the best way.

    In addition to my recommendations above: this code will mess us up:
    Java Code:
            double x_lookback = x, y_lookback = new Parser(new Lexer(
                new CharArrayReader(c))).E1();
    
            for (x += 0.05; x <= 1080; x += 0.05) {
              y = new Parser(new Lexer(new CharArrayReader(c))).E1();
    Your example program should provide data points but in a simpler fashion if possible. Also, in your main program you may not want to be doing parsing and reading inside of a paintComponent method as it could possibly slow your painting down to a painfully slow rate.

  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 Y. Progammer View Post
    shall I post the complete project ..If you say.??
    HECK NO! Please read the link!

  7. #7
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Default

    this plots the graph for the very first time I press the button. But the problem is that when I press the button second time the graph is not replotted i.e.,

    suppose the input is :: y=x ; the graph corresponding to it is plot when I press the submit button.

    but when i press the button second time the graph disappears !!
    I need to restart the application.
    How can I get rid of this problem without bringing significant modifications at the fundamental level.

  8. #8
    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 Y. Progammer View Post
    this plots the graph for the very first time I press the button. But the problem is that when I press the button second time the graph is not replotted i.e.,

    suppose the input is :: y=x ; the graph corresponding to it is plot when I press the submit button.

    but when i press the button second time the graph disappears !!
    I need to restart the application.
    How can I get rid of this problem without bringing significant modifications at the fundamental level.
    Again, please create an SSCCE so we can run your program and see your error. It should have a JPanel for drawing a simpler graph and a button for displaying this, and that's about it.

    Also, put some debug statements in your code. For instance, putting this in your paintComponent method may be revealing:

    Java Code:
        // !! if (flag == true) { // *** don't do if (xxx == true)
        if (flag) {
          System.out.println("x: " + x);
    Why is x a large negative value when you first press your button but then a large positive for all subsequent presses? Interesting, no?

    So if x is incremented in this for loop:
    Java Code:
          for (x += 0.05; x <= 1080; x += 0.05) {
    but then never re-initialized afterwords, it will always be > 1080, and the for loop will never loop.
    Last edited by Fubarable; 02-20-2010 at 10:16 PM.

  9. #9
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Default ok. I post the SSCCE.Here it is.

    Code1:
    Java Code:
    [COLOR="Red"]package lexer;
    
    public class Tag {
    
        public final static int  X = 268;
    }[/COLOR]
    Code2:
    Java Code:
    [COLOR="red"]package lexer;
    
    public class Token {
    
        public int tag;
        public String lexeme = "";
    
        public Token(String s, int k) {
            tag = k;
            lexeme = s;
        }
    }[/COLOR]
    Code3:
    Java Code:
    [COLOR="red"]
    package lexer;
    
    import java.io.*;
    import java.util.*;
    
    public class Lexer {
    
        char peek = ' ';
        Hashtable words = new Hashtable();
    
        void reserve(Token w) {
            words.put(w.lexeme, w);
        }
        public CharArrayReader input;
    
        public Lexer(CharArrayReader m) {
    
            reserve(new Token("x", Tag.X));
            input = m;
        }
    
        void readch() throws IOException {
            peek = (char) input.read();
        }
    
        public Token scan() throws IOException {
            for (;; readch()) {
                if (peek == ' ' || peek == '\t' || peek == '\n') {
                    continue;
                } else {
                    break;
                }
            }
                StringBuffer b = new StringBuffer();
                do {
                    b.append(peek);
                    readch();
                } while (Character.isLetter(peek));
                String s = b.toString();
                Token w = (Token) words.get(s);
                if (w != null) {
                    return w;
                } else {
                    throw new Error("Syntax Error");
                }
            
        }
    }[/COLOR]
    Code4:
    Java Code:
    [COLOR="SeaGreen"]package parser;
    
    import lexer.*;
    import graphmap.*;
    
    import java.io.*;
    
    public class Parser {
    
        private Lexer lex;
        private Token look;
    
        public Parser(Lexer l) throws IOException {
            lex = l;
            move();
        }
    
        private void move() throws IOException {
            look = lex.scan();
        }
    
        public double E1() throws IOException {
            double x = X();
                return x;
        }
        private double X() throws IOException {
            double x;
            switch (look.tag) {
                case Tag.X:
                    return JPanel00.x;
                default:
                    throw new Error("Syntax Error");
            }
        }
    }[/COLOR]
    Code5:
    Java Code:
    [COLOR="RoyalBlue"]package graphmap;
    
    import java.awt.Graphics;
    import java.awt.Color;
    
    public class GraphPaper extends javax.swing.JPanel {
        
        @Override
        public void paintComponent(Graphics g)
        {        
            super.paintComponent(g);
            this.setBackground(Color.WHITE);
            g.setColor(Color.RED); 
            int count=0;
            while(count<3)
            {
                for(int i=1;i<=2;i++)
                {
                    for(int x=0;x<=1280;x=count==0?x+4:x+(4*(count*5) ))
                    {
                        if(i==1) g.drawLine(x,0,x,770);
                        else g.drawLine(0, x, 1280, x);
                    }
                }            
                if(count==1) g.setColor(Color.GRAY);
                else g.setColor(Color.LIGHT_GRAY);
                count++;
            }        
        }
    }
    [/COLOR]
    Code6:
    Java Code:
    [COLOR="rgb(65, 105, 225)"]package graphmap;
    
    import java.io.*;
    
    import lexer.*;
    import parser.*;
    
    import javax.swing.JFrame;
    
    import java.awt.Graphics;
    import java.awt.Color;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class JPanel00 extends GraphPaper {
    
        private boolean flag = false;
        private char c[];
        public static double x = -1080;
        private int x_origin = 640, y_origin = 320;
        private double x_scale = 100, y_scale = 100;
    
        public JPanel00() {
            initComponents();
           
            jButton2.addActionListener(
                    new ActionListener() {
    
                        public void actionPerformed(ActionEvent event) {
                            if (event.getSource() == jButton2) {
                                c = new char[jTextArea1.getText().length()];
                                jTextArea1.getText().getChars(0, jTextArea1.getText().length(), c, 0);
                                flag = true;
                                repaint();
                            }
                        }
                    });     
        }
    
        @Override
        public void paintComponent(final Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.BLACK);
            /*X and Y axes are plotted with origin centered at the coordinates specified by the user*/
    
            g.drawLine(x_origin, 0, x_origin, 770);
            g.drawLine(0, y_origin, 1280, y_origin);
    
            if (flag == true) {
                try {
                    double y = 0;
                    double x_lookback = x, y_lookback = new Parser(new Lexer(new CharArrayReader(c))).E1();
    
                    for (x += 0.05; x <= 1080; x += 0.05) {
    
                        y = new Parser(new Lexer(new CharArrayReader(c))).E1();
    
                        /*Geometric Transformations :: Reflection, Translation, and Scaling are done at single place*/
                        g.drawLine((int) (x_scale * x_lookback) + x_origin, (int) (-y_scale * y_lookback) + y_origin, (int) (x_scale * x) + x_origin, (int) (-y_scale * y) + y_origin);
    
                        x_lookback = x;
                        y_lookback = y;
                    }
                } catch (IOException io) {
                    io.printStackTrace();
                }
            }
        }
    
        private void initComponents() {
    
            jPanel2 = new javax.swing.JPanel();
            jButton2 = new javax.swing.JButton();
            jScrollPane6 = new javax.swing.JScrollPane();
            jTextArea1 = new javax.swing.JTextArea();
    
            setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)));
    
            jPanel2.setBackground(new java.awt.Color(255, 255, 255));
            jPanel2.setBorder(new javax.swing.border.MatteBorder(null));
    
            jButton2.setText("Sketch Graph");
    
            jTextArea1.setColumns(20);
            jTextArea1.setRows(5);
            jScrollPane6.setViewportView(jTextArea1);
    
            javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
            jPanel2.setLayout(jPanel2Layout);
            jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 429, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jButton2)
                    .addContainerGap(818, Short.MAX_VALUE))
            );
            jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGap(20, 20, 20)
                            .addComponent(jButton2)))
                    .addContainerGap())
            );
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(249, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            );
        }
        private javax.swing.JButton jButton2;
        private javax.swing.JPanel jPanel2;
        private javax.swing.JScrollPane jScrollPane6;
        private javax.swing.JTextArea jTextArea1;
    
        public static void main(String args[]) throws IOException {
            JFrame frame = new JFrame("Two-Dimensional Graph Sketcher");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new JPanel00());
            frame.setSize(1280, 770);
            frame.setVisible(true);
        }
    }
    
    [/COLOR]


    Codes in the same color belong to the same pakage. and problem is above as stated ie., for the first time the graph is plotted when i press the button but the very next time i press the button it disappears instead of replotting it!:rolleyes:

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

    Default

    ok. I post the SSCCE.Here it is.
    This only shows that you never read my link as it is most emphatically not an SSCCE. Please read the link (and my other posts above which were done by my creating the farkin' SSCCE).

  11. #11
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Default

    the problem is only in the CODE6. I posted the files so that you can execute the application and atleast have the look at the problem.. I am already really really almost frustrated.. so please don't ask to do read things that currently don't help me. I would surely go through what you asked me to. but presently i need your help. plz run the application and help me if you can .. thanx

  12. #12
    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 Y. Progammer View Post
    the problem is only in the CODE6. I posted the files so that you can execute the application and atleast have the look at the problem.. I am already really really almost frustrated.. so please don't ask to do read things that currently don't help me.
    OK, this is getting really really frustrating. How is my asking you for something that will help me and others to help you, "not helping you"? It seems obvious at least to me, that if you make it very easy for others to read, understand and run your code, that they will have an easier time finding your error and correcting it. How is this not obvious to you? What am I missing here?
    Last edited by Fubarable; 02-20-2010 at 10:59 PM.

  13. #13
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Default

    I think what I posted is really really simple to read and to debug. I only need some experienced person who has encountered such a problem before because I don't find only logical error in my coding .. I can't actually find what is really getting wrong ! perhaps need to flush the stream class and why ... or something else !
    Last edited by Y. Progammer; 02-20-2010 at 11:06 PM.

  14. #14
    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 Y. Progammer View Post
    thanx for not wasting my time anymore.
    While I'm sure it felt good to type and post this, and I suppose that it had the effect on me that you desired, have you considered what affect this comment will have on others who are reading this thread? Do you think that will it induce them to help you where I obviously didn't?

    I do sincerely wish you much luck with your project.
    Last edited by Fubarable; 02-20-2010 at 11:04 PM.

  15. #15
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    @OP: If there's any way you can edit out your last post, I suggest you do.

    There are a lot of people who can help and are willing to help. But you are making a mistake if you imagine that because they do this for no financial reward that they have no expectations and make no demands. There are expectations of anyone posting a question: expectations about their attitude.

    If your post was just an expression of frustration, delete it. Because it has to be added to the list of things that are presently not helping you.

  16. #16
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    Hmm, I don't see why the OP is so mad, fubarable gave him the answer.

  17. #17
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Default

    I really consider your people help and I corrected my mistake and I appologise for the same.

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

    Default

    Apology accepted. You know that I've spent about an hour and a half pouring over your code. Have you looked at all the replies above? Have you addressed that funky for loop and non-re-initialized for loop that I mentioned in comments way up in this thread? The for loop also has a skanky looking initial condition. Also, your parsing and analyzing (again) should not go in paintComponent.

  19. #19
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Default

    thnx for accepting my apolopy and I am sorry If I hurt you !

  20. #20
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Default

    can you please suggest where should I place the parsing and analyzing within my application.

Page 1 of 2 12 LastLast

Similar Threads

  1. How to plot graph in java for given samples
    By annesteve31 in forum New To Java
    Replies: 27
    Last Post: 11-29-2009, 10:27 PM
  2. Replies: 7
    Last Post: 11-16-2009, 02:22 AM
  3. Plot 2D graph in Java from RS-232 data
    By spratana in forum Java 2D
    Replies: 4
    Last Post: 02-11-2009, 07:49 PM
  4. How to plot a dot graph
    By Manfizy in forum Java 2D
    Replies: 3
    Last Post: 01-28-2009, 03:57 PM
  5. Need Help for Dot Plot Graph
    By BHCluster in forum Java 2D
    Replies: 5
    Last Post: 04-15-2008, 03:54 PM

Posting Permissions

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