• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 386
  • Last Modified:

Create image with MemoryImageSource

Hi all,
I'm trying to create an image with MemoryImageSource from an array of pixels. I know the array represents a valid image but I do not get any image on the panel. This is my code:

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

public class ShowImage extends JFrame {
  private Container container;
  private BorderLayout borderLayout;
  private BufferedReader bReader;
  private int[] pixelArray;
  private int[] finalArray;
  private int[][] newArray;
  private int width = 400, height = 360, currentPixel = 0, i = 0, j = 0, temp = 0;
  private boolean isNumber = false;
//  private Image image;
  private MakePic makePic;

  public ShowImage() {
    super ("Cirius Display");               // setting title to page
    container = getContentPane();
    borderLayout = new BorderLayout (5,5);  // set the layout gap size
    container.setLayout(borderLayout);
    pixelArray = new int[width*height];
    newArray = new int[height*2][width*2];
    makePic = new MakePic ();
    makePic();
    container.add(makePic, BorderLayout.CENTER);
    pack();
    setSize (1024,768);
    show();
  }

  public void makePic ()
  {
    try {
      bReader = new BufferedReader(new FileReader("array.txt"));
      String result = bReader.readLine();
      for (int index = 0; index < result.length(); index++)
      {
        if ((result.charAt(index) >= '0')&&(result.charAt(index) <= '9'))
        {
          isNumber = true;
          currentPixel = currentPixel*10 + (result.charAt(index) - '0');
        }
        else
        {
          if (isNumber)
          {
            isNumber = false;
            pixelArray[temp++] = currentPixel;
            currentPixel = 0;
          }
        }
      }
    }
    catch (IOException e)
    {
      System.out.println("IOException");
    }
    writeMatrix();
}

  public void writeMatrix ()
  {
    for (i = 0; i < height; i++)
    {
      for (j = 0; j < width; j++)
      {
        if (pixelArray[i*width + j] < 51)
        {
          newArray[2*i][2*j] = 0;
          newArray[(2*i) + 1][2*j] = 0;
          newArray[i*2][(2*j) + 1] = 0;
          newArray[(2*i) + 1][(2*j) + 1] = 0;
        }
        else
        {
          if (pixelArray[i*width + j] < 102)
          {
            newArray[2*i][2*j] = 1;
            newArray[(2*i) + 1][2*j] = 0;
            newArray[i*2][(2*j) + 1] = 0;
            newArray[(2*i) + 1][(2*j) + 1] = 0;
          }
          else
          {
            if (pixelArray[i*width + j] < 153)
            {
              newArray[2*i][2*j] = 1;
              newArray[(2*i) + 1][2*j] = 0;
              newArray[i*2][(2*j) + 1] = 1;
              newArray[(2*i) + 1][(2*j) + 1] = 0;
            }
            else
            {
              if (pixelArray[i*width + j] < 204)
              {
                newArray[2*i][2*j] = 1;
                newArray[(2*i) + 1][2*j] = 1;
                newArray[i*2][(2*j) + 1] = 1;
                newArray[(2*i) + 1][(2*j) + 1] = 0;
              }
              else
              {
                newArray[2*i][2*j] = 1;
                newArray[(2*i) + 1][2*j] = 1;
                newArray[i*2][(2*j) + 1] = 1;
                newArray[(2*i) + 1][(2*j) + 1] = 1;
              }
            }
          }
        }
      }
    }
    finalArray = new int[width*height*4];
    for (i = 0; i < height*2; i++)
    {
      for (j = 0; j < width*2; j++)
      {
        finalArray[i*width + j] = newArray[i][j];
      }
    }
    System.out.println("111");
    makePic.getPixel(finalArray);
  }


  public static void main (String args[])
  {
    ShowImage showImage = new ShowImage();
    showImage.addWindowListener(
      new WindowAdapter ()
      {
        public void windowClosing (WindowEvent e)
        {
          System.exit(0);
        }
      }
    );
  }

}


// The Class that is responsible for painting the panel:

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

public class MakePic extends JPanel {
  private Image modImage;

  public MakePic() {

  }

  public void getPixel (int[] pixels)
  {
    for (int i = 0 ; i < pixels.length; i++)
      System.out.println (pixels[i]+" ");
    System.out.println (pixels.length);
    modImage = createImage (new MemoryImageSource(400,360,pixels,0,400));
    System.out.println ("333");
    repaint();
  }

  public void paintComponent (Graphics g)
  {
    g.drawImage(modImage,0,0,this);
  }

}


Thanks,
g
0
etgaroy
Asked:
etgaroy
  • 2
1 Solution
 
TimYatesCommented:
public class MakePic extends JPanel
{
  private Image modImage ;
  private MemoryImageSource mis ;

  public MakePic()
  {
  }

  public void getPixel( int[] pixels, int w, int h )
  {
    for (int i = 0 ; i < pixels.length; i++)
      System.out.println( pixels[i]+" " ) ;
    System.out.println (pixels.length);
 
    mis = new MemoryImageSource( w, h, new DirectColorModel(32, 0xff0000, 0xff00, 0xff), pixels, 0, w ) ;
    mis.setAnimated( true );
    mis.setFullBufferUpdates( true );
   
    modImage = createImage( mis );
    repaint();
  }

 public void paintComponent (Graphics g)
 {
   mis.newPixels() ;
   g.drawImage(modImage,0,0,this);
 }
}

Works?

What's wrong with your one?

You didn't say...

And have a look at my Surface class...it wrappers a lot of this stuff up, and uses the new BufferedImage calls if they are available :-)

it's here:

http://www.things.demon.co.uk/java/lightstorm
0
 
etgaroyAuthor Commented:
It works- thanks.
How do I change the color of the background which is blue back to white?
g
0
 
TimYatesCommented:
try filling your image array with 0xFFFFFF before you do your drawing...  hope this helps :-)

Good luck!

Tim.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now