Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't paint a JComponent

 
Alejandro Barrero
Ranch Hand
Posts: 355
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rob Camick
Ranch Hand
Posts: 2619
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alejandro Barrero
Ranch Hand
Posts: 355
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 355
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 355
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Define "breaks".
 
Alejandro Barrero
Ranch Hand
Posts: 355
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6337
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 355
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 355
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RESOLVED--------------The reason the program wasn't showing the drawn figures was because I was drawing to negative coordinates.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic