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