# Bilder übereinander legen



## Wassim (22. Jan 2006)

Hallo! 

Ich würde gerne in eine JAVA Applikation ein Hintergrundbild anzeigen lassen, und darüber nochmal ein paar kleinere Bilder auf die man dann irgendwann klicken können soll. 
Das Problem ist, dass nur das Hintergrundbild angezeigt wird, und das zweite dann schon nicht mehr.
Weiß jemand woran das liegt? 
Ich hänge da echt schon ewig dran.

Hier der Code: 


```
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import javax.swing.*;
import javax.imageio.ImageIO;



public class ImageDisplayer extends JFrame{
    static String imageFile = "background.gif";
    static String homeFile = "home_large.gif";


    public void init() {
        Image bi = null;
        try
        {
            File f = new File("background.gif");
            bi = ImageIO.read(f);
        }catch(IOException e){}


    }

    public static void main(String[] args) {

        //ImagePanel homePanel = new ImagePanel(Homeimage);

        JFrame f = new JFrame("MMI - Webpage");

        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });


        Image image = null;
        Image home = null;
            try {
                // Read from a file
                File file = new File("background.gif");
                image = ImageIO.read(file);
                File homefile = new File("home_large.gif");
                home = ImageIO.read(homefile);

                // Read from an input stream

            } catch (IOException e) {
            }

            // Use a label to display the image
            JFrame frame = new JFrame();
            JLabel label = new JLabel(new ImageIcon(image));
            JLabel HomeLabel = new JLabel(new ImageIcon(home));
            Container c= frame.getContentPane();
            c.setLayout(null);
            label.setBounds(0,0,1024,768);
            frame.getContentPane().add(label, null);
            HomeLabel.setBounds(0,0,150,73);
            frame.getContentPane().add(HomeLabel, null);
            frame.pack();
            frame.setSize(1024,768);
            frame.setVisible(true);
    }
}

class ImagePanel extends JPanel {
    Image image;

    public ImagePanel(Image image) {
        this.image = image;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g); //paint background

        
        g.drawImage(image, 0, 0, this); 

        
    }
}
```

Vielen Dank!

Wassim


----------



## Guest (22. Jan 2006)

Ok, habe es jetzt irgendwie GANZ anders gelöst... wobei ich der Meinung bin dass ich das sicher zu umständlich gemacht habe... aber was soll's . Es funzt.
Eine letzte Hürde habe ich aber noch: 
Ich möchte, dass wenn man mit der Mouse über ein Bild geht das Bild gegen ein anderes ausgetauscht wird. 
Das kriege ich ums Verrecken nicht hin. 
Habe es folgendermaßen probiert (siehe startAnimation() ). Die Ausgaben "I'm in" und "I'm out" kommen wenn ich mit der Mouse in das Bild "t1Label" bzw. raus gehe. Aber das Bild wrd nicht ausgetauscht.

Weiß jemand Rat? Wär echt klasse!  


```
/*
 * Swing version.
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MovingLabels extends JFrame
                          implements ActionListener {
    int frameNumber = -1;
    Timer timer;
    boolean frozen = false;

    JLayeredPane layeredPane;
    JLabel bgLabel, fgLabel, galLabel, mozLabel, conLabel, linLabel, logLabel, t1Label, t2Label, t3Label, picLabel, urlLabel;
    int fgHeight, fgWidth;
    int bgHeight, bgWidth;
    static String mozFile = "mozilla_interface.gif";
    static String fgFile = "home_small.gif";
    static String bgFile = "background.gif";
    static String galFile = "gallery_small.gif";
    static String conFile = "contact_small.gif";
    static String linFile = "links_small.gif";
    static String logFile = "logo.gif";
    static String t1File = "text1_small.gif";
    static String t2File = "text2_small.gif";
    static String picFile = "picture.gif";
    static String urlFile = "http.gif";



    void buildUI(Container container, Image bgImage, Image fgImage, Image galImage, Image mozImage, Image conImage, Image linImage, Image logImage, Image t1Image, Image t2Image, Image picImage, Image urlImage) {
        final ImageIcon bgIcon = new ImageIcon(bgImage);
        final ImageIcon fgIcon = new ImageIcon(fgImage);
        final ImageIcon galIcon = new ImageIcon(galImage);
        final ImageIcon mozIcon = new ImageIcon(mozImage);
        final ImageIcon conIcon = new ImageIcon(conImage);
        final ImageIcon linIcon = new ImageIcon(linImage);
        final ImageIcon logIcon = new ImageIcon(logImage);
        final ImageIcon t1Icon = new ImageIcon(t1Image);
        final ImageIcon t2Icon = new ImageIcon(t2Image);
        final ImageIcon picIcon = new ImageIcon(picImage);
        final ImageIcon urlIcon = new ImageIcon(urlImage);

        bgWidth = bgIcon.getIconWidth();
        bgHeight = bgIcon.getIconHeight();
        fgWidth = fgIcon.getIconWidth();
        fgHeight = fgIcon.getIconHeight();

        //Set up a timer that calls this object's action handler
        timer = new Timer(100, this); //delay = 100 ms
        timer.setInitialDelay(0);
        timer.setCoalesce(true);

        //Create a label to display the background image.
        bgLabel = new JLabel(bgIcon);
        bgLabel.setOpaque(true);
        bgLabel.setBounds(0, 0, bgWidth, bgHeight);

        //Create a label to display the foreground image.
        fgLabel = new JLabel(fgIcon);
        fgLabel.setBounds(-fgWidth, -fgHeight, fgWidth, fgHeight);

        galLabel = new JLabel(galIcon);

        mozLabel = new JLabel(mozIcon);

        conLabel = new JLabel(conIcon);

        linLabel = new JLabel(linIcon);

        logLabel = new JLabel(logIcon);

        t1Label = new JLabel(t1Icon);

        t2Label = new JLabel(t2Icon);

        t3Label = new JLabel(t1Icon);

        picLabel = new JLabel(picIcon);

        urlLabel = new JLabel(urlIcon);


        //Create the layered pane to hold the labels.
        layeredPane = new JLayeredPane();
        layeredPane.setPreferredSize(
                       new Dimension(bgWidth, bgHeight));
        //layeredPane.addMouseListener(new MouseAdapter() {
        t1Label.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
               //if (frozen) {
                //    frozen = false;
                   System.out.println("I'm in!!");
                   startAnimation();
                //} else {
                 //   frozen = true;
                 //   stopAnimation();
                //}
            }
            public void mouseExited(MouseEvent e) {
               //if (frozen) {
                //    frozen = false;
                   System.out.println("I'm out!!");
                   startAnimation();
                //} else {
                 //   frozen = true;
                 //   stopAnimation();
                //}
            }
        });

        layeredPane.add(bgLabel, new Integer(0));  //low layer
        layeredPane.add(mozLabel, new Integer(1));  //high layer
        layeredPane.add(logLabel, new Integer(2));  //high layer
        layeredPane.add(fgLabel, new Integer(3));  //high layer
        layeredPane.add(galLabel, new Integer(4));  //high layer
        layeredPane.add(conLabel, new Integer(5));  //high layer
        layeredPane.add(linLabel, new Integer(6));  //high layer
        layeredPane.add(t1Label, new Integer(7));  //high layer
        layeredPane.add(t2Label, new Integer(8));  //high layer
        layeredPane.add(t3Label, new Integer(9));  //high layer
        layeredPane.add(picLabel, new Integer(10));  //high layer
        layeredPane.add(urlLabel, new Integer(11));  //high layer

        galLabel.setBounds(400,140,90,43);
        fgLabel.setBounds(300,140,90,43);
        mozLabel.setBounds(-2,0,1024,57);
        conLabel.setBounds(500,140,90,43);
        linLabel.setBounds(600,140,90,43);
        logLabel.setBounds(10,65,123,68);
        t1Label.setBounds(250,200,487,156);
        t2Label.setBounds(250,360,231,172);
        t3Label.setBounds(250,536,487,156);
        picLabel.setBounds(485,360,164,175);
        urlLabel.setBounds(245,698,193,37);
        //fgLabel.setLocation(50,100);
        container.add(layeredPane, BorderLayout.CENTER);
    }

    //Invoked by the applet browser only.
    public void start() {
        startAnimation();
    }

    //Invoked by the applet browser only.
    public void stop() {
        stopAnimation();
    }

    public synchronized void startAnimation() {
      
        t1File = "text1_large.gif";
        Image t1Image = Toolkit.getDefaultToolkit().getImage(t1File);
        ImageIcon t1Icon = new ImageIcon(t1Image);
        t1Label = new JLabel(t1Icon);
        this.layeredPane.add(t1Label, new Integer[7]);

    }

    public synchronized void stopAnimation() {
        //Stop the animating thread.
        if (timer.isRunning()) {
            timer.stop();
        }
    }

    public void actionPerformed(ActionEvent e) {
        //Advance animation frame.
        frameNumber++;

        //Display it.
        //fgLabel.setLocation(50,50);
                /*
            ((frameNumber*5)
             % (fgWidth + bgWidth))
            - fgWidth,
            (bgHeight - fgHeight)/2);  */
    }

    //Invoked only when run as an application.
    public static void main(String[] args) {
        Image bgImage = Toolkit.getDefaultToolkit().getImage(MovingLabels.bgFile);
        Image fgImage = Toolkit.getDefaultToolkit().getImage(MovingLabels.fgFile);
        Image galImage = Toolkit.getDefaultToolkit().getImage(MovingLabels.galFile);
        Image mozImage = Toolkit.getDefaultToolkit().getImage(MovingLabels.mozFile);
        Image conImage = Toolkit.getDefaultToolkit().getImage(MovingLabels.conFile);
        Image linImage = Toolkit.getDefaultToolkit().getImage(MovingLabels.linFile);
        Image logImage = Toolkit.getDefaultToolkit().getImage(MovingLabels.logFile);
        Image t1Image = Toolkit.getDefaultToolkit().getImage(MovingLabels.t1File);
        Image t2Image = Toolkit.getDefaultToolkit().getImage(MovingLabels.t2File);
        Image picImage = Toolkit.getDefaultToolkit().getImage(MovingLabels.picFile);
        Image urlImage = Toolkit.getDefaultToolkit().getImage(MovingLabels.urlFile);

        final MovingLabels movingLabels = new MovingLabels();
        JFrame f = new JFrame("MovingLabels");
        f.addWindowListener(new WindowAdapter() {
            public void windowIconified(WindowEvent e) {
                movingLabels.stopAnimation();
            }
            public void windowDeiconified(WindowEvent e) {
                movingLabels.startAnimation();
            }
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        movingLabels.buildUI(f.getContentPane(), bgImage, fgImage, galImage, mozImage, conImage, linImage, logImage, t1Image, t2Image, picImage, urlImage);
        f.setSize(1024, 768);
        f.setVisible(true);
        //movingLabels.startAnimation();
    }
}
```


Vielen Dank! 

Wassim


----------

