?
Solved

Create image with MemoryImageSource

Posted on 2003-02-23
3
Medium Priority
?
385 Views
Last Modified: 2008-03-10
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
Comment
Question by:etgaroy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 80 total points
ID: 8003703
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
 

Author Comment

by:etgaroy
ID: 8006691
It works- thanks.
How do I change the color of the background which is blue back to white?
g
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8007346
try filling your image array with 0xFFFFFF before you do your drawing...  hope this helps :-)

Good luck!

Tim.
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 9 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question