File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Can't paint a JComponent Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Can Watch "Can New topic
Author

Can't paint a JComponent

Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 319
I am using graphics to draw ovals and then I want to display the ovals, but It is not working. My code is

What do I have to do to force painting? Do I have to invalidate something?


Your help will be greatly appreciated,
Alejandro Barrero
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
You should not invoke the paint() method directly. The RepaintManager will determine when a comonent needs to be repainted.

Custom painting should be done in the paintComponent() method of your component. To force a repainting of the component you just invoke the repaint() method on the component.

Read the section from the Swing tutorial on Custom Painting for more explanation and examples.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

Where did you get "graphics" from? While I agree with Rob C if you are doing this with the GUI graphics object, if it's the graphics object of a BufferedImage that shouldn't be a problem.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 319
Thank you both for your prompt response. I am using the graphics from the JPanel. I changed paint() to repaint(), but it still doesn't work. What I am trying to do is display the ovals and strings in the JPanel. How should I do this?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

Did you mean you used getGraphics()? Then Rob C is right - you should override paintComponent, call super.paintComponent(g) first and do the rest of your custom painting (the ovals and strings) in the remainder of the method.
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 319
Following the Swing tutorial, I used a class that extends JPanel with method:

I changed my code to:

But still it doesn't work.
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 319
I just retested my program with debug and to my surprise the program brearks in my my repaint method and executes the graphics methods. But still doesn't display anything. This has to be very close to a solution; what else should I do?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

Define "breaks".
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 319
I apologize, I meant to write breaks. I set a break point at the beginning of the repaint method and the program stops execution at this point; I can then single step and see each graphics method being executed.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Where do you get nodesList from? If it's a member variable which you also set with setNodesList(), won't workFlowPanel.setNodesList(nodesList) be useless?

Where and how do you fill this list of nodes? We can't really see what you're trying to paint exactly.

Why are you adding half the diameter to each node? Most Graphics methods consider x to be the left, and y the upper bound of what you're trying to paint.

You also shouldn't have to call repaint() in the Event Dispatch Thread. repaint() is safe, and will add tasks to the EDT itself.
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 319
Thank you Stephan. I want to display a work flow tree that I create in a dialog (the display panel is in a different dialog). I have simplified the code and I am no longer calling repaint; the sizes and the locations are no problem (I'll correct them when it displays). Currently, I have a button in the dialog that creates the tree to open the display dialog, which contains the display panel in a scroll pane). I am passing, in the constructor, a list of the work flow steps (a step has a list of next steps).

The constructor of the display dialog is

The method constructGraph constructs a list of GraphNode (this, nodesList, is what I set in the display pane with "workFlowPanel.setNodesList(nodesList"). When the constructor of WorkFlowDialog completes the dialog is displayed and it is using my paintComponent method (I can single step through it). However nothing is displayed.
I am perplexed; if the repaint method is executing "graphics.drawOval(x, y, WorkFlowDialog.DIAMETER, WorkFlowDialog.DIAMETER);" why the window doesn't show anything?
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 319
RESOLVED--------------The reason the program wasn't showing the drawn figures was because I was drawing to negative coordinates.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can't paint a JComponent