Results 1 to 5 of 5
  1. #1
    Gatts79 is offline Member
    Join Date
    Aug 2008
    Posts
    16
    Rep Power
    0

    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

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

    Default

    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.

    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.

  3. #3
    Gatts79 is offline Member
    Join Date
    Aug 2008
    Posts
    16
    Rep Power
    0

    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

  4. #4
    roadwolf is offline Member
    Join Date
    Aug 2009
    Posts
    2
    Rep Power
    0

    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 :(

  5. #5
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    Instead of:

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


    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

  1. .add to a JPanel
    By harrier in forum NetBeans
    Replies: 11
    Last Post: 07-13-2010, 10:24 AM
  2. java api for reading adobe frame maker files
    By venkatmatcha in forum Advanced Java
    Replies: 2
    Last Post: 06-30-2008, 06:00 AM
  3. refresh JPanel
    By olesja in forum AWT / Swing
    Replies: 1
    Last Post: 04-16-2008, 03:58 PM
  4. JPanel Problems
    By Riftwalker in forum AWT / Swing
    Replies: 6
    Last Post: 10-15-2007, 11:16 PM
  5. Draw on JPanel, Help
    By carl in forum Java 2D
    Replies: 1
    Last Post: 07-31-2007, 06:56 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
  •