Results 1 to 5 of 5
- 08-07-2008, 05:30 PM #1
Member
- Join Date
- Aug 2008
- Posts
- 16
- Rep Power
- 0
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
-
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.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.
Later when you have a decent grounding in Swing, Graphics and GUI, then it would be OK to use the NetBeans generated code.
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.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?
- 08-09-2008, 03:55 AM #3
Member
- Join Date
- Aug 2008
- Posts
- 16
- Rep Power
- 0
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
- 08-28-2009, 05:59 PM #4
Member
- Join Date
- Aug 2009
- Posts
- 2
- Rep Power
- 0
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 :(
- 08-28-2009, 06:50 PM #5
Instead of:
You should override the paintComponent(...) of a custom JPanel:Java Code:viewer.getGraphics()
Java 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.Last edited by mrmatt1111; 08-28-2009 at 06:58 PM.
My Hobby Project: LegacyClone
Similar Threads
-
.add to a JPanel
By harrier in forum NetBeansReplies: 11Last Post: 07-13-2010, 10:24 AM -
java api for reading adobe frame maker files
By venkatmatcha in forum Advanced JavaReplies: 2Last Post: 06-30-2008, 06:00 AM -
refresh JPanel
By olesja in forum AWT / SwingReplies: 1Last Post: 04-16-2008, 03:58 PM -
JPanel Problems
By Riftwalker in forum AWT / SwingReplies: 6Last Post: 10-15-2007, 11:16 PM -
Draw on JPanel, Help
By carl in forum Java 2DReplies: 1Last Post: 07-31-2007, 06:56 AM


LinkBack URL
About LinkBacks

Bookmarks