PDA

View Full Version : java ImageIO


tricolaire
08-22-2006, 01:47 AM
I am trying to write a program which draws an image on the screen, allows the user to draw on top of that image, and then saves a jpg of what the user drew, MINUS THE ORIGINAL IMAGE.

here is the source:

//
// image_tracer.java
// image tracer
//
// Created by Erik Schmidt on 8/21/06.
// Copyright (c) 2006 __MyCompanyName__. All rights reserved.
//
import java.util.*;
import javax.imageio.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.io.*;
import java.awt.image.*;
import java.util.ArrayList;
import java.awt.Graphics;
import java.net.*;
import java.applet.*;

class MyPanel extends JPanel {

Graphics save;
Image blank;
Image face;
ArrayList x;
ArrayList y;
ImageObserver ob;

private class MListen implements MouseMotionListener {

public void mouseDragged(MouseEvent e){

x.add(new Integer(e.getX()));
y.add(new Integer(e.getY()));

repaint();

}

public void mouseMoved(MouseEvent e){
//do nothing
}

}

public MyPanel(Image f){

x=new ArrayList();
y=new ArrayList();

IMAGE b=new IMAGE();
blank=b.run("/Users/erikschmidt90/Sites/blank.jpg");

face=f;

addMouseMotionListener(new MListen());

x.add(new Integer(-30));
y.add(new Integer(-30));

}

public void paintComponent(Graphics g){

g.drawImage(face,0,0,ob);
save=createImage(500,500).getGraphics();
save.drawImage(blank,0,0,null);
g.setColor(Color.black);
save.setColor(Color.black);
g.drawImage(face,0,0,ob);

for(int i=0;i<x.size();i++){

save.fillOval(((Integer)x.get(i)).intValue(),((Integer)y.get(i)).intValue(),20,20);
g.fillOval(((Integer)x.get(i)).intValue(),((Integer)y.get(i)).intValue(),20,20);

}

}

public Graphics getSave(){

return save;

}

}

class WListen extends WindowAdapter {

public void WindowClosed(WindowEvent e){

System.exit(0);

}

}

class MainFrame extends JFrame {

MyPanel panel;
JButton submit;
JPanel main;

public MainFrame(Image face){

super("Image Tracer");
setSize(750,750);

panel=new MyPanel(face);
submit=new JButton("Save");
submit.addActionListener(new MyActionListener());
main=new JPanel(new BorderLayout());
main.add(panel,BorderLayout.CENTER);
main.add(submit,BorderLayout.SOUTH);

setContentPane(main);

}

private class MyActionListener implements ActionListener {

public void actionPerformed(ActionEvent e){

Object source=e.getSource();

if(source==submit){

BufferedImage w=new BufferedImage(500,500,BufferedImage.TYPE_INT_RGB);
Graphics g=w.getGraphics();
g=panel.getSave().create();
try{

ImageIO.write(w,"jpg",new File("Output.jpg"));

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

}

}

}

}

class IMAGE {

public Image run(String filename){

return Toolkit.getDefaultToolkit().createImage(filename);

}

}

public class image_tracer {

public static void main (String args[]) {

IMAGE image=new IMAGE();
MainFrame frame=new MainFrame(image.run("/Users/erikschmidt90/Sites/face.jpg"));
frame.addWindowListener(new WListen());
frame.show();

}
}

the line about ImageIO only draws an all black image, not what the user drew

how do I get the second graphics context to the file, regardless of the format?

Kura_kai
08-22-2006, 09:21 PM
If I can remeber right ImageIO can only take one image...Not sure if it does or doesn't but you might want to just combine them together before you write it. Also I believe Java has issues with writing .jpgs. This I believe was from 1.5 or before not sure about the new 1.6

tricolaire
08-22-2006, 09:49 PM
how do you mean?

I tried writing it as a png, but that doesn't work either

is there a way to convert a Graphics into an Image or BufferedImage?