Reputation: 141
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.
Upvotes: 0
Views: 722
Reputation: 208984
"It's important for me to have this pictures in two diffrent classes becous i need to connect this images with the other properties"
You may be going about this incorrectly, trying to make each of these classes a JPanel
. I mean, technically you could make it happen with a JLayeredPane
. But I would go about it differently.
First to handle your "other properties", you could use an interface
to combine the objects you want to draw. Just have a define draw
method in the interface.
public interface Drawable {
void draw(Graphics g);
}
Then you can have a main panel to do the drawing. This panel will hold a List
or Map
of Drawable
objects; a List
if access after adding them is not important, and a Map
if you need easy access to them after you add. For instance.
public class DrawingPanel extends JPanel {
List<Drawable> drawList;
// Map<String, Drawable> drawMap;
public void addToDrawList(Drawable drawable) {
drawList.add(drawable);
repaint();
}
// public void addToDrawMap(String key, Drawable drawable) {
// drawMap.put(key, drawable);
// repaint();
// }
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for(Drawable drawable : drawList){
drawable.draw(g);
}
// for (Drawable drawable : drawMap.values()) {
// drawable.draw(g);
// }
}
}
Now you can just make any class you want implements Drawable
, and just add an instance of that class to the drawList
or drawMap
. This way everything is all drawn onto the same surface. For example
class HelloDrawable implements Drawable {
@Override
public void draw(Graphics g) {
g.drawString("Hello", 100, 100);
}
}
class WorldDrawable implements Drawable {
@Override
public void draw(Graphics g) {
g.drawString("World", 150, 100);
}
}
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
DrawingPanel panel = new DrawingPanel();
panel.addToDrawList(new HelloDrawable());
panel.addToDrawList(new WorldDrawable());
// panel.addToDrawMap("Hello", new HelloDrawable());
}
}
Upvotes: 3
Reputation: 19
In JavaFx You could use StackPane and ImageView Put all images in ImageView and leter use stack Pane to put new image over the old one.
Upvotes: -1