get an image displayed by a component

Is there a way to retrieve an image displayed by a component which directly extends JComponent ?

For example, the following class will draw two images on a component

public class GetImagDisplayed extends Jccomponent
{

  Image image01, image02;

  public void init()
  {
        image01=new ImageIcon("image01.gif").getImage();
        image02=new ImageIcon("image02.gif").getImage();            
        
  }

  public void paintComponent(Graphics g) 
  {
        Graphics2D g2 = (Graphics2D)g; 
        g2.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_SPEED); 
        g2.drawImage(image01,100,100,this);
        g2.drawImage(image02,500,500,this);
  }

}

How can I get an image that has two images displayed by the component directly from the component
without using an off screen buffer ? Is there a getImage method to get the image displayed by the
component ?

Jay

Two ways:

  1. Use java.awt.Robot to take a screenshot of the area in question.

  2. Create a BufferedImage and call myComp.paint(image.createGraphics()).

If it’s something like a whiteboard where you forgot what’s been drawn, then the first option works best. However, you really should be using a backbuffer in those sorts of circumstances. Another window could overlay yours and wipe out the information. Of course, it isn’t an issue if you’re full screen…

Thanks for the information. But I have another non-related issue. When I tried to save the image as a jpg after I retrieved the image from the component, it appears that the color actually gets altered. What I was trying to do was to load a jpg image and then draw some lines on it, and then try to save into a file.

The color looks right on the screen but gets changed when I try to save the image as jpg:

public void saveImage()
{
BufferedImage background=new BufferedImage(screenW,screenH,BufferedImage.TYPE_INT_ARGB);
this.paint(background.createGraphics());
RenderedImage ri=(RenderedImage) background;
try{
File file=new File(“c:\ex14\images\newimage.jpg”);
ImageIO.write(ri,“jpg”,file);
} catch (IOException e){}

}

Jay

How is it changed? Remember that JPEG is a lossy format.

It appears that the color has black color mixed into it. All the red and blue lines become darker, almost black color, when the image is reloaded back. It only happens to the scribble, line and outline of shapes. It appears to be fine with filled shapes.

Maybe the problem is not caused by saving the image but by reloading it back. I just do not know what’s wrong with it. Here is the part of code that does the drawing, painting, saving and reloading:

// initialize drawing area

public void initGfx()
{
drawCanvas= new BufferedImage(screenW,screenH,BufferedImage.TYPE_INT_ARGB);
draw_g2=drawCanvas.createGraphics();
}

public void mousePressed(MouseEvent e)
{
canvasX = (short)e.getX();
canvasY = (short)e.getY();
video_g2=(Graphics2D) this.getGraphics(); //screen display
video_g2.setColor(Color.red);
draw_g2.setColor(Color.red);

  //draw on both screen and offscreen buffer
  video_g2.drawLine(canvasX,canvasY,canvasX,canvasY);
  draw_g2.drawLine(canvasX,canvasY,canvasX,canvasY);                        

}
public void mouseDragged(MouseEvent e)
{
int x,y;

  x=e.getX();
  y=e.getY();

  video_g2.drawLine(canvasX,canvasY,x,y);
  draw_g2.drawLine(canvasX,canvasY,x,y);
        
  canvasX=x;
  canvasY=y;

}
public void mouseReleased(MouseEvent e)
{
video_g2.drawLine(e.getX(),e.getY(),e.getX(),e.getY());
draw_g2.drawLine(e.getX(),e.getY(),e.getX(),e.getY());

  video_g2.setPaintMode(); 
  repaint(); // copy offscreen buffer to screen

}

// draw on the component

public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
//g2.setComposite(AlphaComposite.SrcAtop);
g2.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_SPEED);
g2.drawImage(drawCanvas,0,0,screenW,screenH,this);
}

// save image
public void saveImage()
{
ImageIOToolkit.writeImage(drawCanvas,“c:\ex14\images\newimage.jpg”);
}

// load back the image saved

public void loadImage
{
File file;
BufferedImage background;

  fpc.setAccessory(new ImagePreview(fpc));
  int returnVal = fpc.showDialog(frame,"Select Image");
  if (returnVal == JFileChooser.APPROVE_OPTION) 
  {
        file = fpc.getSelectedFile();
        if (file.isFile())
        {                  
              background=ImageIOToolkit.getBufferedImage(file);
              draw_g2.drawImage(background,0,0,screenW,screenH,this);
                    
              repaint();      
        }
  }

}

Jay

Like swpalmer said. Its probably just the compression taking place in the JPEG. Try saving out to PNG or GIF and checking the results.

You could check if its saving or loading by loading the image into a standard paint package.

Kev

Thanks guys. I tried to save the image as png format and it works fine. I guess that it is the issue with JPG.

Jay