Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

java.awt.Image.getWidth() Not Working!

Status
Not open for further replies.

drkestrel

MIS
Sep 25, 2000
439
GB
Right- this is want I want to do.

I have a background image which is added to as a background image to a third party API class which extends Java.awt.Container. Such container is then added to the Content Pane of MyClass (which extends JFrame)

I want to read the width and height of the image and resize MyClass accordingly.

Problem is calling theImage.getWidth(MyClass.this) or theImage.getWidth(theThirdPartyAPIContainer) always return -1 and -1 denoting that no information is available.

What do I need to do to obtain the height and width?
 
Now I figure out I need to do something like
Code:
    Image img = getToolkit().getImage(imgURL);
    MediaTracker tracker = new MediaTracker(this);
    tracker.addImage(img,0);
    try   
    {
        tracker.waitForID(0);
    }
    catch (InterruptedException e){}
    int w = img.getWidth(this);
    int h = img.getHeight(this);

However, what I have is the following
Code:
MyFrame---
          |
_________ \/____________________________________
|                JMenuBar                       |
|_______________________________________________|
|                                               |
|                                               |
|                                               |
|Third Party Class extending java.awt.Container |
|      containing an image of known size        |
|                                               |
|_______________________________________________|

I am trying to calculate what size MyFrame needs to be. MyFrame extends JFrame.

I am fine for the width
I do
Code:
Insets frameInsets = this.getInsets();
width = image.getWidth() + frameInsets.left + frameInsets.right;

Height is the problem
After some Trial an error it LOOKS on Solaris as if the following is right-
Code:
height = image.getHeight()+frameInsets.top+ myJMenuBar.getSize()+getHeight+ myJMenuBar.getInsets().top + myJMenuBar.getInsets().bottom
I cannot comprehend whether this is just a coincidence or not as I don't know why frameInsets.bottom should in any case be excluded.
Here are the dimensions-
frameInsets.left = 6
frameInsets.right= 6
frameInsets.top =28
frameInsets.bottom = 6
JMenuBar height = 23
JMenuBar's Insets.top = 1
JMenuBar's Insets.bottom = 1

What should be the 'correct' calculations and does the Insets for the JMenuBar and the JFrame overlaps?
 
After some more experimentation, it turns out that the calculation is really
Code:
height = image.getHeight()+frameInsets.top + frameInsets.bottom + myJMenuBar.getSize()+getHeight+ myJMenuBar.getInsets().top + myJMenuBar.getInsets().bottom

This works fine. However, MyFrame has a "New Window" JMenuItem that creates another window with the same image inside the theThirdPartyAPIContainer. Very strangely, for the new window, I get
frameInsets.left = 0
frameInsets.right= 0
frameInsets.top =0
frameInsets.bottom = 0
JMenuBar height = 0.0
JMenuBar's Insets.top = 1
JMenuBar's Insets.bottom = 1

Just what is going on?

The skeleton of my code is
Code:
class MyFrame extends JFrame
/// .....
    public MyFrame()
    {
    //.. do something
    buildFrame();
    drawMenu();

    this.show();
    }

    private buildFrame()
    {
        //Load image into myURL and set it as
        // background in a thirdParty API awtComponent

    }

    private drawMenu()
    {
        JMenuItem newItem = new JMenuItem("New Window");
        newItem.addListener(// a Listener which does 
        MyFrame newFrame = new MyFrame());
        JMenu fileMenu = new JMenu("File");
        fileMenu.add(newItem);
        JMenuBar myBar = new JMenuBar();
        myBar.add(fileMeun);
    }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top