I'm trying to display couple of images on the other image. It's important for me to have this pictures in two diffrent classes becous i need to connect this images with the other properties. I wrote a class which add and display one picture at a time.
import java.awt.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Obrazek {
BufferedImage image;
Obrazek(){
File imageFile = new File("tło.jpg");
try {
image = ImageIO.read(imageFile);
} catch (IOException e) {
System.err.println("Blad odczytu obrazka");
e.printStackTrace();
}
}
JPanel pane = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
};
}
Let say i have two classes of that kind, each with diffrent image. Then I wrote my main class with main method which define frame and create image.
import java.awt.*;
import javax.swing.*;
public class ButtonDemo{
public static void main(String[] args) {
JFrame frame = buildFrame();
Obrazek obraz = new Obrazek();
frame.add(obraz.pane);
}
private static JFrame buildFrame() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
return frame;
}
}
But when i add next frame.add(someobject.pane); it's delete previous one and displays only the last one. How to make them display together but from two separate classes.