Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // START WINDOW KLASA KLASUNIA
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.ComponentEvent;
- import java.awt.event.ComponentListener;
- import java.net.InetAddress;
- import java.net.UnknownHostException;
- public class StartWindow extends JFrame implements ActionListener {
- /** włącza grę w tryb offline */
- private JButton offlineGameButton;
- /** włącza grę w tryb online */
- private JButton onlineGameButton;
- /**przycisk pomocy*/
- private JButton helpButton;
- /** Napis powitalny */
- private JLabel welcomeLabel;
- /**Ikona pomocy*/
- private Icon helpButtonIcon;
- /**panel obrazu głównego*/
- private ImagePanel imagePanel;
- private SpringLayout springLayout = new SpringLayout();
- /** Konstruktor - tworzenie i inicjalizowane cale okno startowe*/
- public StartWindow() {
- setPreferredSize(new Dimension(700, 700));
- setMinimumSize(new Dimension(500,500));
- setTitle("Lunar Lander");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- offlineGameButton = new JButton("Gra offline");
- onlineGameButton = new JButton("Gra online");
- welcomeLabel = new JLabel("Witaj w grze Lunar Lander. Wybierz tryb gry.");
- welcomeLabel.setFont(new Font("TimesNewRoman", Font.PLAIN, 20));
- welcomeLabel.setForeground(Color.WHITE);
- imagePanel = new ImagePanel("res/images/StartScreenBg.png");
- helpButtonIcon = new ImageIcon("res/images/helpButton.png");
- helpButton.setOpaque(false); //nieprzezroczysty-wyłącz
- helpButton.setContentAreaFilled(false); //wypełnienie-wyłącz
- helpButton.setBorderPainted(false); //obramowanie - wyłącz
- setLayout(springLayout);
- Container pane = getContentPane();
- //koordynaty helpButton
- springLayout.putConstraint(SpringLayout.NORTH, helpButton, 0, springLayout.NORTH, pane);
- springLayout.putConstraint(SpringLayout.EAST, helpButton, 0, SpringLayout.EAST, pane);
- //koordynaty welcomeLabel
- springLayout.putConstraint(SpringLayout.SOUTH, welcomeLabel, -70, SpringLayout.SOUTH, pane);
- springLayout.putConstraint(SpringLayout.HORIZONTAL_CENTER, welcomeLabel, 0, SpringLayout.HORIZONTAL_CENTER, pane);
- //koordynaty offlineGameButton
- springLayout.putConstraint(SpringLayout.SOUTH, offlineGameButton, -40, SpringLayout.SOUTH, pane);
- springLayout.putConstraint(SpringLayout.HORIZONTAL_CENTER, offlineGameButton, 0, SpringLayout.HORIZONTAL_CENTER, pane);
- //koordynaty onlineGameButton
- springLayout.putConstraint(SpringLayout.HORIZONTAL_CENTER, onlineGameButton, 0, SpringLayout.HORIZONTAL_CENTER, pane);
- springLayout.putConstraint(SpringLayout.SOUTH, onlineGameButton, -10, SpringLayout.SOUTH, pane);
- pane.add(welcomeLabel, SpringLayout.NORTH);
- pane.add(helpButton);
- pane.add(offlineGameButton);
- pane.add(onlineGameButton);
- add(imagePanel);
- onlineGameButton.setToolTipText("Rozpocznij rozgrywke, pobierając dane z servera");
- offlineGameButton.setToolTipText("Rozpocznij rozgrywke korzystając z lokalnych danych");
- helpButton.setToolTipText("Nie wiesz jak grac? Kliknij :)");
- helpButton.addActionListener(this);
- offlineGameButton.addActionListener(this);
- onlineGameButton.addActionListener(this);
- //this - odniesienie do bieżącego obiektu
- //Gdy okno zmienia swój rozmiar tworzone jest zdarzenie i wtedy aktualizowany jest rozmiar obrazu
- //będącego jego tłem
- addComponentListener(new ComponentListener() {
- @Override
- public void componentResized(ComponentEvent e) {
- imagePanel.setSize(getWidth(), getHeight());
- imagePanel.repaint();
- repaint();
- }
- @Override
- public void componentMoved(ComponentEvent e) {
- }
- @Override
- public void componentShown(ComponentEvent e) {
- }
- @Override
- public void componentHidden(ComponentEvent e) {
- }
- });
- pack();
- setLocationRelativeTo(null);
- setVisible(true);
- }
- /**
- *
- * Obsługa zdarzeń
- * @param event
- */
- @Override
- public void actionPerformed(ActionEvent event) {
- Object source = event.getSource();
- boolean errorPort = true;
- boolean errorAddress = true;
- int number=0;
- InetAddress serverAddress = null;
- if (source == helpButton) {
- new HelpWindow();
- } else if (source == onlineGameButton) {
- ServerClient client = ServerClient.getInstance();
- String port = null, address = null;
- while (errorPort == true) {
- try {
- port = JOptionPane.showInputDialog("Podaj numer portu");
- if (port == null) {
- errorPort = false;
- } else {
- number = Integer.parseInt(port);
- errorPort = false;
- }
- } catch (NumberFormatException e) {
- e.printStackTrace();
- JOptionPane.showMessageDialog(null, "Błędny format numeru portu-podaj wlaściwą liczbę", "Bląd ", JOptionPane.ERROR_MESSAGE);
- errorPort = true;
- }
- }
- if (port != null)
- {
- while (errorAddress==true) {
- try {
- address = JOptionPane.showInputDialog("Podaj adres");
- if (address == null) {
- errorAddress = false;
- } else {
- serverAddress = InetAddress.getByName(address);
- errorAddress = false;
- }
- } catch (NumberFormatException e) {
- e.printStackTrace();
- JOptionPane.showMessageDialog(null, "Błędny format adresu -podaj wlaściwy", "Bląd ", JOptionPane.ERROR_MESSAGE);
- errorAddress = true;
- } catch (UnknownHostException e) {
- e.printStackTrace();
- JOptionPane.showMessageDialog(null, "Błędny format adresu -podaj wlaściwy", "Bląd ", JOptionPane.ERROR_MESSAGE);
- }
- }
- if (errorPort != true && port != null && errorAddress != true) {
- client.setOnlineMode(true);
- if (client.errorFlag != true) {
- System.out.println(client.errorFlag);
- if (client.init(serverAddress, number)) {
- new ChooseWindow();
- dispose();
- }
- }
- }
- }
- } else {
- new ChooseWindow();
- dispose();
- }
- }
- }
- // HELP WINDOW KLASA KLASUNIA ///////////////////////
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- /** Okno wyświetlające instrukcję do gry */
- public class HelpWindow extends JFrame implements ActionListener {
- /** przycisk OK */
- private JButton okButton;
- private JLabel titleLabel, textLabel;
- /** Konstruktor okna */
- public HelpWindow() {
- setMinimumSize(new Dimension(600, 600));
- Container pane = getContentPane();
- pane.setBackground(new Color(173, 123, 148));
- setDefaultCloseOperation(DISPOSE_ON_CLOSE);
- setLocationRelativeTo(null);
- setLayout(new BorderLayout());
- okButton = new JButton("OK");
- //Ustawienie parametrów titleLabel
- titleLabel = new JLabel("Lunar Lander - instrukcja ");
- titleLabel.setFont(new Font("ShowCardGothic", Font.PLAIN, 27));
- titleLabel.setForeground(Color.black);
- titleLabel.setHorizontalAlignment(JLabel.CENTER);
- textLabel.setText("<html><br><br><font size=6>Sterowanie</font size>:<br>Prawa strzałka - ruch w lewo<br></html>");
- add(titleLabel, BorderLayout.NORTH);
- add(okButton, BorderLayout.SOUTH);
- add(textLabel);
- okButton.addActionListener(this);
- setVisible(true);
- }
- /** Obsluga zdarzenia przycisku */
- public void actionPerformed(ActionEvent event) {
- Object source = event.getSource();
- if (source == okButton) {
- dispose(); // zamknięcie okna z instrukcją
- }
- }
- }
- /////////// SERVER CLIENT GOWNO KLASA KLASUNIA ////////////////////
- import javax.swing.*;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.InetAddress;
- import java.net.Socket;
- /** Klasa obsługująca stronę klienta w komunikacji z serwerem */
- public class ServerClient {
- /** Instrukcja klienta serwera wykorzystująca wzorzec Singleton */
- private static ServerClient serverClient = new ServerClient();
- /** Określa czy gra jest w trybie online */
- boolean onlineMode = false;
- /** flaga błędu */
- boolean errorFlag;
- /** odczyt ze strumienia */
- private BufferedReader input;
- /** zapis do strumienia */
- private PrintWriter output;
- private ServerClient() { errorFlag=false; }
- /** Zwraca instancję klienta */
- static ServerClient getInstance() { return serverClient; }
- /** Określa czy gra jest w trybie online - true gdy online */
- public boolean isOnlineMode() { return onlineMode; }
- /** Ustawia gracza w tryb online - onlineMode true gdy gra ma działać online */
- public void setOnlineMode(boolean onlineMode) { this.onlineMode = onlineMode; }
- /**
- * Inicjalizuje klienta /////////////////////////////////////// TU NIE WIEMY CO SIE DZIEJE
- * @param address adres na którym działa serwer
- * @param port port na którym działa serwer
- * @return true jeśli poprawnie zaincjalizowano
- */
- boolean init(InetAddress address, int port) {
- Socket socket; //errorFlag=false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement