aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Display image in a BorderLayout 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 "Display image in a BorderLayout" Watch "Display image in a BorderLayout" New topic
Author

Display image in a BorderLayout

achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
Hi all.
I was setting up a template to make it easier for layout designs, I was having fun until it turn very nasty when I tried to display a .jpg image inside a BorderLayout using JFrame.
Compiles okay, but won't display the image. Can some gurus have a look please. Here is the code, I've highlighted the problematic bit:


humanum errare est.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9


There's a couple of strange things here. First, JFrame is a top-level container, which means it is suited for containing an application itself. It is probably not the best choice for inserting into another container. Next, the call to getToolkit().getImage returns immediately. You need to use a MediaTracker instance to watch the loading of the image and let you know when it is 100% and ready for display. Finally, why are you creating a new frame in paint()? paint() can be called hundreds of times if a frame is moved or something is dragged on it.
I think you should check out the
Swing Tutorial. It has a section on graphics which will explain the details of working with images.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
Thanks, I'm actually working through it hoping to find the relevant bits and not covering the same ground over and again.
Just when I thought I had it well under control, I learn that displaying images in JAVA is perhaps one of the most difficult thing.
My productivity will drop down to zero as I come to grips with it.
I think JAVA so complicated, it is too clever by half.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Dude, displaying an image in a JFrame is subtrivial. You're just doing crazy stuff here. I can't even imagine where you're getting your coding ideas.

Now, don't you come 'round here dissin' Java, just because you tried to write a few hundred lines of code to do what can easily be done in five lines.


[Jess in Action][AskingGoodQuestions]
achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
I'm not "dissin" JAVA, I think the idea is great, best thing since sliced, whole-meal, multi-grain bread.
Thanks for the JFrame thing, but I am trying to embed an image in a container. My research suggests JTextArea.
Joe Ess pointed out correctly that JFrame is a top level container and I cannot embed JFrame in a BorderLayout.
Thanks for the input.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Embed an image in a container? Are you talking about making a "wallpaper" for your application? This is easily done, but you must override the correct component. A search for "background image" in this Forum yeilds 80 results. The best approach I have seen is by Nathan, one of our wonderful bartenders. His approach can be found here. Notice the method paintComponent in his custom class. That method is the key if in fact you are trying to wallpaper your app.
Hope this helps.


GenRocket - Experts at Building Test Data
achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
Thanks for taking time to look at my problem.
I'm also reading up John Zukowski's excellent "Java 2 J2SE1.4", Chapter 10 "Animation and Images" goes in detail on loading an image with media traking. However it seems an overkill at this point.
I am just trying to load an icon to display on the screen from the applet I posted earlier.
I think the best way to go is use a JTextArea, but now the Java Console is telling me I've an "access privilege" error.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

You can't put an image in a JTextArea. Do what Earnest said to do, or use the approach I suggested by Nathan. There should ne more guessing on how to do this. Just do it.
achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
Hi Gregg, I'm trying to understand the way its done, some of the codes are very advanced for me.
Here is the code for displaying an image in a JTextArea, unlike my code, it has the benefit of working:

Now here is my tortured code, still trying to put an image into the Borderlayout. It compiles fine, JAVA console displays no error messages but no image, can some see what's wrong with it :
achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
Can someone please explain to me where I've gone wrong.
I've made, on a trial and error basis, so many modifications and still no image. No compilation errors, java console shows no exceptions, the html displays the borderlayout with some fancy add-ons, but no picture.
If only the java console throws an exception, I can handle that 'cause I'll know where I'm at! But no, not even an exception message.
achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
Hmmm, it seems I'm too fixated on awt.
I need to read up more on swing.
Can't do it with awt, need swing, I think...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Display image in a BorderLayout