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