Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-07-2008, 07:46 PM
Member
 
Join Date: Aug 2008
Posts: 11
Gatts79 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2008, 09:45 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Norm is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-07-2008, 11:32 PM
Member
 
Join Date: Aug 2008
Posts: 11
Gatts79 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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

vB 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
Simple Stuff 0.1 Java Tip Java Announcements 0 07-19-2008 06:27 PM
Help with 2-D Drawing Deathmonger New To Java 4 06-18-2008 04:23 AM
Anyone selling SCJA training material? Your OLD stuff? KMN Reviews / Advertising 0 05-03-2008 05:05 PM
X&Y Coordinate Drawing on jPanel BHCluster Java 2D 2 03-27-2008 12:47 PM
No Fluff Just Stuff Software Symposium Series 2007, orchid Reviews / Advertising 0 04-08-2007 10:13 PM


All times are GMT +3. The time now is 12:34 PM.


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