• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Adding image to Content Pane of JFrame

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the example below I just overrode paintComponent() in a custom JComponent to do all the painting.

 
Sheriff
Posts: 22802
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should put a condition on the painting:
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Oh, sure, you could do that. Or you could eat some pie. While reading this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic