Results 1 to 3 of 3
  1. #1
    Catfish1 is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default Simple drawing on a JPanel

    Hi
    I am having problems drawing onto a JPanel. All I need basically is to be able to draw a 9x9 grid onto a jpanel. My problem is I can't even get it to draw one line. I need to make this as an application so here is the basic code I'm trying to use
    Java Code:
    import java.awt.Graphics;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    
    public class SamplePan extends JFrame
    {
    	JPanel main;
    
    	public SamplePan()
    	{
    		super();
    		setSize(500,500);
    		main = new JPanel(new GridLayout(3,3));
    		add(main);
    	}
    
    	public void paintComponent(Graphics g)
    	{
                    super.paintComponent(g);
    		g.drawLine(100,100,300,300);
    	}
    
    
    public static void main(String[] args)
    {
    	SamplePan mySud = new SamplePan();
    	mySud.setVisible(true);
    }
    }
    The super.paintComponent(g) is commented out currently because if i uncomment it it gives me an error saying cannot find symbol, why I'm not sure. The way I see it, the error could be that it isn't drawing on the JPanel, but then I've removed the JPanel and it still doesn't draw. Or that it doesn't actually call the paintComponent() method. I've tried calling repaint() but it didn't change anything. I'm sorry for sounding obtuse here, I've never used graphics with Java before really
    Thanks

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,523
    Blog Entries
    7
    Rep Power
    20

    Default

    You're trying to draw on a JFrame or any of its superclasses; read the API documentation for the JFrame class and see if it can draw anything; it can't. You have to subclass the JPanel class, add it to your JFrame and draw on that JComponent.

    kind regards,

    Jos

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Java Code:
    		main = new JPanel(new GridLayout(3, 3)){
    			public void paintComponent(Graphics g) {
    				super.paintComponent(g);
    				g.drawLine(100, 100, 300, 300);
    			}
    		};

Similar Threads

  1. Drawing on Jpanel
    By nonabhai in forum AWT / Swing
    Replies: 0
    Last Post: 03-13-2010, 04:46 AM
  2. Drawing with JPanel
    By m00nchile in forum New To Java
    Replies: 2
    Last Post: 02-18-2010, 08:12 PM
  3. drawing in JPanel ,beyond the co-ordinates
    By anandnarayan891 in forum AWT / Swing
    Replies: 4
    Last Post: 04-05-2009, 03:42 AM
  4. drawing to a JPanel
    By diggitydoggz in forum New To Java
    Replies: 11
    Last Post: 03-09-2009, 07:42 AM
  5. Drawing points on a JPanel
    By josephdcoleman in forum New To Java
    Replies: 6
    Last Post: 02-25-2009, 03:47 PM

Tags for this Thread

Posting Permissions

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