Help with adding multiple images to an application
I have the following code that I found on the web and modified to my likings:
Code :
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ImagePanel extends JPanel {
BufferedImage image;
BufferedImage image2;
public ImagePanel(BufferedImage image) {
this.image = image;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 50, 50, this);
g.drawImage(image, 100, 100, this);
g.drawImage(image2, 120, 120, this);
}
public static void main(String[] args) throws IOException {
BufferedImage image = ImageIO.read(new File("http://www.javaprogrammingforums.com/images/tile1.png"));
BufferedImage image2 = ImageIO.read(new File("http://www.javaprogrammingforums.com/images/tile2.png"));
ImagePanel contentPane = new ImagePanel(image);
ImagePanel contentPane2 = new ImagePanel(image2);
JFrame f = new JFrame("Bl");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(contentPane);
f.getContentPane().add(contentPane2);
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
The code successfully imports and displays two identical images as shown in the paintComponent. What I am trying to do is change it so that it can load multiple different image files and display them separately. As you can see I added "image2" into in a few places in hopes that I might find a way to add a second image. Unfortunately I had no luck with this. What am I doing wrong? Sorry, I'm sure this is probably something that is very obvious to some, but I'm still pretty new to programming.
Re: Help with adding multiple images to an application
[.code] [./code] taking out the periods.
hey guys how do i display a tag with out it being executed?
I would have liked to type out [.code] [./code] with out the periods, but it displaying the characters instead executing
Re: Help with adding multiple images to an application
Thanks man, I fixed the OP now.
But back to the original question... does anybody have a fix for me?
Re: Help with adding multiple images to an application
Quote:
load multiple different image files and display them separately
Can you explain your problem? I see that you are using ImageIO class's read method to read files
and are using the Graphics class's drawImage method to draw images.
If you are getting errors you need to tell us. Copy and paste the full text of the error messages here.
Re: Help with adding multiple images to an application
Actually I'm not getting any errors, it just plain will not display the second image file. My problem is that i just can't figure out how to make it display that image. Sorry if I was unclear.
Re: Help with adding multiple images to an application
One problem I see is that you are not telling the layout manager where to place the JPanels that you are adding to the frame. Your setting of the contentpane and adding to the contentpane seems confused.
Also where do you assign a value to the image2 variable in the ImagePanel class?
Why not create two instances of the class and add them in different places.
Or create one instance of the class and give it the two images so it can draw them.
Re: Help with adding multiple images to an application
Ah, thank you very much Norm. I took your advice and added a second argument to the constructor so it is now able to load two different images at once. However, I have a new problem. I started wondering how I could add a very large assortment of images to the program. I'm sure I could just keep adding on to the constructor, but that would get to be very, very cluttered. My thought was that I could just create an array of BufferedImages and use that as the argument for my constructor. I worked on it a little and came up with this code:
Code :
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ImagePanel extends JPanel {
public ImagePanel(BufferedImage images[]) {
this.image = images[0];
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image[0], 50, 50, this);
g.drawImage(image[0], 100, 100, this);
g.drawImage(image[0], 120, 120, this);
}
public static void main(String[] args) throws IOException {
BufferedImage image[] = new BufferedImage[1];
image[0] = ImageIO.read(new File("http://www.javaprogrammingforums.com/images/tile1.png"));
image[1] = ImageIO.read(new File("http://www.javaprogrammingforums.com/images/tile2.png"));
ImagePanel contentPane = new ImagePanel(image[]);
JFrame f = new JFrame("dslkh");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(contentPane);
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
Unfortunately I get an error when trying to compile it. I get this message:
C:\Users\Drew\Documents\JCreator LE\MyProjects\ImagePanel\src\ImagePanel.java:59: error: '.class' expected
ImagePanel contentPane = new ImagePanel(image[]);
I don't have a clue what that means. Granted I'm pretty new to Java, but I've never seen anything like that before. Do you have any advice for how to fix this?
Also, is there a better way to set this up than the way I am? This was just the simplest way I could think to get multiple images.
Re: Help with adding multiple images to an application
You don't use the array referencing notation: ([]) when passing an array. Just use the name of the array.
Leave off the []s
Re: Help with adding multiple images to an application
Thank you yet again! I was able to fix up my code and it now does exactly what I want. I can't thank you enough for all of your help. I really appreciate everything that you have done for me.
Re: Help with adding multiple images to an application