Advertisement
Guest User

Untitled

a guest
Aug 28th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.28 KB | None | 0 0
  1. package pianotiles.GUI.panels;
  2.  
  3. import pianotiles.GUI.frames.Frame;
  4. import pianotiles.GUI.hotAreas.HotArea;
  5.  
  6. import javax.swing.*;
  7. import java.awt.*;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.MouseAdapter;
  11. import java.awt.event.MouseEvent;
  12. import java.awt.image.BufferedImage;
  13. import javax.swing.border.Border;
  14. import pianotiles.logic.Game;
  15. import pianotiles.logic.exceptions.InvalidInputException;
  16. import pianotiles.utils.Resources;
  17.  
  18. public class NamePanel extends JPanel {
  19.  
  20.     public static final int WIDTH = 803, HEIGHT = 827;
  21.     public static final int HOTAREA_WIDTH = 75, HOTAREA_HEIGHT = 80;
  22.  
  23.     private HotArea backArea;
  24.     private HotArea startArea;
  25.     private Frame frame;
  26.     private JTextField nameField;
  27.     private BufferedImage background;
  28.    
  29.     public NamePanel(Frame mainFrame) {
  30.         super(null);
  31.         this.frame = mainFrame;
  32.         this.setSize(WIDTH, HEIGHT);
  33.         this.background = Resources.getImage("/pianotiles/GUI/images/black&white.jpg");
  34.        
  35.         backArea = new HotArea(HOTAREA_WIDTH, HOTAREA_HEIGHT);
  36.         backArea.setLocation(715, 15);
  37.         startArea = new HotArea(HOTAREA_WIDTH, HOTAREA_HEIGHT);
  38.         startArea.setLocation(0, 0);
  39.        
  40.         JButton jb = new JButton("Enter");
  41.         Component add = this.add(jb);
  42.        
  43.         this.nameField = new JTextField();
  44.         this.nameField.setLocation(198, 200);
  45.         this.nameField.setSize(446, 50);
  46.         this.nameField.setVisible(true);
  47.         this.nameField.setFont((new Font("Arial", 1, 25)));
  48.         Border border = BorderFactory.createLineBorder(Color.black, 3);
  49.         this.nameField.setBorder(border);
  50.         this.add(this.nameField);
  51.        
  52.         this.nameField.addActionListener(new MyActionListener());
  53.         this.addMouseListener(new MyMouseListener());
  54.     }
  55.  
  56.     private void startGame() {
  57.         boolean validNickname;
  58.         do {
  59.             String input = this.nameField.getText();
  60.  
  61.             if (input == null || input.isEmpty()) {
  62.                 return;
  63.             }
  64.             validNickname = true;
  65.  
  66.             try {
  67.                 Game.setNewGame(input);
  68.             } catch (InvalidInputException e) {
  69.                 JOptionPane.showMessageDialog(null, "Insert a valid nickname", "INVALID NICKNAME", JOptionPane.WARNING_MESSAGE);
  70.                 validNickname = false;
  71.             }
  72.         } while (!validNickname);
  73.         Game.getInstance();
  74.         frame.restartGame();
  75.     }
  76.  
  77.     @Override
  78.     protected void paintComponent(Graphics g) {
  79.            g.drawImage(background, 0, 0, this);
  80.     }
  81.  
  82.     private class MyMouseListener extends MouseAdapter {
  83.  
  84.         @Override
  85.         public void mousePressed(MouseEvent e) {
  86.             if (backArea.contains(e.getPoint())) {
  87.                 frame.changePanel(frame.mainPanel);
  88.             }
  89.             if (startArea.contains(e.getPoint())) {
  90.                 NamePanel.this.startGame();
  91.                 Game.getInstance();
  92.                 frame.restartGame();
  93.             }
  94.         }
  95.     }
  96.  
  97.     private class MyActionListener implements ActionListener {
  98.  
  99.         @Override
  100.         public void actionPerformed(ActionEvent e) {
  101.             NamePanel.this.startGame();
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement