Results 1 to 18 of 18
  1. #1
    halo3 is offline Member
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default Drawing Graph and Add to JPanel

    Hi. I am coding for a simple Graphic Calculator.
    As you can see in the picture (img143.imageshack.us/img143/3800/jpanel.png), a graph is supposed to be shown in that orange box (mainPanel).
    Since I had coded a Draw.class, I created an object and add to mainPanel
    Java Code:
    Draw newGraph=new Draw()
    mainPanel.add(newGraph)
    However, nothing is shown in mainPanel. What did I get wrong?
    Thank you.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Hard to tell without seeing your code.

  3. #3
    halo3 is offline Member
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default

    Thanks for reply.
    My codes for Draw.class
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package DrawPanel;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    /**
     *
     * @author NguyenDuyLong
     */
     public class Draw extends JPanel{
            public double x1,x2;
            public int xCenter;
            public int yCenter;
        public Draw(){
            super();
            setVisible(true);
            setBackground(Color.blue);
            setForeground(Color.red);
        }
        @Override
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            xCenter=(int)1024/2;
            yCenter=700;
            
            //xCenter=(int)Size.getWidth()/2;
            //yCenter=(int)Size.getHeight()/2;
            g.drawLine(0,yCenter,xCenter*2,yCenter);
            g.drawLine(xCenter,0,xCenter,yCenter*2);
            //g.drawOval(500,200,200,500);
        }
    }
    The picture shows how I coded for Panel:
    img98.imageshack.us/img98/8112/panel.png

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    If I add your panel to a frame I do see something but only after maximizing frame.
    You should consider using sizes relative to the the size of the frame in your drawing.

  5. #5
    halo3 is offline Member
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default

    I have been aware of that. However my main point is that the Panel does not seem to work. Even if I change the value of xCenter and yCenter, Panel does not show anything.

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Perhaps show us how you are displaying it then. Are you adding it to a frame?
    I certainly see the blue background and the x-y axis.

  7. #7
    halo3 is offline Member
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default

    Please check this link:
    img196.imageshack.us/gal.php?g=95376863.png

    I try both mainPanel and mainFrame but they show nothing.
    Please tell me what I get wrong.

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Where is your code where you test the panel? That is what I asked for above.


    example: here is my test for your panel
    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import java.awt.Color;
    import java.awt.Graphics;
    
    class Draw extends JPanel {
    	public double x1, x2;
    	public int xCenter;
    	public int yCenter;
    
    	public Draw() {
    		super();
    		setVisible(true);
    		setBackground(Color.blue);
    		setForeground(Color.red);
    	}
    
    	@Override
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		xCenter = 1024 / 2;
    		yCenter = 700;
    
    		// xCenter=(int)Size.getWidth()/2;
    		// yCenter=(int)Size.getHeight()/2;
    		g.drawLine(0, yCenter, xCenter * 2, yCenter);
    		g.drawLine(xCenter, 0, xCenter, yCenter * 2);
    		// g.drawOval(500,200,200,500);
    	}
    
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			@Override
    			public void run() {
    				initGUI();
    			}
    		});
    	}
    
    	public static void initGUI() {
    		JFrame f = new JFrame("Frame ");
    		f.setSize(300, 300);
    		f.add(new Draw());
    		f.setVisible(true);
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    }
    Now post your code which you say shows nothing.

  9. #9
    halo3 is offline Member
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default

    Ooops. I think I misunderstand your point. What I mean is that the codes
    Java Code:
    Draw Graph=new Draw() Panel.add(Graph)
    do not show anything in the NetBean project that I am trying to do.

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Yeah but those panels don't display on their own you know.

  11. #11
    halo3 is offline Member
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default

    I add mainPanel.setVisible(true) and mainFrame.setVisible(true) as well, but they still do not work. Otherwise, I do not know what to do.

  12. #12
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Since it seems you are never going to post your code, read the code I posted above and compare it to yours then.
    P.S If you are new to Java you should close Netbeans, get Textpad and go through Sun's tutorial testing the codes on Textpad.

  13. #13
    halo3 is offline Member
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default

    I think that I did not make my point clear.
    The following is my code to check the "Draw.class"
    Java Code:
    
    package desktopapplication2;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    /**
     *
     * @author NguyenDuyLong
     */
     public class Draw extends JPanel{
            public double x1,x2;
            public int xCenter;
            public int yCenter;
        public Draw(){
            super();
            setVisible(true);
            setBackground(Color.blue);
            setForeground(Color.red);
        }
        @Override
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            xCenter=(int)200;
            yCenter=300;
            
            //xCenter=(int)Size.getWidth()/2;
            //yCenter=(int)Size.getHeight()/2;
            g.drawLine(0,yCenter,xCenter*2,yCenter);
            g.drawLine(xCenter,0,xCenter,yCenter*2);
            //g.drawOval(500,200,200,500);
        }
    
    	public static void main(String[] args) {
    		JFrame f = new JFrame("Frame ");
    		f.setSize(300, 300);
    		f.add(new Draw());
    		f.setVisible(true);
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    
    }
    It works. I agree with you.

    I think I should make my point again: The Draw.class works fine. What troubles me is that when I try to plot such graph on a graphPanel in my application, it does not work. I am not interested in drawing one graph but many graphs. It is kind of Graphic Calculator which user can put in equations, click the button "Graph!" and gets the result on the graphPanel. "Draw.java" runs well but when I create a Draw object and add into graphPanel and hope for a similar result, it does not. So my application would not work.

    I hope that you would understand me now. If not, I have nothing else to say. It seems to me that I somehow make you angry. Sorry for that.

  14. #14
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Is there any possibility in us seeing the full runnable code where the Draw object is added onto another panel and then does not work?

  15. #15
    halo3 is offline Member
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default

    Basically, I just add a new JPanel newPanel to mainPanel(created by NetBean automatically). In newPanel, I choose "Customize Code" and include:
    Java Code:
    Draw newGraph= new Draw();
    newPanel.add(newGraph);
    newPanel.setVisible(true);
    The three java files are here:
    mediafire.com/download.php?hj3zjgy3kmz]DesktopApplication1.java
    mediafire.com/download.php?v1ozdzvatjz]DesktopApplication1AboutBox.java
    mediafire.com/download.php?mmnogzmhywd

  16. #16
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You are going to need frames to display the frames on.
    I suggest you read the Swing tutorial.

  17. #17
    gancio is offline Member
    Join Date
    Aug 2009
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by halo3 View Post
    Basically, I just add a new JPanel newPanel to mainPanel(created by NetBean automatically). In newPanel, I choose "Customize Code" and include:
    Java Code:
    Draw newGraph= new Draw();
    newPanel.add(newGraph);
    --here--
    newPanel.setVisible(true);
    The three java files are here:
    mediafire.com/download.php?hj3zjgy3kmz]DesktopApplication1.java
    mediafire.com/download.php?v1ozdzvatjz]DesktopApplication1AboutBox.java
    mediafire.com/download.php?mmnogzmhywd
    try to add pack(); --here--
    Last edited by gancio; 09-22-2009 at 01:40 PM.

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

Similar Threads

  1. Looking for help on drawing stuff in a jPanel
    By Gatts79 in forum AWT / Swing
    Replies: 3
    Last Post: 08-28-2009, 07:00 PM
  2. drawing in JPanel ,beyond the co-ordinates
    By anandnarayan891 in forum AWT / Swing
    Replies: 4
    Last Post: 04-05-2009, 04:42 AM
  3. drawing to a JPanel
    By diggitydoggz in forum New To Java
    Replies: 11
    Last Post: 03-09-2009, 08:42 AM
  4. Drawing points on a JPanel
    By josephdcoleman in forum New To Java
    Replies: 6
    Last Post: 02-25-2009, 04:47 PM
  5. how to draw x-y graph in Jpanel.--not in APPLET.
    By vincent2001@gmail.com in forum New To Java
    Replies: 2
    Last Post: 08-24-2008, 06:01 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
  •