Can't paint a JComponent
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
What do I have to do to force painting? Do I have to invalidate something?
Your help will be greatly appreciated,
Alejandro Barrero
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Alejandro Barrero
Ranch Hand
Posts: 502
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
Your help will be greatly appreciated,
Alejandro Barrero
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Alejandro Barrero
Ranch Hand
Posts: 502
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Following the Swing tutorial, I used a class that extends JPanel with method:
I changed my code to:
But still it doesn't work.
I changed my code to:
But still it doesn't work.
Your help will be greatly appreciated,
Alejandro Barrero
Alejandro Barrero
Ranch Hand
Posts: 502
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
Your help will be greatly appreciated,
Alejandro Barrero
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Define "breaks".
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Alejandro Barrero
Ranch Hand
Posts: 502
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
Your help will be greatly appreciated,
Alejandro Barrero
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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: 502
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
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?
Your help will be greatly appreciated,
Alejandro Barrero
Alejandro Barrero
Ranch Hand
Posts: 502
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
RESOLVED--------------The reason the program wasn't showing the drawn figures was because I was drawing to negative coordinates.
Your help will be greatly appreciated,
Alejandro Barrero
| Tell me how it all turns out. Here is a tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |









