i have an little problem with my painting. I'm load images with following method:

public Image getImage(String name) {
    Image image = (Image) this.storage_images.get(name);

    if((image == null) && (!name.endsWith("-"))) {
        try {
            InputStream stream = this.getClass().getResourceAsStream(name);

            if(stream != null) {
                byte[] image_bytes  = new byte[stream.available()];
                stream.read(image_bytes);
                image               = Toolkit.getDefaultToolkit().createImage(image_bytes);
            }
        } catch (Exception exception) {
            System.err.println("Unable to read image from JAR.");
        }

        if(image == null) {
            try {
                image = this.client.getImage(this.client.getCodeBase(), name);
            } catch (Exception exception) {
                System.out.println("ERROR: while receiving image(" + name + "): " + exception);
            }
        }

        if(image != null) {
            this.client.prepareImage(image, null);
            this.storage_images.put(name, image);
        }
    }

    return image;
}

When i will draw the image, it will be cutted - curiously. Im only change the with & height proportional. The original image has a dimension from 256x256.

Here is the Problem: on the appletviewer (eclipse) it seems correct. But when i compile it and open that over my webbrowser, the image will be cutted (see the screenshots at the bottom).

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2   = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    new int[] {
        40,     // position left
        10,     // position top
        65,     // width (Original is 256)
        65      // height (Original is 256)
    };
    g2.drawImage(getImage("warning.png"), insets.right + data[0], insets.bottom + data[1], data[2], data[3], null);
}

i hope you can tell me, what i do wrong.

Result on Webbrowser

Webbrowser

Result on AppletViewer in eclipse IDE*

AppletViewer

有帮助吗?

解决方案 2

Aw, problem solved.

Located on the picture itself!

其他提示

Here is the problem.

byte[] image_bytes  = new byte[stream.available()];

The available value is not the full image size, just the number of bytes that are guaranteed available on next read.

But none of that nonsense is necessary in any case. Most methods that can load an image are overloaded to accept an InputStream.

Further notes

g2.drawImage(
     getImage("warning.png"), 
     insets.right + data[0], 
     insets.bottom + data[1], 
     data[2], 
     data[3], 
     null);

Should probably be:

g2.drawImage(
     getImage("warning.png"), 
     insets.right + data[0], 
     insets.bottom + data[1], 
     data[2], 
     data[3], 
     this);

For any better than 'probably', post a MCTaRE (Minimal Complete Tested and Readable Example).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top