Question:

Java! createImage(byte[] imagedata)?

by  |  earlier

0 LIKES UnLike

I need to create an image using

Image createImage(byte[] imagedata)

(NOT createImage(ImageProducer producer))

My code is

import java.awt.*;

import java.awt.image.*;

class MyCreateImage extends Frame{

Image img;

public static void main (String args[]) {

new MyCreateImage("Create Image");

}

public MyCreateImage(String s) {

super(s);

setSize(600, 500);

setVisible(true);

byte pixels[] = {100, 60, 100, 10, 50};

img = Toolkit. getDefaultToolkit(). createImage(pixels)

System.out.println("w = " + img.getWidth(this));

System.out.println("h = " + img.getHeight(this));

}

public void paint (Graphics g) {

g.drawImage(img, 100, 100, this);

} }

Why I get

--> w = -1

--> h = -1

I would appreciate any help! Thanks in advance.

 Tags:

   Report

2 ANSWERS


  1. In your main() and MyCreateImage() methods, i don't see any calls to paint(). It looks as if paint() just sits there and is never called, might want to check that.


  2. Try to use BufferedImage

        int data[] = new int[width*height];

        int i = 0;

        for(int y = 0; y < height; y++){

          int red = (y * 255) / (height - 1);

          for(int x = 0; x < width; x++){

            int green = (x * 255) / (width - 1);

            int blue = 128;

            data[i++] = (red << 16) | (green << 8) | blue;

          }

        }

        img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        img.setRGB(0, 0, width, height, data, 0, width);

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.