Hi,

I have a problem with painting a bufferedImage on the screen. The image is
simply created from an array of shorts that are used to create a
databuffer which together with a componentsamplemodel are used to create a
WritableRaster.  This raster is then used with a colorcomponentmodel to
create the actual bufferedimage.

When i save the created image as a JPEG, it saves fine. i.e. all pixels
are correct.  However when i try to display the image on the screen using
the paintComponent method, only one pixel is displayed - the one who's
value coressponds to the first element in the array of shorts.

Please Help!!

My code is below and is also attached:

Thanks

Raf


import java.awt.*;
import java.awt.color.*;
import java.awt.image.*;
import java.awt.image.renderable.ParameterBlock;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
import java.io.*;
import com.sun.image.codec.jpeg.*;

public class MakeImage extends JComponent {

    BufferedImage bi = createGrayscaleImage(4, 4 , 1, new short[]
{0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1});



    public BufferedImage createGrayscaleImage( int imageWidth, int
imageHeight, int imageDepth, short[] data) {

        ComponentColorModel ccm = new ComponentColorModel(
                                    ColorSpace.getInstance(
ColorSpace.CS_GRAY ),
                                    new int[] {imageDepth},
                                    false, false, Transparency.OPAQUE,
                                    DataBuffer.TYPE_USHORT);

        ComponentSampleModel csm = new ComponentSampleModel(
                                        DataBuffer.TYPE_USHORT,
                                        imageWidth, imageHeight, 1,
                                        imageWidth, new int[] {0});

        DataBuffer dataBuf = new DataBufferUShort( (short[])data,
imageWidth);

        WritableRaster wr = Raster.createWritableRaster( csm, dataBuf,
null);
        System.out.println(wr.getDataBuffer().getDataType());
        Hashtable ht = new Hashtable();
        ht.put( "owner", "Rafqat Aftab");

        return new BufferedImage(ccm, wr, true, ht);
    }

        public void  save() {

            try {

                ByteArrayOutputStream boutstream = new
ByteArrayOutputStream();
                JPEGImageEncoder enc =
JPEGCodec.createJPEGEncoder(boutstream);
                enc.encode(bi);
                FileOutputStream fimage = new FileOutputStream(new
File("university-crest.jpg"));
                boutstream.writeTo(fimage);
                fimage.close();

            }  catch (Exception e) { System.out.println(e); }


        }





        public void paintComponent(Graphics g) {

            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D)g;


            g2d.drawRenderedImage(bi,null);

        }


         public static void main (String args[]) {

            MakeImage c = new MakeImage();
            c.save();
            JFrame f = new JFrame();
            f.getContentPane().setLayout(new FlowLayout());
            f.setSize(1000,1000);
            f.getContentPane().add(c);
            f.pack();
            f.show();
            f.repaint();

        }



}

Thanks

Raf



import java.awt.*;
import java.awt.color.*;
import java.awt.image.*; 
import java.awt.image.renderable.ParameterBlock;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
import java.io.*;
import com.sun.image.codec.jpeg.*;

public class MakeImage extends JComponent {
    
    BufferedImage bi = createGrayscaleImage(4, 4 , 1, new short[] 
{0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1});
   
    
    
    public BufferedImage createGrayscaleImage( int imageWidth, int imageHeight, int 
imageDepth, short[] data) {
        
        ComponentColorModel ccm = new ComponentColorModel(
                                    ColorSpace.getInstance( ColorSpace.CS_GRAY ), 
                                    new int[] {imageDepth}, 
                                    false, false, Transparency.OPAQUE,
                                    DataBuffer.TYPE_USHORT);
                                
        ComponentSampleModel csm = new ComponentSampleModel(
                                        DataBuffer.TYPE_USHORT,
                                        imageWidth, imageHeight, 1, 
                                        imageWidth, new int[] {0});

        DataBuffer dataBuf = new DataBufferUShort( (short[])data, imageWidth);
       
        WritableRaster wr = Raster.createWritableRaster( csm, dataBuf, null);
        System.out.println(wr.getDataBuffer().getDataType());
        Hashtable ht = new Hashtable();
        ht.put( "owner", "Rafqat Aftab");
              
        return new BufferedImage(ccm, wr, true, ht); 
    }
    
        public void  save() {
            
            try {
        
                ByteArrayOutputStream boutstream = new ByteArrayOutputStream();
                JPEGImageEncoder enc = JPEGCodec.createJPEGEncoder(boutstream);
                enc.encode(bi);
                FileOutputStream fimage = new FileOutputStream(new 
File("university-crest.jpg"));
                boutstream.writeTo(fimage);
                fimage.close();
        
            }  catch (Exception e) { System.out.println(e); }
        
            
        }
        
        

    
        
        public void paintComponent(Graphics g) {
            
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D)g;
            
            
            g2d.drawRenderedImage(bi,null);

        }
        
        
         public static void main (String args[]) {
 
            MakeImage c = new MakeImage();
            c.save();
            JFrame f = new JFrame();
            f.getContentPane().setLayout(new FlowLayout());
            f.setSize(1000,1000);
            f.getContentPane().add(c);
            f.pack();
            f.show();
            f.repaint();        
            
        }

    
    
}

Reply via email to