• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Difficulities in adding loaded image to JPanel

 
Ranch Hand
Posts: 76
IntelliJ IDE Spring BSD
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey all. Didnt' know whether to place this here or in the GUI section, but considering it's most likely a beginner issue, figured it was the place to go.

I'm trying to have a JPanel load up an image to a certain section of the site itself. Here's the JPanel code that I'm using:


As you can see I'm dividing the JPanel into their own get methods, as I found having it all together got unwieldy fast. I've tried to load images using Imageicon, wrapping the image loader inside a JLabel, even tried to create a separate class for having the image loaded (which is what I'd like to do in the end) and all it's gotten me is very confused. If anyone has any advice I'd be more then grateful in how to have this Jpanel (and down the road, others) be able to load images easily.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The GUI forum would have been better. Moving
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
none of the code you posted has anything to do with the stated problem.

things you might try
- search this forum and google keywords
JLabel ImageIcon
you should find plenty of sample code re loading an image into a JLabel, then adding that to a parent container

- this forum's main page has a Swing FAQ link, with one of the topics being BackgroundImageOnJPanel,
this'll show you how to draw an image

note: this line will probably cause you a lot of pain, best left for netbeans-generated code
panelPicture .setLayout(new GroupLayout());
 
Chris Creed
Ranch Hand
Posts: 76
IntelliJ IDE Spring BSD
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Dunn wrote:none of the code you posted has anything to do with the stated problem.



Well the rest of it is other methods that either create JPanels, or appends them to the JFrame itself. As they are working I omitted them for brevity (and I doubt anyone wants to look at a wall of code).

Michael Dunn wrote:things you might try
- search this forum and google keywords



Been there, done that. Even got the tshirt! :D

Michael Dunn wrote:
JLabel ImageIcon
you should find plenty of sample code re loading an image into a JLabel, then adding that to a parent container

- this forum's main page has a Swing FAQ link, with one of the topics being BackgroundImageOnJPanel,
this'll show you how to draw an image



The Imageicon one is one that Iv'e tried with no success. Same with the forum example (good suggestion btw). So far the closest that I've gotten was being able to have a 1x1 square within the JPanel itself with the following code.



the sample.jpg image is in the same folder as the src files for reference.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> So far the closest that I've gotten was being able to have a 1x1 square within the JPanel itself...

generally indicates nothing has a size or forces a size.

add the indicated line and see if it makes a difference (modify the 400,300 to suit)



 
Chris Creed
Ranch Hand
Posts: 76
IntelliJ IDE Spring BSD
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Dunn wrote:> So far the closest that I've gotten was being able to have a 1x1 square within the JPanel itself...

generally indicates nothing has a size or forces a size.

add the indicated line and see if it makes a difference (modify the 400,300 to suit)





That displayed the image! Needs a bit of work to have it completely fill the JPanel but its' a great start in the right direction. Thank you VERY much!

I assume that their are ways to have images loaded either automatically fill the entire Panel/Frame, or have java read the measurements of the loaded image and display accordingly?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the problem is that you're adding the BackgroundPanel to panelPicture which is a FlowLayout.
panelPicture = new JPanel();//default layoutManager is a FlowLayout

whatever size panelPicture becomes (assuming added to a JFrame), it will simply grow/shrink,
leaving it's children at their preferredSizes.

to see the difference
1) remove the line you just added
2) change panelPicture to a BorderLayout
panelPicture = new JPanel(new BorderLayout());
3) add getPanelPicture() to the contentPane of a JFrame
frame.getContentPane().add(getPanelPicture());

now size the frame to whatever you want, and when it is showing on the screen,
drag the frame wider/taller and see the image change size.
 
We don't have time for this. We've gotta save the moon! Or check this out:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic