JPanel billede på JPanel BIllede - GUI menu system
Hej alle,Er lige begyndt at rode med grafik - har tænkt mig at bygge et fullscreen gui med "skin" muligheder. Har lavet følgende kode, der laver et stort baggrunds billede med "skinnede" knapper oven på. Problemet er når jeg jeg maximerer vinduet, så hver gang jeg skifter focus fra knap til knap så kommer der fejl i baggrundsbilledet. Nogen ideér til hvad der er galt???
KODE (3 klasser)
##############################################################
import java.awt.Image;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.event.FocusListener;
import java.awt.event.FocusEvent;
import java.awt.Dimension;
import javax.swing.JPanel;
public class ActiveComponent extends JPanel implements FocusListener {
Image image = null;
private Image m_released;
private Image m_pressed;
private boolean pressed = false;
public ActiveComponent(Image pressed, Image released) {
m_pressed = pressed;
m_released = released;
this.setFocusable(true);
addFocusListener(this);
Color c = new Color(0, 0, 0, 0);
this.setBackground(c);
//widht, height
Dimension d = new Dimension(90, 31);
this.setPreferredSize(d);
this.setSize(d);
this.setMaximumSize(d);
this.setMinimumSize(d);
}
public void setImage(Image image){
this.image = image;
}
public Image getImage(Image image){
return image;
}
public void focusGained(FocusEvent e){
pressed = true;
this.repaint();
}
public void focusLost(FocusEvent e){
pressed = false;
this.repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g); //paint background
if(pressed){
image = m_pressed;
g.drawImage(image, 0, 0, this);
}else{
image = m_released;
g.drawImage(image, 0, 0, this);
}
paintChildren(g);
}
} //end class
################################################################
import java.awt.Image;
import java.awt.Graphics;
import javax.swing.JPanel;
public class ImagePanel extends JPanel{
Image image = null;
public ImagePanel(Image image) {
this.image = image;
}
public void setImage(Image image){
this.image = image;
}
public Image getImage(Image image){
return image;
}
public void paintComponent(Graphics g) {
super.paintComponent(g); //paint background
if (image != null) { //there is a picture: draw it
//int height = this.getSize().height;
//int width = this.getSize().width;
//g.drawImage(image,0,0, width, height, this);
g.drawImage(image, 0, 0, this); //original image size
}
}
} //end class
##############################################################
import javax.swing.JFrame;
import java.awt.Image;
import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JPanel;
import javax.swing.BoxLayout;
import javax.swing.BorderFactory;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JScrollPane;
public class GUI{
public static void main(String[] args) {
JFrame frame = new JFrame(":: GUI ::");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Image pressed = java.awt.Toolkit.getDefaultToolkit().getImage("button2.jpg");
Image released = java.awt.Toolkit.getDefaultToolkit().getImage("button1.jpg");
Image background = java.awt.Toolkit.getDefaultToolkit().getImage("BLueWireFrame_Wallpaper.jpg");
ImagePanel contentPane = new ImagePanel(background);
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
contentPane.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
frame.setContentPane(contentPane);
JPanel left = new JPanel();
left.setLayout(new GridLayout(5,2));
//left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
// top,left,buttom,right
left.setBorder(BorderFactory.createEmptyBorder(0,50,0,0));
JPanel center = new JPanel();
JPanel right = new JPanel();
for(int i=0; i <11; i++){
left.add(new ActiveComponent(pressed, released));
}
Color c = new Color(0,0,0,0);
left.setBackground(c);
center.setBackground(c);
right.setBackground(c);
contentPane.setBackground(c);
contentPane.add(left);
contentPane.add(center);
contentPane.add(right);
frame.pack();
frame.setVisible(true);
}
}// end class