Friday, December 9, 2016

Internal resources in Eclipse

Files can be opened from storage devices, but they need a specific path and drive letter, sometimes.  If you are planning to create a runnable jar file created from your project, it is better to access the resources internally from an internal resource path and include them in the runnable jar file.

This simple example demonstrates the process.

1. Create two images with 600x400 dimensions and name them one.jpg and two.jpg.  Store them on your C drive's temp directory for now.
2. Follow the image below to create a new resource folder (1-8) in your project.
3. Import (9) the two image files into the newly created res resource folder.



import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


@SuppressWarnings("serial")
public class FirstGuiApp extends JPanel{
BufferedImage img;

//First try - load images from your hard drive
//String img1 = "c:\\temp\\one.jpg";
//String img2 = "c:\\temp\\two.jpg";

//Second try - load images from internal resource, comment out first step

/*Add project folder res ( add res path to project properties->Build Path->Source)
 * Then, import two jpg images 600x400 named one.jpg and two.jpg
 * */

String img1 = "one.jpg";
String img2 = "two.jpg";
boolean evenClick = false;

public FirstGuiApp(){
setSize(600,400);
setVisible(true);
loadImage(img1);

this.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me){
if(evenClick){
    loadImage(img1);
    evenClick=false;
    }
else{
    loadImage(img2);
    evenClick=true;
    }
updateUI();
}

});
}//end of FirstGuiApp

private void loadImage(String str){
try{
       //img=ImageIO.read(FirstGuiApp.class.getResource(str)); //one way to load internal resoure
       img=ImageIO.read(getClass().getResource(str)); //preferred way of loading internal resource
       //img=ImageIO.read(new File(str)); //use this with first step to load image from hard drive
     }
catch(IOException e){   e.printStackTrace();  }
}//end of loadImage method

protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(img, 0, 0, 600, 400, this);

}//end of paintComponent

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
              public void run(){
                         JFrame frm = new JFrame();
                         frm.setSize(620,465);
                         frm.setVisible(true);
                         frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                         frm.add(new FirstGuiApp());
                         frm.setTitle("My GUI Application");
                         }//end of run
              }//end of runnable
          );
     }//end of main
}//end of class

Run the program.  The program window should show your image one.jpg and when you click on the image the image two.jpg is loaded alternating between the images. If it does not work, uncomment

//String img1 = "c:\\temp\\one.jpg";
//String img2 = "c:\\temp\\two.jpg";


and 

//img=ImageIO.read(new File(str)); //use this with first step to load image from hard drive

and comment out the following lines to load the images from your hard drive directly for trouble shooting.

String img1 = "one.jpg";
String img2 = "two.jpg";

img=ImageIO.read(getClass().getResource(str)); //preferred way of loading internal resource
 
After you have tested your application, you can then create the runnable jar file.


No comments:

Post a Comment