Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Images (read/write, drawing)

  1. #1
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Images (read/write, drawing)

    Here's a quick and dirty tutorial on how to draw images in Java, then save them to the file system.

    Drawing an image from scratch and saving to a png:
    int width = 1024;
    int height = 768;
    // we're going to use RBG coloring. There are a bunch of different types, though
    BufferedImage image= new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics g = image.getGraphics();
    // now drawing is just like painting a component
    g.setColor(new Color(0.2f, 0.4f, 0.2f));
    // make the background a slight shade of green
    g.fillRect(0, 0, width, height);
    g.setColor(new Color(1.0f, 0.0f, 0.0f));
    // draw a red circle
    g.drawCircle(0, 0, width, height);
    // now to save the image
    try
    {
         ImageIO.write(image, "png", new File("test.png"));
    }
    catch (IOException exception)
    {
         exception.printStackTrace();
    }

    It is possible to save to multiple file types, for a list of available formats call ImageIO.getWriterFormatNames().

    Reading an image in is as easy as calling the read() method of ImageIO:
    try
    {
         BufferedImage myImage = ImageIO.read(new File("test.png"));
    }
    catch(IOException e)
    {
         e.printStackTrace();
    }

    Happy drawing!

  2. The Following User Says Thank You to helloworld922 For This Useful Post:

    JavaPF (June 1st, 2010)


  3. #2
    Junior Member
    Join Date
    Sep 2011
    Location
    Italy
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Images (read/write, drawing)

    very funny!

    import java.io.*;
    import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;
    import java.awt.Graphics;
    import java.awt.Color;
     
    public class draw {
    	public static void main(String[] args) {
     
    		int width = 200;
    		int height = 200;
    		int circlew = width;
    		int circleh = height;
    		boolean choser = true;
    		int x = width/2;
    		int y = height/2;
     
    		BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    		Graphics g = bi.getGraphics();
     
    		g.setColor(new Color(30,30,30));
    		g.fillRect(0,0, width, height);
    		while(circlew>=25 && circleh>=25){
    			if(choser) {
    				g.setColor(Color.WHITE);
    			}
    			else {
    				g.setColor(Color.black);
    			}
    			if(circlew==25 && circleh==25) {
    				g.setColor(Color.red);
    			}
    			g.fillOval(x-circlew/2,y-circleh/2, circlew, circleh);
    			choser = choser^true;
    			circlew = circlew-25;
    			circleh = circleh-25;
    		}		
     
    		save(bi);
     
    	}
     
    	public static void save(BufferedImage image) {
    		try{
    			ImageIO.write(image, "png", new File("flag.png"));
    		}
    		catch(Exception e) {
    			System.out.println("errore");
    		}
    	}
    }

    flag.png

Similar Threads

  1. Drawing
    By toxikbuni in forum Java Theory & Questions
    Replies: 0
    Last Post: April 20th, 2010, 02:43 PM
  2. Drawing circles with smoother lines?
    By tabutcher in forum Java Theory & Questions
    Replies: 4
    Last Post: April 18th, 2010, 10:12 AM
  3. keeping an drawing centered in Graphics
    By dvsumosize in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 27th, 2010, 11:26 PM
  4. How to Write and Read Binary Data
    By neo_2010 in forum File Input/Output Tutorials
    Replies: 3
    Last Post: January 4th, 2010, 02:38 PM
  5. Drawing image on JPanel from another frame
    By jeryslo in forum AWT / Java Swing
    Replies: 3
    Last Post: December 8th, 2009, 04:01 PM