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

    Unhappy Looking for help on drawing stuff in a jPanel

    Hello All -

    I wasn't sure where to post this, but this seems like the appropriate forum. So I'm using the NetBeans IDE and it's GUI application. I Drag and drop a panel into the frame and I want to draw something into it. So I looked on google and all the tutorials I find explain it in a code perspective extending the JPanel class to one where you can override the JPanel paint(Graphics e) function. I understand how the inheritence works and stuff, but I don't see how i can override the JPanel to paint what I want (ie text or graphics) if I drag and drop a JPanel into the Frame in NetBeans.

    I have tried writing a function like the following:

    JPanel viewer;

    public void DrawSomething() {
    String data = "Testing";
    Canvas.getGraphics().setColor(new Color(255,0,0));
    Canvas.getGraphics().setPaintMode();
    Canvas.getGraphics().fillRect(10, 20, 40, 10);
    Canvas.getGraphics().setColor(new Color(0,0,0));
    Canvas.getGraphics().setPaintMode();
    Canvas.getGraphics().drawString(data, 10, 10);
    }

    I called this method inside the main function and nothing happens. I then set a listener the canvas on mouseclick and called this function in the auto-generated listener action. On clicking of the JPanel it flickers and if you click a couple of times eventually the rectangle and text appears on the panel. The problem with this function is that the red color I set for the rectangle doesn't make it red. In C# you set a brush color for images, but I didn't see a similar method in java. Also why does it flicker instead of displaying on the screen the first time?

    Basically I'm hoping to get some direction on the proper way of drawing stuff on a JPanel or Canvas in NetBeans IDE using the NetBeans GUI application. I like being able to see what I'm doing visually as I set up the GUI. So...

    1- What am I doing wrong trying to draw to the JPanel
    2- Is there a way I can have the JPanel draw / update what I want w/o a listener? Or how do I have it update/ draw without some sort of keypress or mouse press?
    3- I've looked for tutorials on how to do things with the NetBeans SWING GUI application, but all I find is extending functionality of these base classes which I don't see how it helps if I am dragging and dropping things into the frame. If you have your own tutorial on how to do it just dragging and dropping and stuff it would be great (Especially drawing differnt things to multiple JPanels or Canvasses).

    Thanks In Advance

    Gatts79

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,410
    Rep Power
    25

    Default

    Not sure how to use your IDE.
    To create your own JPanel component, you extend JPanel and override the paintComponent() method and put your drawing code in it.
    There must be lots of code examples if you use Search for paint or paintComponent

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

    Default

    Thanks for the reply norm. But my problem is a bit diffrent than that. The main part of the problem is that I am dragging a NetBeans image of the JPanel onto the Form I'm making. I am using the NetBeans GUI to visually see and place the Swing components and when I do that it auto generates code that sets up a JPanel at the location i drag it to. Because of that I can't overload the paint command. I was looking for a way around this. I know I can extend the JPanel and write whatever I want it to do but then I lose being able to move it around in the visual interpretation that NetBeans provides.

    Like I said above if i get the panels graphic I can get to methods to make it draw things, but it flickers alot before it will even stay on screen and only when i have it set to a listener for input.

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

    Default

    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

Similar Threads

  1. Anyone selling SCJA training material? Your OLD stuff?
    By KMN in forum Reviews / Advertising
    Replies: 2
    Last Post: 08-24-2009, 05:58 PM
  2. Simple Stuff 0.1
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 07-19-2008, 04:27 PM
  3. Help with 2-D Drawing
    By Deathmonger in forum New To Java
    Replies: 4
    Last Post: 06-18-2008, 02:23 AM
  4. X&Y Coordinate Drawing on jPanel
    By BHCluster in forum Java 2D
    Replies: 2
    Last Post: 03-27-2008, 10:47 AM
  5. No Fluff Just Stuff Software Symposium Series 2007,
    By orchid in forum Reviews / Advertising
    Replies: 0
    Last Post: 04-08-2007, 08:13 PM

Posting Permissions

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