Hey! I got two classes here, one that does stuff with an image and one class utilizing the other class. So inside my picture class I have an Image object and in my other class I want to have a JFrame and I want to set the size of the frame to the size of the picture... So inside my picture class I tried creating two methods, getX and getY that returns

int methodX(){return picture.getWidth(this);

alternatively with null in the argument:

 int methodX(){return picture.getWidth(null)

but when I call these methods from my other class with a System.out.print(just to check the value) it returns -1.

What would be the proper way to get the pixel size of the image?

What does the API doc say for the Image class's getWidth() method?
Does the picture variable have a valid image?

abstract int getHeight(ImageObserver observer)
Determines the height of the image.
Determines the height of the image. If the height is not yet known, this method returns -1 and the specified ImageObserver object is notified later. "

The image is valid I suppose since this method work, inside my picture class there's the:

 Image picture;
void paintComponent(Graphics g){
        super.paintComponent(g);

        g.drawImage(picture, 0,0, picture.getWidth(this), picture.getHeight(this), this);

    }

But if I make another method that tries to return the int of "picture.getWidth(this)" it just gives me a -1.
Maybe if I try all this with a BufferedImage instead of an Image it will work better!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.