Advertisement
Guest User

Untitled

a guest
Oct 28th, 2013
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.56 KB | None | 0 0
  1. package ecrosogames.celldefender;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.Dimension;
  6. import java.awt.Font;
  7. import java.awt.Graphics;
  8. import java.awt.GridBagConstraints;
  9. import java.awt.GridBagLayout;
  10. import java.awt.Image;
  11. import java.awt.Insets;
  12.  
  13. import javax.swing.ImageIcon;
  14. import javax.swing.JButton;
  15. import javax.swing.JFrame;
  16. import javax.swing.JLabel;
  17. import javax.swing.JPanel;
  18. import javax.swing.SwingConstants;
  19. import javax.swing.UIManager;
  20.  
  21. public class MainMenu {
  22.  
  23.     private static final short WIDTH = 440;
  24.     private static final short HEIGHT = WIDTH;
  25.  
  26.     private JFrame frame;
  27.     private JPanel pane, paneT, paneC, paneB;
  28.     private JLabel lblTitle, lblFooter;
  29.     private JButton btn1, btn2, btn3, btn4;
  30.  
  31.     private Font font;
  32.  
  33.     public MainMenu() {
  34.         try {
  35.             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  36.         } catch (Exception e) {
  37.             e.printStackTrace();
  38.         }
  39.         Image bgImg = new ImageIcon(this.getClass().getResource("/icon/background.png")).getImage();
  40.  
  41.         frame = new JFrame();
  42.         pane = new ImagePanel(bgImg);
  43.         paneT = new JPanel();
  44.         paneC = new JPanel();
  45.         paneB = new JPanel();
  46.         lblTitle = new JLabel("Cell Defender");
  47.         lblFooter = new JLabel("Footer");
  48.         btn1 = new JButton("Regular");
  49.         btn2 = new JButton("Hardmode");
  50.         btn3 = new JButton("Survival");
  51.         btn4 = new JButton("Swarm");
  52.  
  53.         initPane();
  54.         initFrame();
  55.     }
  56.  
  57.     private void initFrame() {
  58.         frame.setTitle("Cell Defender");
  59.         frame.setResizable(false);
  60.         frame.add(pane);
  61.         frame.pack();
  62.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  63.         frame.setLocationRelativeTo(null);
  64.         frame.setVisible(true);
  65.     }
  66.  
  67.     private void initPane() {
  68.         pane.setPreferredSize(new Dimension(WIDTH, HEIGHT));
  69.         pane.setLayout(new BorderLayout());
  70.         paneT.setOpaque(false);
  71.         paneC.setLayout(new GridBagLayout());
  72.         paneC.setOpaque(false);
  73.         paneB.setPreferredSize(new Dimension(WIDTH, 150));
  74.         paneB.setLayout(new BorderLayout());
  75.         paneB.setOpaque(false);
  76.  
  77.         font = new Font("Courier New", 0, 36);
  78.         lblTitle.setForeground(Color.white);
  79.         lblTitle.setFont(font);
  80.  
  81.         paneT.add(lblTitle);
  82.  
  83.         customizeButton(btn1);
  84.         customizeButton(btn2);
  85.         customizeButton(btn3);
  86.         customizeButton(btn4);
  87.  
  88.         GridBagConstraints gbc = new GridBagConstraints();
  89.  
  90.         int insets = 10;
  91.  
  92.         gbc.insets = new Insets(insets, insets, insets, insets);
  93.         gbc.gridy = 0;
  94.         paneC.add(btn1, gbc);
  95.         gbc.gridy = 1;
  96.         paneC.add(btn2, gbc);
  97.         gbc.gridy = 2;
  98.         paneC.add(btn3, gbc);
  99.         gbc.gridy = 3;
  100.         paneC.add(btn4, gbc);
  101.  
  102.         lblFooter.setFont(font);
  103.         lblFooter.setHorizontalAlignment(SwingConstants.CENTER);
  104.         paneB.add(lblFooter, BorderLayout.CENTER);
  105.  
  106.         pane.add(paneT, BorderLayout.NORTH);
  107.         pane.add(paneC, BorderLayout.CENTER);
  108.         pane.add(paneB, BorderLayout.SOUTH);
  109.     }
  110.  
  111.     private void customizeButton(JButton b) {
  112.         Font f = new Font(null, 1, 12);
  113.  
  114.         b.setPreferredSize(new Dimension(200, 35));
  115.         b.setFocusPainted(false);
  116.         b.setFont(f);
  117.     }
  118.  
  119.     public static void main(String[] args) {
  120.         new MainMenu();
  121.     }
  122. }
  123.  
  124.     class ImagePanel extends JPanel {
  125.         private static final long serialVersionUID = 1L;
  126.  
  127.         private Image img;
  128.  
  129.         public ImagePanel(String img) {
  130.             this(new ImageIcon(img).getImage());
  131.         }
  132.  
  133.         public ImagePanel(Image img) {
  134.             this.img = img;
  135.             Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
  136.             setPreferredSize(size);
  137.             setMinimumSize(size);
  138.             setMaximumSize(size);
  139.             setSize(size);
  140.             setLayout(null);
  141.         }
  142.  
  143.         public void paintComponent(Graphics g) {
  144.             g.drawImage(img, 0, 0, null);
  145.         }
  146.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement