Results 1 to 4 of 4
  1. #1
    FaintSmile is offline Member
    Join Date
    Jul 2010
    Rep Power

    Exclamation How to save the line that i draw as a picture.

    i was working on a picture where i need to draw some lines over the picture and save it as a new picture. my problem now is i only able to save the picture using the drawimage method while i cant save the lines that i draw using the drawline method. is there any ways that can save both the picture that i read and the line that i draw into a new picture ? below is part of my code.

    ================================================== =======

    BufferedImage grid1;
    File file1 = new File("C:/apple.jpg)
    grid1 =;

    BufferedImage bi = new BufferedImage(300, 300, BufferedImage.TYPE_INT_ARGB);

    Graphics2D ig2 = bi.createGraphics();
    ig2.drawImage(grid1, 0, 0, this);
    ig2.drawLine(96, 127, 122, 127);
    ig2.drawLine(122, 127, 135, 127);

    File outputfile = new File("D:/New Folder/c.gif");
    ImageIO.write(bi, "gif", outputfile);

    ================================================== ========

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Rep Power


    I don't think you can save it as a .gif file. Try saving it as a .jpg file.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power


    What happens when you execute your code?
    Is the image written to disk?
    Are there errors?

  4. #4
    FaintSmile is offline Member
    Join Date
    Jul 2010
    Rep Power


    I manage to solve the problem after looking into several examples on the web.
    Below are the example coding that solve my problem... Hope it can help someone that face the same problem as i do :):)

    U need to have a picture A.gif and B.gif in D:/New Folder/
    just get any .GIF and save it as A and B. B MUST be smaller then A in order to see B above of A.
    ================================================== ========

    import java.awt.*;
    import java.awt.image.*;
    import javax.imageio.ImageIO;
    import java.util.*;
    import java.sql.*;

    import java.awt.Graphics2D;

    public class IMG{
    private static final int DEFAULT_IMAGE_TYPE = BufferedImage.TYPE_INT_RGB;
    public static void main(String arg[])

    File file = new File("D:/New Folder/A.gif");
    File fileNode = new File("D:/New Folder/B.gif");

    Image imageNode =;

    Image image =;
    BufferedImage cpimg = bufferImage(image,BufferedImage.TYPE_INT_RGB);
    Graphics g = cpimg.createGraphics();
    Font fnt=new Font("Impact",1,14);
    Color fntC = new Color(0,100,0);
    g.drawString("This IS Write BY Code",53,15);
    g.drawLine(96, 127, 122, 127);
    g.drawImage(imageNode, 90, 121,null);

    File f1 = new File("D:/New Folder/NewImage.gif");
    ImageIO.write(cpimg, "gif", f1);
    catch(Exception e){

    public static BufferedImage bufferImage(Image image, int type)
    BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    Graphics2D g = bufferedImage.createGraphics();
    g.drawImage(image, null, null);
    return bufferedImage;

    ================================================== ========

Similar Threads

  1. Draw line
    By janes in forum Java 2D
    Replies: 6
    Last Post: 03-25-2010, 11:48 PM
  2. Draw more than one line on run time
    By aiman in forum Java Applets
    Replies: 3
    Last Post: 12-10-2009, 03:44 AM
  3. When do we draw the line for loose coupling.
    By h8alfred in forum Advanced Java
    Replies: 0
    Last Post: 03-27-2009, 04:05 AM
  4. How to Draw line in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:08 PM
  5. How to draw a thick line
    By johnt in forum Java 2D
    Replies: 1
    Last Post: 05-31-2007, 04:27 PM

Posting Permissions

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