Hallo Leute,
brauche mal wieder dringend Hilfe. Ich habe ein JFrame mit einem Hintergrundbild. Sobald ich die Größe des Fenster verändere, so soll erreicht werden, dass das Hintergrundbild skaliert wird und damit an die Größe des Fensters angepasst wird. Wie mache ich das am besten. Anbei mein bisheriges Coding und schon mal Danke für jede Hilfe
Ciao, skywest
und noch die Klasse für das eingebettete JPanel
brauche mal wieder dringend Hilfe. Ich habe ein JFrame mit einem Hintergrundbild. Sobald ich die Größe des Fenster verändere, so soll erreicht werden, dass das Hintergrundbild skaliert wird und damit an die Größe des Fensters angepasst wird. Wie mache ich das am besten. Anbei mein bisheriges Coding und schon mal Danke für jede Hilfe
Ciao, skywest
Code:
import java.awt.*;
import javax.swing.*;
public class PaintingTest extends JFrame {
private GraphicPanel mvGraphicPanel = null;
private JPanel jContentPane = null;
private void initialize() {
setLocation(25, 25);
setSize(600, 500);
this.setContentPane(getJContentPane());
}
private GraphicPanel getGraphicPanel(){
if (mvGraphicPanel == null){
mvGraphicPanel = new GraphicPanel(this);
}
return mvGraphicPanel;
}
private JPanel getJContentPane() {
if (jContentPane == null) {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.ipadx = 0;
gridBagConstraints.ipady = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weighty = 1.0D;
gridBagConstraints.weightx = 1.0D;
jContentPane = new JPanel();
jContentPane.setLayout(new GridBagLayout());
jContentPane.add(getGraphicPanel(), gridBagConstraints);
}
return jContentPane;
}
public static void main(java.lang.String[] args) {
PaintingTest pt = new PaintingTest();
pt.initialize();
pt.setVisible(true);
}
}
Code:
import java.awt.*;
import javax.swing.*;
public class GraphicPanel extends JPanel{
JFrame mvOwner = null;
Image image = null;
Image scaled2 = null;
ImageIcon scaledIcon = null;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Insets ins = getInsets();
scaledIcon.paintIcon(this, g, ins.left, ins.top);
}
/**
*
*/
public GraphicPanel(JFrame owner) {
super();
mvOwner = owner;
System.out.println(mvOwner.getWidth());
System.out.println(mvOwner.getHeight());
image = new ImageIcon(GraphicPanel.class.getResource
( "Christine.jpg" )).getImage();
scaled2 = image.getScaledInstance(
mvOwner.getWidth(),
mvOwner.getHeight(),
Image.SCALE_SMOOTH );
scaledIcon = new ImageIcon(scaled2);
}
}