Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pianotiles.GUI.panels;
- import pianotiles.GUI.frames.Frame;
- import pianotiles.GUI.hotAreas.HotArea;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.image.BufferedImage;
- import javax.swing.border.Border;
- import pianotiles.logic.Game;
- import pianotiles.logic.exceptions.InvalidInputException;
- import pianotiles.utils.Resources;
- public class NamePanel extends JPanel {
- public static final int WIDTH = 803, HEIGHT = 827;
- public static final int HOTAREA_WIDTH = 75, HOTAREA_HEIGHT = 80;
- private HotArea backArea;
- private HotArea startArea;
- private Frame frame;
- private JTextField nameField;
- private BufferedImage background;
- public NamePanel(Frame mainFrame) {
- super(null);
- this.frame = mainFrame;
- this.setSize(WIDTH, HEIGHT);
- this.background = Resources.getImage("/pianotiles/GUI/images/black&white.jpg");
- backArea = new HotArea(HOTAREA_WIDTH, HOTAREA_HEIGHT);
- backArea.setLocation(715, 15);
- startArea = new HotArea(HOTAREA_WIDTH, HOTAREA_HEIGHT);
- startArea.setLocation(0, 0);
- JButton jb = new JButton("Enter");
- Component add = this.add(jb);
- this.nameField = new JTextField();
- this.nameField.setLocation(198, 200);
- this.nameField.setSize(446, 50);
- this.nameField.setVisible(true);
- this.nameField.setFont((new Font("Arial", 1, 25)));
- Border border = BorderFactory.createLineBorder(Color.black, 3);
- this.nameField.setBorder(border);
- this.add(this.nameField);
- this.nameField.addActionListener(new MyActionListener());
- this.addMouseListener(new MyMouseListener());
- }
- private void startGame() {
- boolean validNickname;
- do {
- String input = this.nameField.getText();
- if (input == null || input.isEmpty()) {
- return;
- }
- validNickname = true;
- try {
- Game.setNewGame(input);
- } catch (InvalidInputException e) {
- JOptionPane.showMessageDialog(null, "Insert a valid nickname", "INVALID NICKNAME", JOptionPane.WARNING_MESSAGE);
- validNickname = false;
- }
- } while (!validNickname);
- Game.getInstance();
- frame.restartGame();
- }
- @Override
- protected void paintComponent(Graphics g) {
- g.drawImage(background, 0, 0, this);
- }
- private class MyMouseListener extends MouseAdapter {
- @Override
- public void mousePressed(MouseEvent e) {
- if (backArea.contains(e.getPoint())) {
- frame.changePanel(frame.mainPanel);
- }
- if (startArea.contains(e.getPoint())) {
- NamePanel.this.startGame();
- Game.getInstance();
- frame.restartGame();
- }
- }
- }
- private class MyActionListener implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent e) {
- NamePanel.this.startGame();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement