• Post Reply Bookmark Topic Watch Topic
  • New Topic

Paint Problem

 
sajjan sarkars
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My problem is this,I have a class called VizTest which extends JPanel.I draw some graphics here.Then I need to add it to another JPanel which is then added to a JFrame. But when I run it it doesnt show my drawing!!

Can you help me? Please find attached the sample code .

 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The panel on which you're drawing the circle is much too small to show it. Because of its red background you can see that its dimensions are barely larger than the input field - there's no room for the circle.

What is the "panel" JPanel supposed to accomplish? If you leave that out, and add "v" directly to "frame", you should be able to see the circle (because then "v" covers the complete Frame, instead of just a tiny portion of it).
 
sajjan sarkars
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf,

Thank you for replying! I tried making "panel" larger by saying


It still doesnt work. I need to use the panel as the way my GUI is set up,I cant add directly to the JFrame.I just tried to simplify the problem to post here,hence it appears trivial.I need to place my graphic in a panel which goes into another panel which goes into the JFrame.

Thank you for taking the time!
 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to call frame.pack() between adding components and frame.setVisible().

And you dont need setVisible(true) or setOpaque(true) methods in paintComponent(Graphics g) painting gets called when the component is visible (showing) so... and JPanel is opaque by default.

And you should add VizTest to frame so it will be stretched on whole area, now you are adding it to some other JPanel which has FlowLayout by default.

And you could override VizTest getPreferredSize() method where you add the dimensions of the oval to preferredSize which you get from super class (JPanel).


those lines you can delete pack() does the work.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cross-posted on mult fora.
 
sajjan sarkars
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mikko,

Thank you for your advice and refactoring suggestions.I did not know about pack() . But even after adding it I am unable to get the drawing.here is the updated code.



 
sajjan sarkars
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pete stein wrote:cross-posted on mult fora.


I am not aware of this,please let me know the URLs of the other posts so I can learn their solutions. Thanks!
 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>> And you could override VizTest getPreferredSize() method where you add the dimensions of the oval to preferredSize which you get from super class (JPanel).

>> And you should add VizTest to frame so it will be stretched on whole area, now you are adding it to some other JPanel which has FlowLayout by default.

And one thing is that setBackground(Color.RED); in paint method ??? replase it with g.setColor(Color.RED);
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sajjan sss wrote:
pete stein wrote:cross-posted on mult fora.


I am not aware of this,please let me know the URLs of the other posts so I can learn their solutions. Thanks!


You're not aware?? You yourself cross-posted this on the Sun fora and then never acknowledged replies or noted cross posting elsewhere.

Cross-posting can frustrate anyone who tries to help you only to find out later that the same answer was given hours ago in a cross-posted thread. No one likes wasting their time, especially a volunteer. The polite thing to do would be to not do this, but if you feel that you absolutely must, to at least provide links in both cross-posts to each other.

This is also directly against JavaRanch policy.
Sun Swing Forum
 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shit now I have wasted my precious time
 
sajjan sarkars
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just wanted to say a few things,
1. When Pete said "other fora" ,I misunderstood ,thinking he meant other forums WITHIN JavaRanch. Since I had not done that I wanted to clarify.
2. True,I DID post the same question on Sun's Forum as well,genuinely not knowing the rules of both fora. For this, I sincerely apologize.I will not do so again.I only did so out of eagerness to learn how to fix the problem,and nothing else. Wasting your time was not my intention.I understand your irritation. I apologize again.
3. I am truly grateful to all the people who run the forums and appreciate every reply that you have given,spending a lot of time to analyze your answers.Please do not stop doing so ,or get discouraged by my silliness.

[crossposted at http://forums.sun.com/thread.jspa?threadID=5361877 ]





 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Sajjan SSS",
Please check your private messages for an important administrative matter.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sajjan,
It might be worth your while to go through all the items on this page,one of which pertains to cross posting.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!