Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

romanMagyar

help with swing code

This topic is 5388 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have to say, I like Swing, but sometimes making simple things with it can be such a pain. I copied one of the tutorials from java.sun, and it still doesn't work. I know it's somewhat long, but I would really appreciate it if you could help. It's supposed to display an image on top of the background. Here it is.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Example implements ActionListener {
    JLayeredPane layeredPane;
    JLabel bgLabel, fgLabel;
    int fgHeight, fgWidth;
    int bgHeight, bgWidth;
    
    void buildUI(Container container, Image bgImage, Image fgImage) {
        final ImageIcon bgIcon = new ImageIcon(bgImage);
        final ImageIcon fgIcon = new ImageIcon(fgImage);
        bgWidth = bgIcon.getIconWidth();
        bgHeight = bgIcon.getIconHeight();
        fgWidth = fgIcon.getIconWidth();
        fgHeight = fgIcon.getIconHeight();
    
        bgLabel = new JLabel(bgIcon);
        bgLabel.setOpaque(true);
        bgLabel.setBounds(0, 0, bgWidth, bgHeight);
        
        fgLabel = new JLabel(fgIcon);
        fgLabel.setBounds(0, 0, fgWidth, fgHeight);
    
        layeredPane = new JLayeredPane();
        layeredPane.setPreferredSize(new Dimension(bgWidth, bgHeight));
        layeredPane.add(bgLabel, new Integer(0));
        layeredPane.add(fgLabel, new Integer(1));
        container.add(layeredPane, BorderLayout.CENTER);
    }
    
    public static void main(String[] args) {
        Image bgImage = Toolkit.getDefaultToolkit().getImage("arena.bmp");
        Image fgImage = Toolkit.getDefaultToolkit().getImage("man.bmp");
        Example ex = new Example();
        JFrame f = new JFrame();
        ex.buildUI(f.getContentPane(), bgImage, fgImage);
        f.setSize(500, 500);
        f.setVisible(true);
    }
    
    public void actionPerformed(ActionEvent event) {
        fgLabel.setLocation(100, 100);
    }
}

  
EDIT: forgot to mention that it compiles but when you run it, you see nothing but the frame. Thanks. [edited by - romanMagyar on September 23, 2003 8:16:22 PM]

Share this post


Link to post
Share on other sites
Advertisement
That''s wierd man, everything looks OK to me. There should be a repaint() in the actionPerformed() method probably. And maybe in main, put an f.show() statement. Different versions of Java behave differently with the JFrame stuff, which can be really really annoying at times. Make sure you have the latest, 1.4.2 I believe.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!