Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Create an image from an integer array

  Asked By: Ernesta    Date: Jul 02    Category: Java    Views: 4551

I'm having a bit of trouble using Java's MemoryImageSource methods at the
moment. I was therefore wondering if any of you could help me out!

I've been trying to create an image from an integer array, but with no luck.
I then copied and pasted code from the Java API to see how it worked, but
even that didn't work. (Code below). I keep getting the error-message: can't
resolve symobl 'createImage()'. But I've imported java.awt.image.* which
should be all I need.

Does anyone know what I'm doing wrong? (Must be something very obvious, but
I just can't see it)

I was also wondering if you knew how to read in a ppm image in Java, and if
it was possible to convert it to a gif or jpeg.

I'll be glad for any suggestions you may have!


import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.image.MemoryImageSource;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;

public class ImgSourceTest

public static void main (String[] args)

Image image;
MemoryImageSource source;

int w = 100;
int h = 100;
int pix[] = new int[w * h];
int index = 0;

for (int y = 0; y < h; y++) {
int red = (y * 255) / (h - 1);
for (int x = 0; x < w; x++) {
int blue = (x * 255) / (w - 1);
pix[index++] = (255 << 24) | (red << 16) | blue;

source = new MemoryImageSource(w, h, pix, 0, w);
image = createImage(source);





3 Answers Found

Answer #1    Answered By: Josie Roberts     Answered On: Jul 02

The line:

image = createImage(source);

should read:

image = Toolkit.getDefaultToolkit().createImage(source);

The example code provided in the JavaDoc is imcomplete. This is probably
because the author just pulled it out of some
larger example without properly referencing the example (it mentions "adapted
from the MemoryAnimationSourceDemo by
Garth Dickie" but that doesn't mean much to me).

Answer #2    Answered By: Fdsfg Gdrt     Answered On: Aug 21

i know some code example demonstrates how to create image files using this Method in C#.NET applications. check out:

Didn't find what you were looking for? Find more on Create an image from an integer array Or get search suggestion and latest updates.