Java Swing - Graphics
TOPIC 04 –
WORKING WITH IMAGE FILES

 

 

LESSON NOTE

 

 

SUPPORTED IMAGES

 

Java supports JPG, GIF and PNG formats.  There are apparently ways to add support for other formats but this will not be covered in this lesson.

 

IMAGE LOCATION

 

The biggest frustration related to displaying images is figuring out where to place the image on the hard drive so that Java and find it. 

 

The answer is that you must place it in your project folder.  However, the definition of project folder depends on the IDE that you are using.

 

If you are using JCreator or another basic setup, then you have a folder that contains all your java and class files.  That is the folder.

 

If you are using Eclipse, you need to use the folder that contains the subfolders named src and bin.  Eclipse itself refers to it as the project folder.

 

TRY

 

In Java, when we want to access a file on the hard drive, we have to place that code inside of a try/catch statement.  The try section is executed.  If there is an error (or exception), then the program immediately switches to the catch section.

 

DISPLAYING AN IMAGE

 

import javax.swing.JFrame;

 

public class ImageDisplayTester

{

   public static void main(String[] args)

   {

          JFrame jf = new JFrame();               

          jf.setSize(600,400);

          jf.setVisible(true);

          jf.setTitle("Java2D Shapes");  

 

          //Create my panel and add it to JFrame object

          ImageDisplayPanel pan = new ImageDisplayPanel();

          jf.add(pan);

   }

}

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

 

import javax.imageio.ImageIO;

import javax.swing.JPanel;

 

public class ImageDisplayPanel extends JPanel

{

   public void paint(Graphics g)

   {

      Graphics2D g2D = (Graphics2D)g;      

 

       BufferedImage img = null;

       try

       {

          img = ImageIO.read(new File("sensLogo.png"));  //image in project folder (Eclipse)

          g2D.drawImage(img, 0, 0, null);

       }   

       catch (IOException e)

       {

          System.out.println("Error");

          e.printStackTrace();  //Optional - Shows the location of the error issue.

       }

   }

}

 

WRITING AN IMAGE TO FILE

 

This process is very simple.  It only requires a few lines of code.  However, for the example, we will also read and file and display it to screen.  Notice the comments in the code  that make it clear what is new code and what was already there before.

 

import javax.swing.JFrame;

 

public class ImageDisplayWriteTester

{

   public static void main(String[] args)

   {

          JFrame jf = new JFrame();               

          jf.setSize(600,400);

          jf.setVisible(true);

          jf.setTitle("Java2D Shapes");  

 

          //Create my panel and add it to JFrame object

          ImageDisplayWritePanel pan = new ImageDisplayWritePanel();

          jf.add(pan);

   }

}

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

 

import javax.imageio.ImageIO;

import javax.swing.JPanel;

 

public class ImageDisplayWritePanel extends JPanel

{

   public void paint(Graphics g)

   {

       Graphics2D g2D = (Graphics2D)g;     

 

       //=================================================

       //PART 1 - DISPLAYING THE IMAGE (nothing new here)

       //=================================================

      

       BufferedImage img = null;

       try

       {

          img = ImageIO.read(new File("sensLogo.png"));  //img in project folder

          g2D.drawImage(img, 0, 0, null);

       }   

       catch (IOException e)

       {

          System.out.println("Error in Reading Section");

          e.printStackTrace();  //Optional - Shows the location of the error issue.

       }

         

       //=================================================

       //PART 2 - WRITING THE IMAGE TO FILE

       //=================================================

         

       try

       {

          File im = new File("newFile.png");

          ImageIO.write(img, "png", im);   

       }

       catch (IOException e)

       {

          System.out.println("Error in Writing Section");

          e.printStackTrace();  //Optional - Shows the location of the error issue.

       }

   }

}


IMAGE TRANSFORMATIONS

 

We can use AffineTransform objects to apply transformations to images before they are drawn.  This works in the exact way as it did before.

 

import javax.swing.JFrame;

 

public class ImageTransformationTester

{

   public static void main(String[] args)

   {

          JFrame jf = new JFrame();               

          jf.setSize(600,400);

          jf.setVisible(true);

          jf.setTitle("Java2D Shapes");  

 

          //Create my panel and add it to JFrame object

          ImageTransformationsPanel pan = new ImageTransformationsPanel();

          jf.add(pan);

   }

}

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.geom.AffineTransform;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

 

import javax.imageio.ImageIO;

import javax.swing.JPanel;

 

public class ImageTransformationsPanel extends JPanel

{

   public void paint(Graphics g)

   {

       Graphics2D g2D = (Graphics2D)g;

         

       BufferedImage img = null;

       try

       {            

          //Draw the image normally.

          img = ImageIO.read(new File("sensLogo.png"));  //img in project folder (Eclipse)

          g2D.drawImage(img, 0, 0, null);

             

          //Create a transformation to translate (move) anything to the right by an amount

          //equal to the width of the image.

             

          AffineTransform at = new AffineTransform();

          at.translate(img.getWidth(), 0);

          g2D.setTransform(at);

             

           //Draw image again.

           g2D.drawImage(img, 0, 0, null);

         }  

         catch (IOException e)

         {

            System.out.println("Error");

            e.printStackTrace();

         }

    }

}