Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-07-2008, 06:30 PM
Member
 
Join Date: Aug 2008
Posts: 16
Rep Power: 0
Gatts79 is on a distinguished road
Unhappy How to draw in a JPanel using Netbeans 6.1 GUI maker
Hello Everyone -

So I'm pretty new to the java language, but I've been coding in C/C++ and C# for awhile now. I know C# is basically a rip-off clone of java so it hasn't been to difficult to pick things up. The problem I am having is drawing 2D objects in a JPanel. I've read the differnt tutorials online about drawing stuff, basically extending from the JPanel and overwriting the paint() function, however when I use NetBeans to add in JPanels to the project it is just a JPanel and I don't see how I can overwritte the paint function.

With a little trial and error I've basically done something like this:

jPanel viewer;

public void SomeFunction() {
viewer.getGraphics().setColor(new Color(255, 0, 0));
viewer.getGraphics().fillRect(10, 0, 20, 10);
viewer.getGraphics().setColor(new Color(0, 0, 0));
viewer.getGraphics().drawString("Test", 10, 20);
}

Yes I do have the right imports included to do what I need. So I tried 2 things. I called this function in the main function and nothing happens. I then tried attaching a listener to the canvas on mouse click and I do see a flicker and after a couple of clicks it stays on the panel. The thing is the color for the rectangle is black and not red.

I'm pretty sure I'm doing things totally wrong. If anyone has used NetBeans GUI stuff I would love tutorials on doing it right and information on what I'm doing wrong.

1- Basically is it possible to overwrite the paint function for the JPanel through the NetBeans GUI application (I'm just dragging the panel into the
frame).
2- How do i get it so the canvas is always drawing so I don't have to do a listener on the click.
3- All the tutorials I've seen basically has you extending the JPanel and overwritting the paint function. Can I specifiy what the body of the paint function for the JPanel I dragged into the Frame without extending a new class?

I apologize if my wording is a bit wierd and confusing; I'm not sure myself if I am asking the write questions.

Thanks In Advance

Gatts79
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 08-09-2008, 04:03 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,396
Rep Power: 8
Fubarable is on a distinguished road
Default
Quote:
So I'm pretty new to the java language, but I've been coding in C/C++ and C# for awhile now. I know C# is basically a rip-off clone of java so it hasn't been to difficult to pick things up. The problem I am having is drawing 2D objects in a JPanel. I've read the differnt tutorials online about drawing stuff, basically extending from the JPanel and overwriting the paint() function, however when I use NetBeans to add in JPanels to the project it is just a JPanel and I don't see how I can overwritte the paint function.
If you are just learning GUI, you'll be far better off ditching the NetBeans generated code and learning to do Swing by hand. From my experience with posters here and elsewhere, those who start to learn GUI with NetBeans stay far behind those who dont, that it actually hinders your Swing education. Else you'll probably be flailing from one confusion and problem to another. The Sun site has some decent tutorials on how to do graphics in Swing and with Graphics2D. There you'll learn to override the JPanel's paintComponent method, to not use getGraphics without a darn good reason (you don't have one here), etc.

Later when you have a decent grounding in Swing, Graphics and GUI, then it would be OK to use the NetBeans generated code.

Quote:
1- Basically is it possible to overwrite the paint function for the JPanel through the NetBeans GUI application (I'm just dragging the panel into the
frame).
2- How do i get it so the canvas is always drawing so I don't have to do a listener on the click.
3- All the tutorials I've seen basically has you extending the JPanel and overwritting the paint function. Can I specifiy what the body of the paint function for the JPanel I dragged into the Frame without extending a new class?
The Sun graphics tutorials will answer most of these questions for you. Again, try to do it without the auto-generate code. If you do, you'll thank me later, promise.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-09-2008, 04:55 AM
Member
 
Join Date: Aug 2008
Posts: 16
Rep Power: 0
Gatts79 is on a distinguished road
Default
Thanks for the advice... that was the conclusion I was getting at near the end here. I did find places to customize the code myself and for the most part I am not writting the code myself and just use the form as a visual aid.

Yeah I've been heavily hitting the java tutorials they are quit useful, I just wish there were some more design patterns for interaction with multiple panels and frames.

Thanks Again
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-28-2009, 06:59 PM
Member
 
Join Date: Aug 2009
Posts: 2
Rep Power: 0
roadwolf is on a distinguished road
Default solution
Hello Gatts79,

It looks like I can't post links but google: World's Fastest Java Netbeans 5.5 Tutorial

Have a look it at this link. It has exactly what you want:

***
To be able to post links or images your post count must be 20 or greater. You currently have 0 posts.

Please remove links from your message, then you will be able to submit your post
***

It looks like I am not allowed to post links
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-28-2009, 07:50 PM
mrmatt1111's Avatar
Senior Member
 
Join Date: Aug 2009
Location: San Jose, CA, USA
Posts: 293
Rep Power: 1
mrmatt1111 is on a distinguished road
Default
Instead of:

Code:
viewer.getGraphics()
You should override the paintComponent(...) of a custom JPanel:


Code:
public void paintComponent( Graphics g )
{
   super.paintComponent(g); //draw background
   
   Graphics2D g2d = (Graphics2D)g;

   ...
}

You can then suggest to the panel to redraw itself with "repaint()"

You can force netbeans to use your custom JPanel by adding it to the Palette. After adding it the palette you can drag it onto your gui form.
__________________
My Hobby Project: LegacyClone

Last edited by mrmatt1111; 08-28-2009 at 07:58 PM.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
.add to a JPanel harrier NetBeans 10 07-25-2008 08:24 PM
java api for reading adobe frame maker files venkatmatcha Advanced Java 2 06-30-2008 07:00 AM
refresh JPanel olesja AWT / Swing 1 04-16-2008 04:58 PM
JPanel Problems Riftwalker AWT / Swing 6 10-16-2007 12:16 AM
Draw on JPanel, Help carl Java 2D 1 07-31-2007 07:56 AM


All times are GMT +2. The time now is 01:34 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org