Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Center an image in a JDesktopPane

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I would like to display an image in the center of a JDesktopPane.
I tried adding a JLabel containing an Icon to the JDesktopPane,
but the image will always be displayed in the uppel left corner
of the desktop.
Kind Regards,
Felix
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DefaultToolkit screen = DefaultToolKit.getScreenSize();
dx = the width of the pic
dy = the height of the pic
xwidth = (screen.width-dx)/2;
yheight = (screen.height-dy)/2;
yourLabel.setBounds(xwidth, yheight, dx, dy);
yourPanel.add(yourLabel);
Try that

------------------
Happy Coding,
Gregg Bolinger
 
Felix Schudel
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Gregg.
I tried this, I'm adding the Label to a DesktopPane which ignores
the bounds (at least the x, y coordinates):
ImageIcon icon = new ImageIcon("O:\\logo.jpg");
JLabel label = new JLabel(icon);
label.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());
java.awt.Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
int dx = icon.getIconWidth();
int dy = icon.getIconHeight();
int xwidth = (screen.width-dx)/2;
int yheight = (screen.height-dy)/2;
myDesktop.add( label, JLayeredPane.FRAME_CONTENT_LAYER);

my other problem is, that the size of my
desktop is not equal to the size of the screen and I would
like to use a kind of LayoutManager, so I won't have to
change the position of the label every time the size of
the desktop changes.
regards,
Felix
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could try adding this:

Also, if you implement ComponentListener you can catch the componentResized and update your image location.
[This message has been edited by Matt Hansen (edited October 04, 2001).]
[This message has been edited by Matt Hansen (edited October 04, 2001).]
 
If you look closely at this tiny ad, you will see five bicycles and a naked woman:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic