CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2002
    Location
    Croatia
    Posts
    275

    Help with graphics

    Ok... I need help with something

    When I push the button, I need to draw graph of function f(x) = a* x*x + b*x + c in the new window. a, b & c are in the text area.
    The main problem is how to connect two classes... In one class I have variables a, b & c and the button, and in other class I have program which draws the graph, but I can't get the variables a,b,c from the first class to the second, therefore, the graph is always f(x) = 0 * x*x + 0*x + 0

    I'm using BlueJ

    I hope you understand what I'm saying. I'd appreciate any help

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Help with graphics

    Can you show your code (inside code tags please).

  3. #3
    Join Date
    Mar 2002
    Location
    Croatia
    Posts
    275

    Re: Help with graphics

    Perhaps there are some mistakes but it isn't important, I can fix it myself later. I only don't know how:
    1. Send variables a, b, c from the first class to the second class
    2. Create a command on the button bt that will draw the graph from the second class in the new window

    Oh, and I said that i need graph a*x*x + b*x + c, and in the program is a* sin(b*x + c), but nevermind that


    Code:
     import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    
    
    
    public class mmm extends JFrame implements ActionListener
    {
        private final int length = 600;
        private final int height = 400;
        private JTextArea ta;
        private JButton bt;
        private Container c1;
        
       public mmm()
        {
            setSize (length, height);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setTitle("Test");
                    
            c1 = getContentPane();
            c1.setLayout(null);
           
            
            ta = new JTextArea();
            ta.setSize(sirina - 100, visina - 100);
            ta.setLocation(1,1);
            c1.add(ta); 
            
            bt = new JButton();
            bt.setLocation(sirina - 80, 50);
            bt.setSize(60, 20);
            bt.addActionListener(this);
            bt.setText("U redu");
            c1.add(bt);
                   
        
            setVisible(true);
        }
        
        
        
        public void get_a_b_c()
        {
            String tmp = ta.getText();
            String a1 = "";
            String b1 = "";
            String c1 = "";
            String g = "";
            
            boolean u1 = true;
            boolean u2 = true;
            boolean bo = true;
            for (int i = 0; i < tmp.length(); i++)
            {
                String z = tmp.substring(i, i+1);
                if (((u1 == true) && (u2 == true)) && ((z.equals("0")) || (z.equals("1")) || (z.equals("2")) || (z.equals("3")) || (z.equals("4")) || (z.equals("5")) || (z.equals("6")) || (z.equals("7")) || (z.equals("8")) || (z.equals("9")))) 
                    a1 = a1 + z;
                if ((z.equals(",")) && (u1 == false))
                    u2 = false;
                if ((z.equals(",")) && (u1 == true))
                    u1 = false;
                if (((u1 == false) && (u2 == true)) && ((z.equals("0")) || (z.equals("1")) || (z.equals("2")) || (z.equals("3")) || (z.equals("4")) || (z.equals("5")) || (z.equals("6")) || (z.equals("7")) || (z.equals("8")) || (z.equals("9"))))  
                    b1 = b1 + z;
                if (((u1 == false) && (u2 == false)) && ((z.equals("0")) || (z.equals("1")) || (z.equals("2")) || (z.equals("3")) || (z.equals("4")) || (z.equals("5")) || (z.equals("6")) || (z.equals("7")) || (z.equals("8")) || (z.equals("9"))))  
                    c1 = c1 + z;
                bo = ((z.equals(",")) || (z.equals("0")) || (z.equals("1")) || (z.equals("2")) || (z.equals("3")) || (z.equals("4")) || (z.equals("5")) || (z.equals("6")) || (z.equals("7")) || (z.equals("8")) || (z.equals("9")));
                if (bo == false)
                    g = "greska!!";
            }
            if (g.equals("greska!!"))
            {
                JOptionPane.showMessageDialog (this, "Error!", "Error", JOptionPane.ERROR_MESSAGE);
            }
            
            int a = Integer.parseInt(a1);
            int b = Integer.parseInt(b1);
            int c = Integer.parseInt(c1); 
        
            
        }
        
        
        
                    
        public void actionPerformed(ActionEvent e)
        {
    
            
            if (e.getSource().equals(bt))
          
            // Here should be the command that draws graph
            
      }
        
        public static void main(String[] s)
        {
            mmm m = new mmm();
        }
        
    }
    Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class test2 extends JFrame
    {
        private final int length = 640, height = 480;
     
        public test2()
        {
            
            setTitle ("Graph of function f (x) = " + a + " sin (" + b + " x + " + c + ")");
            
            setSize (length, height);
            setDefaultCloseOperation (EXIT_ON_CLOSE);
            setBackground (Color.white);
            setVisible (true);
        }
        private void xos (Graphics g)
        {
            g.setColor (Color.blue);
            g.drawLine (0, height / 2, length, height / 2);
            for (int i = 1; i < length/ 20; i++)
            g.drawLine (i * 20, height / 2 - 2, i * 20, height / 2 + 2);
        }
        private void yos (Graphics g)
        {
            g.setColor (Color.blue);
            g.drawLine (length / 2, 0, length / 2, height);
            for (int i = 1; i < height / 20; i++)
            g.drawLine (length / 2 - 2, i * 20, length / 2 + 2, i * 20);
        }
        public void crtaj (Graphics g)
        {
            double x = - length / 40.0, xs = length / 2.0, ys = height / 2.0, y;
            int xp, yp;
            g.setColor (Color.red);
            while (x < length / 40)
            {
                y = a * x*x + b * x + c;
                xp = (int)(xs + 20 * x);
                yp = (int)(ys - 20 * y);
                g.drawOval (xp, yp, 1, 1);
                x += 0.05;
            }
        }
        public void paint (Graphics g)
        {
            xos (g);
            yos (g);
            crtaj (g);
        }
        
        public static void main (String[] s)
        {
            test2 t2 = new test2 (3,3,3);
        }   
    }

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Help with graphics

    1. Send variables a, b, c from the first class to the second class
    For 2 objects to communicate with each other at least one of them must hold a reference to the other. So your mmm class's (terrible name by the way) main method should construct both objects and pass the test2 (another bad name) object's reference to mmm's constructor. Now that the mmm object has a reference to the test2 object it can then call methods on the test2 object to set the a, b & c values and to draw the graph.

  5. #5
    Join Date
    Mar 2002
    Location
    Croatia
    Posts
    275

    Re: Help with graphics

    Thanks Keang.

    I'm new to Java. My problem is to draw sometning in a new window, so I had an idea to use a new class for drawing.

    Now I have tried a new approach.
    I create a new JFrame and I want to draw to a new frame.

    A PROBLEM:
    paint() mehtod immediatelly draws in my main Frame, and hides a text area and a button.
    If you try an example, and click in upper right area of the Frame, a command will appear which openes a new Frame, and draws into it.

    HOW to disable paint() command for my main window, and to paint only to the new Frame?

    Ergas


    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class graph extends JFrame implements ActionListener
    {
        private final int length = 640;
        private final int height = 480;
        private JTextArea ta;
        private JButton bt;
        private Container c1, c2;
        static JFrame frame;
        private Graphics g;
        
        public int a = 0;
        public int b = 0;
        public int c = 0;
    
        public graph()
        {
            frame = new JFrame("FrameDemo");
             frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
            frame.setSize(length, height);
            //frame.setVisible(true);
            frame.setBackground(Color.white);
            
            c2 = frame.getContentPane();
            c2.setLayout(null);
            g = frame.getGraphics();
           
            setSize (length, height);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setTitle("Test");
                    
            c1 = getContentPane();
            c1.setLayout(null);
                      
            
            ta = new JTextArea();
            ta.setSize(length - 100, height - 100);
            ta.setLocation(1,1);
            c1.add(ta); 
            
            bt = new JButton();
            bt.setLocation(length - 80, 50);
            bt.setSize(60, 20);
            bt.addActionListener(this);
            bt.setText("ok");
            c1.add(bt);
                    
            setVisible(true);
    
        }
       
        public void get_a_b_c()
        {
            String tmp = ta.getText();
            String a1 = "";
            String b1 = "";
            String c1 = "";
            String g = "";
            boolean u1 = true;
            boolean u2 = true;
            boolean bo = true;
            
            for (int i = 0; i < tmp.length(); i++)
            {
                String z = tmp.substring(i, i+1);
                if (((u1 == true) && (u2 == true)) && ((z.equals("0")) || (z.equals("1")) || (z.equals("2")) || (z.equals("3")) || (z.equals("4")) || (z.equals("5")) || (z.equals("6")) || (z.equals("7")) || (z.equals("8")) || (z.equals("9")))) 
                    a1 = a1 + z;
                if ((z.equals(",")) && (u1 == false))
                    u2 = false;
                if ((z.equals(",")) && (u1 == true))
                    u1 = false;
                if (((u1 == false) && (u2 == true)) && ((z.equals("0")) || (z.equals("1")) || (z.equals("2")) || (z.equals("3")) || (z.equals("4")) || (z.equals("5")) || (z.equals("6")) || (z.equals("7")) || (z.equals("8")) || (z.equals("9"))))  
                    b1 = b1 + z;
                if (((u1 == false) && (u2 == false)) && ((z.equals("0")) || (z.equals("1")) || (z.equals("2")) || (z.equals("3")) || (z.equals("4")) || (z.equals("5")) || (z.equals("6")) || (z.equals("7")) || (z.equals("8")) || (z.equals("9"))))  
                    c1 = c1 + z;
                bo = ((z.equals(",")) || (z.equals("0")) || (z.equals("1")) || (z.equals("2")) || (z.equals("3")) || (z.equals("4")) || (z.equals("5")) || (z.equals("6")) || (z.equals("7")) || (z.equals("8")) || (z.equals("9")));
                if (bo == false)
                    g = "error!!";
            }
            
            if (g.equals("error!!"))
            {
                JOptionPane.showMessageDialog (this, "Error!", "Error", JOptionPane.ERROR_MESSAGE);
            }  
            
            a = Integer.parseInt(a1);
            b = Integer.parseInt(b1);
            c = Integer.parseInt(c1);
        }
        
        private void xos (Graphics g)
        {
            g.setColor (Color.blue);
            g.drawLine (0, height / 2, length, height / 2);
            for (int i = 1; i < length / 20; i++)
            g.drawLine (i * 20, height / 2 - 2, i * 20, height / 2 + 2);
        }
      
      private void yos (Graphics g)
        {
            g.setColor (Color.blue);
            g.drawLine (length / 2, 0, length / 2, height);
            for (int i = 1; i < height / 20; i++)
            g.drawLine (length / 2 - 2, i * 20, length / 2 + 2, i * 20);
        }
       
        public void draw (Graphics g)
        {
            double x = - length / 40.0, xs = length / 2.0, ys = height / 2.0, y;
            int xp, yp;
            g.setColor (Color.red);
            while (x < length / 40)
            {
                y = a * x*x + b * x + c;
                xp = (int)(xs + 20 * x);
                yp = (int)(ys - 20 * y);
                g.drawOval (xp, yp, 1, 1);
                x += 0.05;
            }
        }
       
          public void paint (Graphics g)
        {   
                xos (g);
                yos (g);
                draw (g);   
        }
            
        public void actionPerformed(ActionEvent e)
        {
            if (e.getSource().equals(bt))
            {
                g = frame.getGraphics();
                frame.setVisible(true); 
                paint(g);
            }
                
      }
        
        public static void main(String[] s)
        {
            graph ph = new graph();
               
        } 
    }

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Help with graphics

    I'm new to Java. My problem is to draw sometning in a new window, so I had an idea to use a new class for drawing.
    Now I have tried a new approach.
    I create a new JFrame and I want to draw to a new frame.
    Randomly trying different ways of solving the problem is no substitute for learning how to do it correctly before trying to write the code. It also makes trying to help you extremely frustrating as by the time one of us has answered your post you no doubt will already trying out a different approach.
    paint() mehtod immediatelly draws in my main Frame, and hides a text area and a button.
    First if all you shouldn't be overriding the paint() method, for swing components you should override the paintComponent() method. And secondly you shouldn't be overriding any paint() or paintXXX() method on JFrame. If you want to draw anything create a class that extends JPanel and draw in that.

    Your use of components is very confused I suggest you read a swing tutorial before attempting any more code

  7. #7
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Help with graphics

    Keang is absolutely right - trial and error will get you totally confused if you don't know how to use Swing to draw on components. It may seem like a tedious waste of time to go through the tutorials, but it's by far the quickest way to find out how to do it. Using Swing is not very intuitive at first, but once you get to know how to use it (from the tutorial), you'll find it fairly easy. If you don't understand anything, just ask.

    There does not now, nor will there ever exist, a programming language in which it is the least bit hard to write bad programs...
    L. Flon.
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured