How do I add an image to a JButton - java

I am trying to add an image to a JButton and I'm not sure what I'm missing. When I run the following code the button looks exactly the same as if I had created it without any image attribute. Water.bmp is in the root of my project folder.
ImageIcon water = new ImageIcon("water.bmp");
JButton button = new JButton(water);
frame.add(button);

I think that your problem is in the location of the image. You shall place it in your source, and then use it like this:
JButton button = new JButton();
try {
Image img = ImageIO.read(getClass().getResource("resources/water.bmp"));
button.setIcon(new ImageIcon(img));
} catch (Exception ex) {
System.out.println(ex);
}
In this example, it is assumed that image is in src/resources/ folder.

#Rogach
and you may like to add:
// to remote the spacing between the image and button's borders
button.setMargin(new Insets(0, 0, 0, 0));
// to add a different background
button.setBackground( ... );
// to remove the border
button.setBorder(null);

It looks like a location problem because that code is perfectly fine for adding the icon.
Since I don't know your folder structure, I suggest adding a simple check:
File imageCheck = new File("water.bmp");
if(imageCheck.exists())
System.out.println("Image file found!")
else
System.out.println("Image file not found!");
This way if you ever get your path name wrong it will tell you instead of displaying nothing. Exception should be thrown if file would not exist, tho.

You put your image in resources folder and use follow code:
JButton btn = new JButton("");
btn.setIcon(new ImageIcon(Class.class.getResource("/resources/img.png")));

public class ImageButton extends JButton {
protected ImageButton(){
}
#Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Image img = Toolkit.getDefaultToolkit().getImage("water.bmp");
g2.drawImage(img, 45, 35, this);
g2.finalize();
}
}
OR use this code
class MyButton extends JButton {
Image image;
ImageObserver imageObserver;
MyButtonl(String filename) {
super();
ImageIcon icon = new ImageIcon(filename);
image = icon.getImage();
imageObserver = icon.getImageObserver();
}
public void paint( Graphics g ) {
super.paint( g );
g.drawImage(image, 0 , 0 , getWidth() , getHeight() , imageObserver);
}
}

I did only one thing and it worked for me .. check your code is this method there ..
setResizable(false);
if it false make it true and it will work just fine ..
I hope it helped ..

buttonB.setIcon(new ImageIcon(this.getClass().getResource("imagename")));

//paste required image on C disk
JButton button = new JButton(new ImageIcon("C:water.bmp");

This code work for me:
BufferedImage image = null;
try {
URL file = getClass().getResource("water.bmp");
image = ImageIO.read(file);
} catch (IOException ioex) {
System.err.println("load error: " + ioex.getMessage());
}
ImageIcon icon = new ImageIcon(image);
JButton quitButton = new JButton(icon);

For example if you have image in folder res/image.png you can write:
try
{
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("image.png");
// URL input = classLoader.getResource("image.png"); // <-- You can use URL class too.
BufferedImage image = ImageIO.read(input);
button.setIcon(new ImageIcon(image));
}
catch(IOException e)
{
e.printStackTrace();
}
In one line:
try
{
button.setIcon(new ImageIcon(ImageIO.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("image.png"))));
}
catch(IOException e)
{
e.printStackTrace();
}
If the image is bigger than button then it will not shown.

eclipse example:
After reading the above, it still took me more research to understand, where and how to place image resources, using eclipse.
The outcome: in eclipse you need to store images underneath any "Source Folder" (such as "src") or below in a "Folder".
You create both by right-clicking on your project, "New"->"Source Folder" or "New"->"Folder". Any folder name is fine. Examples for "<Source Folder>/<image Folder>" are "src/images" or "resource/img".
Here's some fully functioning sample code, which expects two button images, "Button-Image-Up.png" and "Button-Image-Down.png" in folder "images":
import javax.swing.JDialog;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
#SuppressWarnings("serial")
public class ImageButtonDlg extends JDialog {
// define button images and fall-back text
private static gstrTxt="<Button Fall-back Text>"; // in case image does not load
private static String gstrImgUp="Button-Image-Up.png"; // regular image
private static String gstrImgDown="Button-Image-Down.png"; // button pressed image
private static String gstrImgFolder="images"; // image folder, "/images" is also fine
public static void main(String[] args) {
ImageButtonDlg dialog = new ImageButtonDlg();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
}
/**
* Create the dialog.
*/
public ImageButtonDlg() {
initializeDialog();
}
/**
* Create a new ImageIcon from an image resource.
* The input can be:
* - an image file name, e.g. <image.png>
* - a relative path, e.g. <image/image.png>
* - an absolute path, e.g. </home/user/dev/image.png>
* In eclipse, images can be stored below any project "Source folder",
* such as "src".
* ├── src
* │   ├── img1.png
* │   └── images
* │      └── img2.png
* ├── resources
* │   ├── img3.png
* │   └── images
* │      └── img4.png
* └── img5.png
* In above example img1.png to img4.png are found by
* image file name or relative path
* However, img5 is stored in the project root folder and
* needs an absolute path.
*
* #param strImagePath - image filename, absolute path or relative path
* #return new ImageIcon or 'null' if load fails
*/
public static ImageIcon getResourceImageIcon(String strFilepath) {
ClassLoader loader = null;
URL url = null;
Image img=null;
ImageIcon imgIcon=null;
loader = ImageButtonDlg.class.getClassLoader();
System.out.println(loader.toString());
try { // file location: <a relative path>
url = loader.getResource("images/"+strFilepath);
if(url==null) {
System.out.printf("ImageButtonDlg.class.getResource(%s)=>null\n", "images/"+strFilepath);
}
} catch (Exception e0) {
e0.printStackTrace();
}
if(url==null) {
try { // file location: <image filename>
url = loader.getResource(strFilepath);
if(url==null) {
System.out.printf("Util.class.getResource(%s)=>null\n", strFilepath);
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
try {
img = ImageIO.read(url);
} catch (Exception e2) {
e2.printStackTrace();
System.exit(0);
}
if (img!=null){
imgIcon = new ImageIcon(img);
}
return imgIcon;
}
/**
* Create JButton with Image
* In case Image load fails, we create a JButton with text
* #param strImgPath - path to Image
* #param strAltText - fall-back text
* #return
*/
public static JButton getNewJButtonWithImageIcon(String strImgPath, String strAltText) {
JButton btnReturn = null;
ImageIcon imgIcon = getResourceImageIcon(strImgPath);
if (imgIcon!=null){
btnReturn = new JButton(imgIcon);
}
else {
btnReturn = new JButton(strAltText);
}
return btnReturn;
}
/**
* Image button was pressed, display a message box
*/
private void actionImageButtonPressed() {
try {
JOptionPane.showMessageDialog(null, "Image Button was pressed", "Info", JOptionPane.INFORMATION_MESSAGE);
}
catch (Exception e) {
; // ignore
}
}
/**
* Initialize the dialog
* add a button panel and the image button to the window/content pane
*/
private void initializeDialog()
{
this.setTitle("Image Button Example");
this.setResizable(false);
this.setBounds(200, 200, 699, 601);
JPanel panelButton = new JPanel();
panelButton.setLayout(new FlowLayout(FlowLayout.RIGHT)); // all buttons in a row
getContentPane().add(panelButton, BorderLayout.SOUTH); // button pane at the bottom of the window
// create the Image Button
JButton btnImageButton = getNewJButtonWithImageIcon(gstrImgUp, gstrTxt);
btnImageButton.setToolTipText("<Explain what pressing the Button does>");
btnImageButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
actionImageButtonPressed();// The Action
}
});
// load button image when button clicked/released
btnImageButton.addMouseListener((MouseListener) new MouseAdapter() {
public void mousePressed(MouseEvent e) {
btnImageButton.setIcon(getResourceImageIcon(gstrImgDown));
}
public void mouseReleased(MouseEvent e) {
btnImageButton.setIcon(getResourceImageIcon(gstrImgUp));
}
});
btnImageButton.setActionCommand("ImageButtonAction");
// add button to button panel
panelButton.add(btnImageButton);
}
}
Have Fun!
Volker Fröhlich
P.S.:
Perhaps someone can share additional practical experience.
In eclipse "WindowBuilder Editor" such image buttons are shown, but they cannot be accessed - is there some workaround?
What is the NetBeans approach, if any different?

Related

How to load image from file [duplicate]

I am attempting to display an image file as soon as it is selected from a file chooser. The file chooser is restricted to .png and .jpg files with the selected files being stored in a variable of type File. To do this I have set up an ImageView, and I wish to set the image with this new file only problem is it is of type File not Image.
How can this be achieved? Code so far...
public void fileSelection(){
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Select Profile Picture");
fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("Image Files", "*.png", "*jpg"));
File selectedFile = fileChooser.showOpenDialog(null);
File selectedFileInput = selectedFile;
if(selectedFile != null) {
selectedFileOutput.setText("File selected: " + selectedFile.getName());
previewPicture.setImage();
} else {
selectedFileOutput.setText("Please select a profile picture...");
}
}
You can simply create an image with
Image image = new Image(selectedFile.toURI().toString());
and then place it in the ImageView:
previewPicture.setImage(image);
Other constructors offer more control over resources required for loading the image. If you want to force the image to be a certain size, you can resize it on loading, which will save memory if the user chooses a large image but you only want to display a scaled-down version. Additionally, loading a large image may take time, so you should not load it on the UI thread. The Image constructors taking string versions of URLs have options to automatically load the image in a background thread. The following forces the width and height to be both no more than 240 pixels (while maintaining the original aspect ratio), and loads the image in the background (thus not blocking the UI):
Image image = new Image(selectedFile.toURI().toString(),
240, // requested width
240, // requested height
true, // preserve ratio
true, // smooth rescaling
true // load in background
);
See the documentation for other available constructors.
You create image and set to the ImageView as follows
Image image = new Image(new FileInputStream(selectedFile));
previewPicture.setImage(image);
I adding a late answer to demonstrate different image-file loading alternatives:
import java.io.File;
import java.net.URL;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;
public class ImagesFromFile extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("Load Images From File");
primaryStage.setScene(new Scene(new ImagePanel().getPane()));
primaryStage.show();
}
public static void main(final String[] args) {
launch(args);
}
}
/**
<pre>
Folders structure:
-Project
| - src
| | - package
| | | - ImagePanel.java
| | | - red_dot.png
| |
| | - resources
| | - blue_dot.png
|
| - black_dot.png
</pre>
*/
class ImagePanel{
private Pane pane;
ImagePanel() {
try{
//not embedded resource. doesn't work when packaged in jar
File file = new File("black_dot.png");
Image imageFromProjectFolder = new Image(file.toURI().toString());//or new Image(new FileInputStream("black_dot.png"));
ImageView view1 = new ImageView(imageFromProjectFolder);
URL url = getClass().getResource("red_dot.png");
Image imageFromSourceFolder = new Image(url.openStream());
ImageView view2 = new ImageView(imageFromSourceFolder); //or new ImageView("/package/red_dot.png");
url = getClass().getResource("/resources/blue_dot.png");
Image imageFromReourceFolder = new Image(url.openStream());
ImageView view3 = new ImageView(imageFromReourceFolder); //or new ImageView("/resources/blue_dot.png");
pane = new TilePane(view1, view2, view3);
} catch (Exception ex) {ex.printStackTrace();}
}
Pane getPane(){
return pane;
}
}

How to load Image file to ImageView?

I am attempting to display an image file as soon as it is selected from a file chooser. The file chooser is restricted to .png and .jpg files with the selected files being stored in a variable of type File. To do this I have set up an ImageView, and I wish to set the image with this new file only problem is it is of type File not Image.
How can this be achieved? Code so far...
public void fileSelection(){
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Select Profile Picture");
fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("Image Files", "*.png", "*jpg"));
File selectedFile = fileChooser.showOpenDialog(null);
File selectedFileInput = selectedFile;
if(selectedFile != null) {
selectedFileOutput.setText("File selected: " + selectedFile.getName());
previewPicture.setImage();
} else {
selectedFileOutput.setText("Please select a profile picture...");
}
}
You can simply create an image with
Image image = new Image(selectedFile.toURI().toString());
and then place it in the ImageView:
previewPicture.setImage(image);
Other constructors offer more control over resources required for loading the image. If you want to force the image to be a certain size, you can resize it on loading, which will save memory if the user chooses a large image but you only want to display a scaled-down version. Additionally, loading a large image may take time, so you should not load it on the UI thread. The Image constructors taking string versions of URLs have options to automatically load the image in a background thread. The following forces the width and height to be both no more than 240 pixels (while maintaining the original aspect ratio), and loads the image in the background (thus not blocking the UI):
Image image = new Image(selectedFile.toURI().toString(),
240, // requested width
240, // requested height
true, // preserve ratio
true, // smooth rescaling
true // load in background
);
See the documentation for other available constructors.
You create image and set to the ImageView as follows
Image image = new Image(new FileInputStream(selectedFile));
previewPicture.setImage(image);
I adding a late answer to demonstrate different image-file loading alternatives:
import java.io.File;
import java.net.URL;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;
public class ImagesFromFile extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("Load Images From File");
primaryStage.setScene(new Scene(new ImagePanel().getPane()));
primaryStage.show();
}
public static void main(final String[] args) {
launch(args);
}
}
/**
<pre>
Folders structure:
-Project
| - src
| | - package
| | | - ImagePanel.java
| | | - red_dot.png
| |
| | - resources
| | - blue_dot.png
|
| - black_dot.png
</pre>
*/
class ImagePanel{
private Pane pane;
ImagePanel() {
try{
//not embedded resource. doesn't work when packaged in jar
File file = new File("black_dot.png");
Image imageFromProjectFolder = new Image(file.toURI().toString());//or new Image(new FileInputStream("black_dot.png"));
ImageView view1 = new ImageView(imageFromProjectFolder);
URL url = getClass().getResource("red_dot.png");
Image imageFromSourceFolder = new Image(url.openStream());
ImageView view2 = new ImageView(imageFromSourceFolder); //or new ImageView("/package/red_dot.png");
url = getClass().getResource("/resources/blue_dot.png");
Image imageFromReourceFolder = new Image(url.openStream());
ImageView view3 = new ImageView(imageFromReourceFolder); //or new ImageView("/resources/blue_dot.png");
pane = new TilePane(view1, view2, view3);
} catch (Exception ex) {ex.printStackTrace();}
}
Pane getPane(){
return pane;
}
}

Swing JMenuItem - Icons not showing up [duplicate]

I am having a error for my GUI. Trying to set title bar icon then be included in a Runnable JAR.
BufferedImage image = null;
try {
image = ImageIO.read(getClass().getClassLoader().getResource("resources/icon.gif"));
}
catch (IOException e) {
e.printStackTrace();
}
frame.setIconImage(image);
Here is the error I am getting:
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at GUI.<init>(GUI.java:39)
at GUI.main(GUI.java:351)
The image is in the correct directory which "resources" folder is the root of the
project file
First of all, change this line :
image = ImageIO.read(getClass().getClassLoader().getResource("resources/icon.gif"));
to this :
image = ImageIO.read(getClass().getResource("/resources/icon.gif"));
More info, on as to where lies the difference between the two approaches, can be found on this thread - Different ways of loading a Resource
For Eclipse:
How to add Images to your Resource Folder in the Project
For NetBeans:
Handling Images in a Java GUI Application
How to add Images to the Project
For IntelliJ IDEA:
Right-Click the src Folder of the Project. Select New -> Package
Under New Package Dialog, type name of the package, say resources. Click OK
Right Click resources package. Select New -> Package
Under New Package Dialog, type name of the package, say images. Click OK
Now select the image that you want to add to the project, copy it. Right click resources.images package, inside the IDE, and select Paste
Use the last link to check how to access this file now in Java code. Though for this example, one would be using
getClass().getResource("/resources/images/myImage.imageExtension");
Press Shift + F10, to make and run the project. The resources and images folders, will be created automatically inside the out folder.
If you are doing it manually :
How to add Images to your Project
How to Use Icons
A Little extra clarification, as given in this answer's first
code example.
QUICK REFERENCE CODE EXAMPLE(though for more detail consider, A little extra clarification link):
package swingtest;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
/**
* Created with IntelliJ IDEA.
* User: Gagandeep Bali
* Date: 7/1/14
* Time: 9:44 AM
* To change this template use File | Settings | File Templates.
*/
public class ImageExample {
private MyPanel contentPane;
private void displayGUI() {
JFrame frame = new JFrame("Image Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPane = new MyPanel();
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private class MyPanel extends JPanel {
private BufferedImage image;
public MyPanel() {
try {
image = ImageIO.read(MyPanel.class.getResource("/resources/images/planetbackground.jpg"));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
#Override
public Dimension getPreferredSize() {
return image == null ? new Dimension(400, 300): new Dimension(image.getWidth(), image.getHeight());
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new ImageExample().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
There's a much easier way to load and set an image as a frame icon:
frame.setIconImage(
new ImageIcon(getClass().getResource("/resources/icon.gif")).getImage());
And thats all :)! You don't even have to use a try-catch block because ImageIcon does not throw any declared exceptions. And due to getClass().getResource(), it works both from file system and from a jar depending how you run your application.
If you need to check whether the image is available, you can check if the URL returned by getResource() is null:
URL url = getClass().getResource("/resources/icon.gif");
if (url == null)
System.out.println( "Could not find image!" );
else
frame.setIconImage(new ImageIcon(url).getImage());
The image files must be in the directory resources/ in your JAR, as shown in How to Use Icons and this example for the directory named images/.

Image in not repainting from List<path>

i am trying to load image from specified path, and list of files are stored inside List<>.
at first time when i initialize image it display but when i am trying to display next image from List instance which contain list of files, it doesn't repaint the image.
what's wrong going is i am initializing image in constructor first time that overwrite
the new image, now where to initialize image first time outside constructor i don't know.
my code :
public void nextImage(int cnt)
{
System.out.println(cnt);
if (cnt < imageFiles.size())
{
System.out.println(imageFiles.size());
try
{
bg = ImageIO.read(new File((imageFiles.get(cnt)).toString()));
scaled = getScaledInstanceToFit(bg, new Dimension(600, 600));
setBackground(Color.BLACK);
}
catch(Exception e)
{
e.printStackTrace();
}
}
MouseHandler handler = new MouseHandler();
addMouseListener(handler);
addMouseMotionListener(handler);
System.out.println(cnt);
System.out.println(imageFiles.get(cnt).toString());
}
menu item click code :
JMenuItem mntmRestoreImage = new JMenuItem("Next Image");
final ImagePane st = new ImagePane();
mntmRestoreImage.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent arg0)
{
st.nextImage(k);
k++;
}
});
mnEdit.add(mntmRestoreImage);
class code & constructor :
private BufferedImage bg;
private BufferedImage scaled;
java.util.List<Path> imageFiles= getFilesFromDirectory(FileSystems.getDefault().getPath("D:\\New folder"));
public ImagePane()
{
try
{
bg = ImageIO.read(getClass().getResource("/images/src11.jpg"));
scaled = getScaledInstanceToFit(bg, new Dimension(600, 600));
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
counter also increments, now the code inside ImagePane() constructor
overwrites the image of nextImage() function, so idea what happen out
in this code ??
any suggestion ?
I think I have the perfect solution for you! I wrote a little program for you so it is easier to understand.
First I have a method for you to check if the file is a picture:
public Stack<File> getFilesInFolder(String startPath) {
File startFolder = new File(startPath);
Stack<File> picturestack = new Stack<File>();
String extension;
int dotindex;
// Go through the folder
for (File file : startFolder.listFiles()) {
extension = "";
dotindex = file.getName().lastIndexOf('.'); // Get the index of the dot in the filename
if (dotindex > 0) {
extension = file.getName().substring(dotindex + 1);
// Iterate all valid file types and check it
for (String filetype : validpicturetypes) {
if (extension.equals(filetype)) {
picturestack.add(file);
}
}
}
}
return picturestack;
}
Very easy! Take the folder and iterate his files. Take the extension of the file and check if it is a valid file type. Define the file types in a array at the begining of your code.
String[] validpicturetypes = {"png", "jpg", "jpeg", "gif"};
At the end I push every file into a stack. Remember to fill the stack into a variable, do not read the files more than once because than you get the same problem as before:
Stack<File> pictures = getFilesInFolder("C:\\Users\\Admin\\Desktop");
After that use a Action for your JMenuItem! In my example I do not have much, you have to put your methods in!
Action nextpictureaction = new AbstractAction("Next Picture") {
private static final long serialVersionUID = 2421742449531785343L;
#Override
public void actionPerformed(ActionEvent e) {
if (!pictures.isEmpty()) {
System.out.println(pictures.pop().getName());
}
}
};
Add the Action at your JMenu and set the properties of your Frame.
/*
* Add Components to Frame
*/
setJMenuBar(menubar);
menubar.add(toolsmenu);
toolsmenu.add(nextpictureaction);
/*
* Frame Properties
*/
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationByPlatform(true);
setSize(1000, 700);
setTitle("PictureEditor");
setVisible(true);
At the end execute your program with the invokeLater method!
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new PictureEditor();
}
});
}
Summary
Basically you need a somthing to iterate through because values like integer are not saved the way you like. In my example I used a Stack and save at the beginning all pictures in it. Important is that, if you used or finished with the picture, you have to remove it (use stack.pop() for a Stack). I do not found a method where you check if the file is a picture (if it is the ImageIO catch it is bad). I wrote a method for that if you want you could use it.
This is not an answer, but I cannot paste that much code into a comment.
I would change your code to something along the lines of this piece of code. This seperates the image loading from the gui updating logic (like adding mousehandlers and the like, I pasted only image loading code).
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Arrays;
import java.util.Iterator;
import javax.imageio.ImageIO;
public class ImageLoader
{
public static class ImageContainer
{
BufferedImage bg = null;
BufferedImage scaled;
}
Iterator<File> imageFiles = Arrays.asList(
new File("D:\\New folder").listFiles()).iterator();
public ImageContainer nextImage(Dimension dimensionToFit) throws Exception
{
ImageContainer c = new ImageContainer();
if (imageFiles.hasNext())
{
File file = imageFiles.next();
//you might not need this, if only images are in this directory
if(file.isDirectory())
return null;
c.bg = ImageIO.read(file);
c.scaled = getScaledInstanceToFit(c.bg, dimensionToFit);
return c;
}
else
return null;
}
private BufferedImage getScaledInstanceToFit(BufferedImage bg,
Dimension dimensionToFit)
{
//do the risizing
}
}
This is not yet optimized though.

Adding image to JPanel through ImageIO.read?

I'm trying to add a JPanel with a picture in it. I'm using ImageIO.read to get the path but i get an IOException saying : can't read input file
The picture is called TCHLogo. It's a PNG inside a 'res' folder inside my project.
If there is any better way of displaying this image please also mention it!
Here is the code for my JPanel:
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class ImagePanel extends JPanel{
private BufferedImage image;
public ImagePanel() {
try {
//THIS LINE BELLOW WAS ADDED
image = ImageIO.read(getClass().getResourceAsStream("res/TCHLogo.png"));
} catch (IOException ex) {
// handle exception...
System.out.println(ex);
}
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g); //THIS LINE WAS ADDED
g.drawImage(image, 0, 0, null); // see javadoc for more info on the parameters
}
}
Here is how i add the JPanel in my Applet:
ImagePanel appletRunningPanel;
appletRunningPanel = new ImagePanel();
appletRunningPanel.setSize(300, 300);
appletRunningPanel.validate();
add(appletRunningPanel);
EDIT
I created a folder inside the bin which the application starts to look in currently..
the folder is called res and the picture is inside..
Now i get the following IOException when i run the line:
image = ImageIO.read(getClass().getResourceAsStream("res/TCHLogo.png"));
Here is the error log:
java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1338)
at surprice.applet.ImagePanel.<init>(ImagePanel.java:17)
at surprice.applet.MainClass.init(MainClass.java:41)
at sun.applet.AppletPanel.run(AppletPanel.java:436)
at java.lang.Thread.run(Thread.java:679)
Likely your image's file path isn't correct relative to the user directory. To find out where Java is starting to look, where the user directory is, place something like this line of code somewhere in your program:
System.out.println(System.getProperty("user.dir"));
Perhaps you'd be better off getting the image as an InputStream obtained from a resource and not as a file. e.g.,
image = ImageIO.read(getClass().getResourceAsStream("res/TCHLogo.png"));
This will look for the image at the path given relative to the location of the class files, and in fact this is what you must do if your image is located in your jar file.
Edit 2
As an aside, often you need to first call the super's paintComponent method before doing any of your own drawing so as to allow necessary house-keeping can be done, such as getting rid of "dirty" image bits. e.g., change this:
public void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, null);
}
to this:
public void paintComponent(Graphics g) {
super.paintComponent(g); // **** added****
g.drawImage(image, 0, 0, null);
}
I've written this ImagePanel class that i use for this scope :
public class ImagePanel extends JPanel {
private static final long serialVersionUID = 7952119619331504986L;
private BufferedImage image;
public ImagePanel() { }
public ImagePanel(String resName) throws IOException {
loadFromResource(resName);
}
public ImagePanel(BufferedImage image) {
this.image = image;
}
#Override
public void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, null); // see javadoc for more info on the parameters
}
public BufferedImage getImage() {
return image;
}
public void setImage(BufferedImage image) {
this.image = image;
}
/**
* Load the Image from a File
* #param path image name and path
* #throws IOException
*/
public void loadFromFile(String path) throws IOException {
image = ImageIO.read(new java.io.File(path));
}
/**
* Load Image from resource item
* #param resName name of the resource (e.g. : image.png)
* #throws IOException
*/
public void loadFromResource(String resName) throws IOException {
URL url = this.getClass().getResource(resName);
BufferedImage img = ImageIO.read(url);
image = img;
}
}
Then i use the ImagePanel in this way :
//Inizialization of the ImagePanel
ImagePanel panelImage = new ImagePanel();
//Try loading image into the image panel
try {
panelImage.loadFromResource("/Resources/someimage.png");
} catch (java.io.IOException e) {
//Handling Exception
}

Categories

Resources