Advertisement
Guest User

Untitled

a guest
Mar 26th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.60 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.image.BufferedImage;
  3. import java.io.File;
  4. import java.io.IOException;
  5.  
  6. import javax.imageio.ImageIO;
  7. import javax.swing.JButton;
  8. import javax.swing.JFrame;
  9. import javax.swing.JLayeredPane;
  10. import javax.swing.JPanel;
  11.  
  12. public class GUIFrame extends JFrame{
  13.  
  14.     JLayeredPane layerPane = new JLayeredPane();
  15.     BackgroundPanel gameBackground = new BackgroundPanel(loadImage("Background.png"), this);
  16.     JButton testButton = new JButton("TEST BUTTON");
  17.     JPanel testPanel = new JPanel();
  18.    
  19.     public GUIFrame(String frameTitle) {
  20.         super(frameTitle);
  21.        
  22.         //Set BorderLayout
  23.         setLayout(new BorderLayout());
  24.        
  25.         //Put testButton inside testPanel
  26.         testButton.setVisible(true);
  27.         testButton.setSize(500, 50);
  28.         testPanel.add(testButton);
  29.         testPanel.setSize(500, 50);
  30.        
  31.         //Add gameBackground to default layer of layerPane
  32.         layerPane.add(gameBackground, JLayeredPane.DEFAULT_LAYER);
  33.         //Add testPanel to the layer above default
  34.         layerPane.add(testPanel, JLayeredPane.PALETTE_LAYER);
  35.        
  36.         //Add layerPane to JFrame
  37.         add(layerPane);
  38.        
  39.         /* If I uncomment these to lines and comment the line
  40.          * it will work but then the button will be in the same layer */
  41.         //add(testPanel);
  42.         //add(gameBackground);
  43.     }
  44.    
  45.     //Simple image loading method; Returns buffered image
  46.     public static BufferedImage loadImage(String fileLocation){
  47.         BufferedImage image = null;
  48.         try{
  49.             image = ImageIO.read(new File(fileLocation));
  50.         }catch(IOException exceptionReturned){
  51.             System.out.println("Image loading exception when loading " + fileLocation);
  52.         }
  53.         return image;
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement