I have the following code that I found on the web and modified to my likings:
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.