das Bild kommt über einen JFileChooser) dieses Bild möchte ich jetzt in meinem JPanel setzen (also anzeigen lassen), allerdings weiß ich nicht wie ich vorgehen soll. Hat jemand eine Lösung?
Hier ein Beispiel mit einem herkömmlichen Image.
[CODE lang="java" title="Start"]import javax.swing.JFrame;
public class Start {
public static void main(String[] args) {
JFrame frame = new JFrame("Image Load");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ImageSetPanel(600, 600));
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
}
}[/CODE]
[CODE lang="java" title="ImagePanel" highlight="11,12,30-33,37-49"]import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class ImagePanel extends JPanel {
private Image img;
private Point pos = new Point();
public ImagePanel(int width, int height) {
setPreferredSize(new Dimension(width, height));
setBackground(Color.DARK_GRAY);
setBorder(BorderFactory.createLineBorder(Color.WHITE, 2));
}
public void centerImage(int width, int height) {
pos.x = 0;
pos.y = 0;
if (width < getWidth())
pos.x = (getWidth() - width) / 2;
if (height < getHeight())
pos.y = (getHeight() - height) / 2;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null)
g.drawImage(img, pos.x, pos.y, this);
}
public void setImage(Image img) {
if (img == null)
return;
int width = getWidth();
int height = getHeight();
int imgW = img.getWidth(this);
int imgH = img.getHeight(this);
float rel = imgW / (float) imgH;
if (imgW < imgH)
width = (int) (height * rel);
else
height = (int) (width / rel);
this.img = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
centerImage(width, height);
}
}[/CODE]
[CODE lang="java" title="ImageSetPanel"]import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;
@SuppressWarnings("serial")
public class ImageSetPanel extends JPanel {
JFileChooser chooser = new JFileChooser();
private JButton open = new JButton("load Image");
private ImagePanel image;
public ImageSetPanel(int width, int height) {
setPreferredSize(new Dimension(width, height));
setLayout(new BorderLayout(5, 5));
image = new ImagePanel(height, (int) (height * .9));
add(open, BorderLayout.NORTH);
add(image, BorderLayout.CENTER);
chooser.setFileFilter(new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif"));
open.addActionListener(e -> {
int ok = chooser.showOpenDialog(this);
if (ok == JFileChooser.APPROVE_OPTION) {
Image img;
try {
img = ImageIO.read(chooser.getSelectedFile());
} catch (IOException e1) {
JOptionPane.showMessageDialog(this, "Can't load " + chooser.getSelectedFile().getName());
return;
}
image.setImage(img);
repaint();
}
});
}
}[/CODE]