0

I have been using jFrame to build a GUI. I had to insert images in the GUI, for which i inserted a label and put the image as an icon for the label. Now, i have to find out the position of the image in terms of the x and y co-ordinates and i am unable to do that. I have used

 setLocaction(x,y);

but it still doesn't seem to work. I even disabled the layout manager by using

 setLayout(null); 

What is the possible solution for this problem?


Edit

Basically i am creating a Solar system GUI using Swing, so the positions of the planets are to be set by me. I being new to java, there is being some difficulty in implementing the layouts.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Please tell much more detail and show more code. It would help if you showed an image of what you're getting and what you're trying to achieve. Understand that in 95% of the cases, it is best to not use absolute positioning, and that often positions are better placed using the layout managers to achieve a GUI that looks good on **all** platforms and all screen resolutions. – Hovercraft Full Of Eels Nov 26 '14 at 21:17
  • I have also had issues with using specific positioning for JLabels. It'd be nice if the whole process worked like CSS, where I could just set specific coordinates for all items. EDIT: Hovercraft is also right, though. Exact positioning would most likely give you errors on different resolutions. – Zulfe Nov 26 '14 at 21:17
  • Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify. Have a look at [Why is it frowned upon to use a null layout in SWING?](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) for more details – MadProgrammer Nov 26 '14 at 21:31
  • Because the image is been rendered by a `JLabel`, the exact location would be impossible to determine, as the `JLabel` does not provide any information about how it has laid out the image within it's own context... – MadProgrammer Nov 26 '14 at 21:32
  • Basically i am creating a Solar system GUI using Swing, so the positions of the planets are to be set by me. I being new to java, there is being some difficulty in implementing the layouts. – Chandrasekhar Malladi Nov 26 '14 at 22:44

2 Answers2

2

This isn't a layout issue at all, but a drawing and possibly an animation issue. If this were my project, I'd

  • First and foremost, separate out the program logic from its display, a la the MVC or Model-View-Control pattern, or one of its many variants.
  • Create one JPanel for the GUI's graphics and do all my drawing in this
  • I would not display my planet images using ImageIcons inside of JLabels.
  • Rather, I'd create a background image and draw my planet sprites inside of the drawing JPanel's paintComponent method.
  • I'd create non-GUI Planet classes that a non-GUI model would hold.
  • In the GUI portion of my program, I would associate a BufferedImage with each Planet, probably using a HashMap<Plant, Image>.
  • I'd then draw each Planet's associated image in the drawing JPanel's paintComponent(...) method, and place it depending on the Planet's position field values.
  • If I wanted to animate this, I'd use a Swing Timer to drive my simple animation.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Sorry for not commenting. I happened to go through this approach and was not able to get it. It would be of great help if you could direct me on how to do this. Would be glad. Thanks and Sorry again. – Chandrasekhar Malladi Nov 30 '14 at 01:27
  • @ChandrasekharMalladi: thank you for replying. My concern is that the breadth of the question is a bit broad to be able to go step by step through it. If you can ask a more specific question, I'll be glad to try to help though. – Hovercraft Full Of Eels Nov 30 '14 at 02:38
-1

With null layout you should use setSize and setLocation methods on you label to get your image visible correctly inside your frame.

Naruto Biju Mode
  • 2,011
  • 3
  • 15
  • 28