• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Display image in a BorderLayout

 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
achana chan
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
achana chan
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
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
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.
 
achana chan
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
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
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
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Your mind is under my control .... your will is now mine .... read this tiny ad
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic