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

Adding image to Content Pane of JFrame  RSS feed

 
Akhil Jain
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I have this small application which contains some menu options. application adds components to it only after some selection has been made from menu.

so at the starting my application's screen looks very empty.
I want to know how to put a gradient background to it and attach some images, like some logo and some information about applicaiton.

I am completely blank on how to do this.
I tried creating a complete JPG for background and adding it to the JLabel.
but the problem is , when i resize my application window, the image remains of the same size.

I am attaching screen shot of the application main window.
can some one please tell me how to make its free space's background as gradient one, like starting with white and slowly increasing color to light grey....(so that when i resize window it still remains the same.).



i am also attaching some custom made effect in MS-word, which i want to create in free space here.



Please help on how to do this.


regards,
Akhil Jain
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the example below I just overrode paintComponent() in a custom JComponent to do all the painting.

 
Rob Spoor
Sheriff
Posts: 21088
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should put a condition on the painting:
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd rather use a CardLayout in the frame than have a special condition in the background component... but either works.
 
Akhil Jain
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks nathan

That was of help.

your idea of using CardLayout, seems good to me, i will rather appreciate it, if you can provide the same example using CardLayout, this will help me understand the layouts clearly, and will be able to make out differences in two approaches.

Thanks again,
Akhil
 
Akhil Jain
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more question:

If we paint the component's background, as done above. then how can we add our images (say some logo) at the same painted contentPane.
I wanna place some logo on left bottom corner and right bottom corner of this customely painted contentpane background.

Please help me out with this as well.

Thanks and regards
Akhil
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For images you would use the Graphics.drawImage(image, x, y, imageObserver) method - you can just pass 'this' in for imageObserver if you're painting in a component. You would use the bounds of the component to compute the x and y location to paint the logo at. component.getWidth() - image.getWidth() - offset would specify the x location of an image based off the right edge of the component. You could do the same thing with getHeight() for the y location if you wanted to calculate from the bottom of the component.

There is a section on CardLayout in the Java tutorial.
[ November 20, 2007: Message edited by: Nathan Pruett ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!