Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem adding JPanel in JFrame  RSS feed

 
yousaf khan
Greenhorn
Posts: 24
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have drawn a simple paint brush program. It works fine. Only problem is that the JPanel is not showing on JFrame. When i remove painComponent method, JPanel appears. I have set the color of JPanel White, if i maximize the JFrame window, the White color appears on the background except on some area where JFrame was present when it was not maximized, The rest of the background is colored white or whatever color i set for JPanel. Any help finding what's causing this problem ? Here's my code. (PaintComponent method is in Draw class)
Also i am tring to make a "clear_all" function which if called erases everything from the drawing area. I can't figure out how to do it. Any suggestions how i can make this function ?

MyGui class


Draw class

Main class
 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only problem is that the JPanel is not showing on JFrame. When i remove painComponent method, JPanel appears.


So then look at your paintComponent() method.

1) first you paint the background
2) then you draw an empty image over the background

I would guess you need to make your image transparent so the background color shows through.

Check out the DrawOnImage example found in Custom Painting Approaches which shows how I create the image to be used as a drawing canvas.
 
yousaf khan
Greenhorn
Posts: 24
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if i have understood you correctly, what i am doing wrong is that i am adding Jpanel to Jframe and on that Jpanel i am adding empty image which completely covers JPanel. You suggested i have to make my image transparent, how can i do that ?
Is it possible that i can make the color of image same as background color ? If so how ?
 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I gave you a link to working code that shows how to do what you want.
 
yousaf khan
Greenhorn
Posts: 24
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah, i have finally figured it out. I also asked about making a function that removes all the drawing from the drawing area at once. Any suggestion on how i can make that ?
 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What part about actually reading the information provided in the link you have been given do you not understand?

What part of the "Clear Button" logic do you not understand??? You obviously didn't even take the time to read the information found on the provided link.

We are not here to spoon feed code for you.

Do some simple reading!!!
 
yousaf khan
Greenhorn
Posts: 24
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry if you are offended. I actually did see that code in the link you provided but i was so busy in finding the answer to first part of my question regarding JPanel that i forgot that there was code to clear the entire drawing area. Thanks for your help !
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!