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

Bit Map Image

I have a file which contains the bit map for an image. Is there a facility in Java to display that image i.e. setting the pixels based on that bit map?
0
jc31415
Asked:
jc31415
  • 4
  • 4
  • 4
1 Solution
 
CEHJCommented:
Image i = Toolkit.getDefaultToolkit().createImage("your.bmp");
0
 
jc31415Author Commented:
My image is not a "BMP" image. It just a series of 1's and 0's that represent the value of each pixel.
0
 
CEHJCommented:
How are these 1s and 0s stored?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
jc31415Author Commented:
That shouldn't matter. I just want to read them and set the apporpriat pixel. I have often done this in C++, where there is a function SetPixel.
0
 
CEHJCommented:
It does matter, because different files are interpreted in different ways. What have you got - a 'binary' file?
0
 
TimYatesCommented:
You need to look at BufferedImage (JDK > 1.3) or MemoryImageSource (JDK > 1.0 )

have a look here for an example:

    int iw = im.getWidth( parent ) ;
    int ih = im.getHeight( parent ) ;

    int[] imagearr = new int[ iw * ih ] ;

    PixelGrabber pg = new PixelGrabber( im, 0, 0, iw, ih, imagearr, 0, iw );
    try
    {
      pg.grabPixels();
    }
    catch (InterruptedException e)
    {
    }
0
 
TimYatesCommented:
NOT THAT!!

*hits keyboard*

This:

http://www.cafeaulait.org/course/week9/24.html
0
 
jc31415Author Commented:
That's what I wanted, except for one thing. It is for an applet, and everything I found on a web search was for an applet. Can't an image be created for a program? If not I'll just have to use the applet version.
0
 
TimYatesCommented:
Toolkit.getDefaultToolkit().createImage()
0
 
TimYatesCommented:
Like CEHJ Said:

Image i = Toolkit.getDefaultToolkit().createImage("your.bmp");

:-)
0
 
CEHJCommented:
Here's an example of an image being created in an application:


import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

public class DynImage {

  public static void main(String[] args){
    JFrame f = new JFrame();
    f.getContentPane().add(new DynImagePanel());
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(300, 300);
    f.setVisible(true);
  }

}

class DynImagePanel extends JPanel {
  BufferedImage image;
  int width;
  int height;

  public DynImagePanel() {
    super();
    // Create an image and paint it on the component
    width = 200;
    height = 200;
    image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    WritableRaster raster = image.getRaster();
    // Make a light blue colour
    int[] colour = { 0xcc, 0xcc, 0xff };
    for(int i = 0;i < width;i++) {
      for(int j = 0;j < height;j++) {
        raster.setPixel(i, j, colour);
      }
    }
    setPreferredSize(new Dimension(width, height));
  }

  public void paintComponent(Graphics g) {
    g.drawImage(image, 0, 0, width, height, null);
  }


}
0
 
jc31415Author Commented:
That's exactly what I wanted!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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