aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Image display in layout manager Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Image display in layout manager" Watch "Image display in layout manager" New topic
Author

Image display in layout manager

Ronald Vermeij
Ranch Hand

Joined: Sep 05, 2009
Posts: 37
Goodmorning All,
I am looking for some help, a hint on my AWT Layout challenge i have.

Example GUI_1:
|=======================|
| Window border..............[_][x]....|
|=======================|
|.[label].| display area |..[button].|
|.[label].| display area |..[button].|
|............| display area |..[button].|
|............| display area |..[button].|
|............| display area |................|
|=======================|
|.................................[QUIT]........|
|=======================|

I want to
- display a picture in the "display area" of the this GUI
- put labels left of it with some text
- put a couple of buttons right of it, through which i am able to manipulate the image

Questions:
1. Is it (even) possible to display an (jpeg,gif) image in the "display area" while using a layout manager?

Surfing over the internet...so far i only have found applet-examples that use the full gui surface of the applet to display an image via the g.drawImage (imagename, x,y); without any gui elements (labels, buttons) beside it.

2. What kind of AWT gui element do i need at the location of the "display area"?
Is is possible to use a CANVAS or do i need somethen else? (e.g. glue an image on a button)

3. Is is possible to display an ANIMATION (series of sequences jpeg, gifs) in the display area?
I already found out how to locate and load the images (via mediatracker).
Now i need to find a way to "paint" the loaded images in the GUI.

4. Is this even possible with AWT layout or do i need to switch to SWING layout?
I have not used Swing yet, cause i'm working my way up through the oldest gui technologie first.

5. Do know any usefull websites, online tutorials (on awt and displaying images) that can help me tackle my challenge?

Thank you very much for your hints, tips and tricks

Ronald
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Welcome to JavaRanch

The short answer is, yes, it is possible. But I think you are going about it the wrong way.

Stop using AWT components and go straight to Swing. Nobody uses AWT components any more. Start with something simple if you are learning. Have a look at the Java™ Tutorials which has sections about layouts in. You may find you have to use the more complicated layouts.
  • You can try GridBagLayout along with the GBC class of Cay Horstmann's (and look at the GridBag anti-tutorial (!)). There are more details of the GBC class in Horstmann's book (I think vol I).
  • You can try Googling for MigLayout, which a lot of people say is easier to use, though I have never tried it.
  • Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19722
        
      20

    Campbell is right about forgetting all about AWT. If you switch to Swing, you can use a simple JLabel in combination with ImageIcon for displaying your images. That will not handle animation, but with a SwingWorker or javax.swing.Timer you can change the icon of the JLabel every few milliseconds.


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
    Brian Cole
    Author
    Ranch Hand

    Joined: Sep 20, 2005
    Posts: 862
    Ronald Vermeij wrote:
    1. Is it (even) possible to display an (jpeg,gif) image in the "display area" while using a layout manager?
    Surfing over the internet...so far i only have found applet-examples that use the full gui surface of the applet to display an image via the g.drawImage (imagename, x,y); without any gui elements (labels, buttons) beside it.

    Yes, and it's pretty easy if you want the image to appear at its natural size: new JLabel(new ImageIcon(yourJpegOrGif))


    2. What kind of AWT gui element do i need at the location of the "display area"?
    Is is possible to use a CANVAS or do i need somethen else? (e.g. glue an image on a button)

    It is possible to use java.awt.Canvas, but it's easier to use javax.swing.JLabel.

    Swing is built on AWT, so JLabel is a fine thing to use in AWT apps. (The reverse isn't always true. It can be difficult to use a java.awt.Label in Swing apps due to lightweight/heavyweight issues.)


    3. Is is possible to display an ANIMATION (series of sequences jpeg, gifs) in the display area?
    I already found out how to locate and load the images (via mediatracker).
    Now i need to find a way to "paint" the loaded images in the GUI.

    Sure.

    If you use JLabel's setIcon() method you don't have to mess with MediaTracker or painting.

    But if you want to do the painting yourself, that works too. In that case you can use the repaint() method to force a redraw.


    4. Is this even possible with AWT layout or do i need to switch to SWING layout?

    Swing uses AWT layout, so there is no difference.

    Even BoxLayout and SpringLayout (which are packaged with Swing) are not Swing-specific and work fine in AWT. I tend to stick with the AWT layout managers anyway.


    I have not used Swing yet, cause i'm working my way up through the oldest gui technologie first.

    I would not recommend this approach for most people.

    Swing corrected a lot of mistakes (poor design decisions) that AWT is stuck with. Also Sun has fixed/enhanced Swing over the years while for the most part not doing so with AWT. The result is that Swing is (usually) both less cumbersome and more flexible than straight AWT.

    I would say in most circumstances it would be unwise to use AWT component classes instead of their Swing counterparts.
    Ronald Vermeij
    Ranch Hand

    Joined: Sep 05, 2009
    Posts: 37
    Campbell Ritchie wrote:Welcome to JavaRanch
    The short answer is, yes, it is possible. But I think you are going about it the wrong way. Stop using AWT components and go straight to Swing. Nobody uses AWT components any more. Start with something simple if you are learning.

    Thanks Ritchie for the encouragement to switch to Swing.. it really works better.


    Ronald Vermeij
    Ranch Hand

    Joined: Sep 05, 2009
    Posts: 37
    Rob Prime wrote:Campbell is right about forgetting all about AWT. If you switch to Swing, you can use a simple JLabel in combination with ImageIcon for displaying your images.

    Thanks Rob, I've figured out how to do it with your JLabel and ImageIcon combination It works great!
    Ronald Vermeij
    Ranch Hand

    Joined: Sep 05, 2009
    Posts: 37
    Brian Cole wrote:
    1. Yes, and it's pretty easy if you want the image to appear at its natural size: new JLabel(new ImageIcon(yourJpegOrGif))
    2. It is possible to use java.awt.Canvas, but it's easier to use javax.swing.JLabel

    It works great and very easy. This was i had in mind when i started

    3. If you use JLabel's setIcon() method you don't have to mess with MediaTracker or painting.
    But if you want to do the painting yourself, that works too. In that case you can use the repaint() method to force a redraw.

    Yes when bypassing the media tracker, for my it a lot easier this way, but i did had a hard time figuring out where the images needed to be in the filesystem, in order for the ImageIcon method to work correctly

    I've found a beautifull tutorial website on AWT and SWING over here, owned by Chua Hock Chuan
    http://www3.ntu.edu.sg/home/ehchua/programming/java/J4a_GUI.html
    Nice layout and (for me) a perfect balance between code-snippets and crystal clear graphics!

    The JavaNotes website of Fred Swartz was also helpfull
    http://www.leepoint.net/notes-java/GUI-lowlevel/graphics/45imageicon.html

    On the (Awt, Swing) layout part i have no more questions for now.
    Thank you for your hints and tips
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Image display in layout manager