Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Center an image in a JDesktopPane  RSS feed

 
Felix Schudel
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Matt Hansen
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • 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).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!