How to Go About Adding an Image into a Java Project With Eclipse

How do I go about adding an image into a java project with eclipse?

Place the image in a source folder, not a regular folder. That is: right-click on project -> New -> Source Folder. Place the image in that source folder. Then:

InputStream input = classLoader.getResourceAsStream("image.jpg");

Note that the path is omitted. That's because the image is directly in the root of the path. You can add folders under your source folder to break it down further if you like. Or you can put the image under your existing source folder (usually called src).

adding image to eclipse web project

You should in general try to avoid absolute paths. Specifically I believe from a normal web project you should try something like:

<img border="0" src="images/network.jpg"
alt="Pulpit rock" width="304" height="228">

Which works like a charm for me.

Importing Images Into Eclipse

  1. From the main menu bar,
  2. select command link File > Import.... The Import wizard opens.
    Select General > File System and click Next.
  3. Click the Browse button on the next page of the wizard to select the
    directories from which you would like to add the resources.
  4. In the import selection panes, use the following methods to select
    exactly the resources you want to add:

    4.1 Expand the hierarchies in the left pane and select or clear the checkboxes that represent the folders in the selected directory. Then in the right pane, select or clear checkboxes for individual files.

    4.2 Click Filter Types to filter the current selection for files of a specific type.

    4.3 Click Select All to select all resources in the directory, then go through and deselect the ones that you do not want to add.

    4.4 Click Deselect All to deselect all resources in the directory, then go through and choose individual resources to add.

  5. Specify the Workbench project or folder that will be the import
    destination.

  6. When you have finished specifying your import options, click
    Finish.

Reference: eclipse

Adding images to eclipse java project using ImageIcon(getClass().getResource()

Make sure image file is present at correct location. It should be under src/images folder.

You can try any one based on image location.

// Read from same package 
ImageIO.read(getClass().getResourceAsStream("folder63.png"));

// Read from images folder parallel to src in your project
ImageIO.read(new File("images/folder63.jpg"));

// Read from src/images folder
ImageIO.read(getClass().getResource("/images/folder63.png"))

// Read from src/images folder
ImageIO.read(getClass().getResourceAsStream("/images/folder63.png"))

Read more...

It's worth reading Java Tutorial on Loading Images Using getResource

Adding an image into a GUI made with Eclipse

An Icon is not a component. You need to add the Icon to a component like a JLabel:

ImageIcon image = new ImageIcon(getClass().getResource("EXTS.png"));
//JPanel.add(image, BorderLayout.NORTH);
JPanel.add(new JLabel(image), BorderLayout.NORTH);

Java - How to add an image to resource

You should use getResource to load images or whatever from resource folder.

For example:

String pathToImage = "res/Gold_coin.png";
ImageIcon myIcon = new ImageIcon(getClass().getClassLoader().getResource(pathToImage));

or with all project path: nameOfProject/res/Gold_coin.png.

How to place image in source folder and use it in Eclipse

Generally eclipse starts jvm with current directory from base of the project so place it on base of the project to read it with existing code

Java Eclipse upload image to image folder within package

Hope that this helps answer your question :)

// Choose file
JFileChooser fc = new JFileChooser();
int result = fc.showOpenDialog(null);

// Make sure that a file was chosen, else exit
if (result != JFileChooser.APPROVE_OPTION) {
System.exit(0);
}

// Get file path
String path = fc.getSelectedFile().getAbsolutePath();

// Create folder "images" (variable success will be true if a folder was created and false if it did not)
File folder = new File("images");
boolean success = folder.mkdir();
// Get the destination of the folder and the new image (image.jpg will be the new name)
String destination = folder.getAbsolutePath() + File.separator + "img.jpg";

try {
// Copy file from source to destination
FileChannel source = new FileInputStream(path).getChannel();
FileChannel dest = new FileOutputStream(destination).getChannel();
dest.transferFrom(source, 0, source.size());

// Close shit
source.close();
dest.close();

System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}


Related Topics



Leave a reply



Submit