Results 1 to 2 of 2
  1. #1
    romina is offline Member
    Join Date
    Jul 2007
    Posts
    26
    Rep Power
    0

    Default Help with ImageIO

    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.

    Java Code:
    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?Thanks

  2. #2
    trill is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default

    If I can remember 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

    Greetings.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •