Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-16-2007, 11:52 PM
Member
 
Join Date: Jul 2007
Posts: 26
romina is on a distinguished road
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.

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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2007, 06:21 AM
Member
 
Join Date: Jul 2007
Posts: 40
trill is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 02:31 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org