Results 1 to 3 of 3
Thread: Simple drawing on a JPanel
- 10-04-2010, 03:00 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
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
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 reallyJava 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); } }
Thanks
- 10-04-2010, 03:26 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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
- 10-04-2010, 03:59 PM #3
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Similar Threads
-
Drawing on Jpanel
By nonabhai in forum AWT / SwingReplies: 0Last Post: 03-13-2010, 04:46 AM -
Drawing with JPanel
By m00nchile in forum New To JavaReplies: 2Last Post: 02-18-2010, 08:12 PM -
drawing in JPanel ,beyond the co-ordinates
By anandnarayan891 in forum AWT / SwingReplies: 4Last Post: 04-05-2009, 03:42 AM -
drawing to a JPanel
By diggitydoggz in forum New To JavaReplies: 11Last Post: 03-09-2009, 07:42 AM -
Drawing points on a JPanel
By josephdcoleman in forum New To JavaReplies: 6Last Post: 02-25-2009, 03:47 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks