Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.48 KB | None | 0 0
  1. // START WINDOW KLASA KLASUNIA
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.ComponentEvent;
  8. import java.awt.event.ComponentListener;
  9. import java.net.InetAddress;
  10. import java.net.UnknownHostException;
  11.  
  12. public class StartWindow extends JFrame implements ActionListener {
  13.  
  14. /** włącza grę w tryb offline */
  15. private JButton offlineGameButton;
  16.  
  17. /** włącza grę w tryb online */
  18. private JButton onlineGameButton;
  19.  
  20. /**przycisk pomocy*/
  21. private JButton helpButton;
  22.  
  23. /** Napis powitalny */
  24. private JLabel welcomeLabel;
  25.  
  26. /**Ikona pomocy*/
  27. private Icon helpButtonIcon;
  28.  
  29. /**panel obrazu głównego*/
  30. private ImagePanel imagePanel;
  31. private SpringLayout springLayout = new SpringLayout();
  32.  
  33. /** Konstruktor - tworzenie i inicjalizowane cale okno startowe*/
  34. public StartWindow() {
  35.  
  36. setPreferredSize(new Dimension(700, 700));
  37. setMinimumSize(new Dimension(500,500));
  38. setTitle("Lunar Lander");
  39. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  40.  
  41. offlineGameButton = new JButton("Gra offline");
  42. onlineGameButton = new JButton("Gra online");
  43. welcomeLabel = new JLabel("Witaj w grze Lunar Lander. Wybierz tryb gry.");
  44.  
  45. welcomeLabel.setFont(new Font("TimesNewRoman", Font.PLAIN, 20));
  46. welcomeLabel.setForeground(Color.WHITE);
  47.  
  48. imagePanel = new ImagePanel("res/images/StartScreenBg.png");
  49. helpButtonIcon = new ImageIcon("res/images/helpButton.png");
  50.  
  51. helpButton.setOpaque(false); //nieprzezroczysty-wyłącz
  52. helpButton.setContentAreaFilled(false); //wypełnienie-wyłącz
  53. helpButton.setBorderPainted(false); //obramowanie - wyłącz
  54. setLayout(springLayout);
  55. Container pane = getContentPane();
  56.  
  57. //koordynaty helpButton
  58. springLayout.putConstraint(SpringLayout.NORTH, helpButton, 0, springLayout.NORTH, pane);
  59. springLayout.putConstraint(SpringLayout.EAST, helpButton, 0, SpringLayout.EAST, pane);
  60.  
  61. //koordynaty welcomeLabel
  62. springLayout.putConstraint(SpringLayout.SOUTH, welcomeLabel, -70, SpringLayout.SOUTH, pane);
  63. springLayout.putConstraint(SpringLayout.HORIZONTAL_CENTER, welcomeLabel, 0, SpringLayout.HORIZONTAL_CENTER, pane);
  64.  
  65. //koordynaty offlineGameButton
  66. springLayout.putConstraint(SpringLayout.SOUTH, offlineGameButton, -40, SpringLayout.SOUTH, pane);
  67. springLayout.putConstraint(SpringLayout.HORIZONTAL_CENTER, offlineGameButton, 0, SpringLayout.HORIZONTAL_CENTER, pane);
  68.  
  69. //koordynaty onlineGameButton
  70. springLayout.putConstraint(SpringLayout.HORIZONTAL_CENTER, onlineGameButton, 0, SpringLayout.HORIZONTAL_CENTER, pane);
  71. springLayout.putConstraint(SpringLayout.SOUTH, onlineGameButton, -10, SpringLayout.SOUTH, pane);
  72.  
  73. pane.add(welcomeLabel, SpringLayout.NORTH);
  74. pane.add(helpButton);
  75. pane.add(offlineGameButton);
  76. pane.add(onlineGameButton);
  77.  
  78. add(imagePanel);
  79.  
  80. onlineGameButton.setToolTipText("Rozpocznij rozgrywke, pobierając dane z servera");
  81. offlineGameButton.setToolTipText("Rozpocznij rozgrywke korzystając z lokalnych danych");
  82. helpButton.setToolTipText("Nie wiesz jak grac? Kliknij :)");
  83.  
  84. helpButton.addActionListener(this);
  85. offlineGameButton.addActionListener(this);
  86. onlineGameButton.addActionListener(this);
  87. //this - odniesienie do bieżącego obiektu
  88.  
  89. //Gdy okno zmienia swój rozmiar tworzone jest zdarzenie i wtedy aktualizowany jest rozmiar obrazu
  90. //będącego jego tłem
  91. addComponentListener(new ComponentListener() {
  92. @Override
  93. public void componentResized(ComponentEvent e) {
  94. imagePanel.setSize(getWidth(), getHeight());
  95. imagePanel.repaint();
  96. repaint();
  97. }
  98.  
  99. @Override
  100. public void componentMoved(ComponentEvent e) {
  101.  
  102. }
  103.  
  104. @Override
  105. public void componentShown(ComponentEvent e) {
  106.  
  107. }
  108.  
  109. @Override
  110. public void componentHidden(ComponentEvent e) {
  111.  
  112. }
  113. });
  114. pack();
  115. setLocationRelativeTo(null);
  116.  
  117. setVisible(true);
  118. }
  119.  
  120. /**
  121. *
  122. * Obsługa zdarzeń
  123. * @param event
  124. */
  125. @Override
  126. public void actionPerformed(ActionEvent event) {
  127. Object source = event.getSource();
  128. boolean errorPort = true;
  129. boolean errorAddress = true;
  130. int number=0;
  131. InetAddress serverAddress = null;
  132. if (source == helpButton) {
  133. new HelpWindow();
  134. } else if (source == onlineGameButton) {
  135. ServerClient client = ServerClient.getInstance();
  136. String port = null, address = null;
  137.  
  138.  
  139. while (errorPort == true) {
  140.  
  141. try {
  142. port = JOptionPane.showInputDialog("Podaj numer portu");
  143. if (port == null) {
  144. errorPort = false;
  145.  
  146. } else {
  147. number = Integer.parseInt(port);
  148. errorPort = false;
  149. }
  150. } catch (NumberFormatException e) {
  151. e.printStackTrace();
  152. JOptionPane.showMessageDialog(null, "Błędny format numeru portu-podaj wlaściwą liczbę", "Bląd ", JOptionPane.ERROR_MESSAGE);
  153. errorPort = true;
  154.  
  155. }
  156.  
  157. }
  158.  
  159. if (port != null)
  160. {
  161.  
  162. while (errorAddress==true) {
  163. try {
  164. address = JOptionPane.showInputDialog("Podaj adres");
  165. if (address == null) {
  166.  
  167. errorAddress = false;
  168.  
  169. } else {
  170. serverAddress = InetAddress.getByName(address);
  171. errorAddress = false;
  172. }
  173. } catch (NumberFormatException e) {
  174. e.printStackTrace();
  175. JOptionPane.showMessageDialog(null, "Błędny format adresu -podaj wlaściwy", "Bląd ", JOptionPane.ERROR_MESSAGE);
  176. errorAddress = true;
  177.  
  178. } catch (UnknownHostException e) {
  179. e.printStackTrace();
  180. JOptionPane.showMessageDialog(null, "Błędny format adresu -podaj wlaściwy", "Bląd ", JOptionPane.ERROR_MESSAGE);
  181. }
  182. }
  183. if (errorPort != true && port != null && errorAddress != true) {
  184.  
  185. client.setOnlineMode(true);
  186. if (client.errorFlag != true) {
  187. System.out.println(client.errorFlag);
  188. if (client.init(serverAddress, number)) {
  189. new ChooseWindow();
  190. dispose();
  191. }
  192.  
  193. }
  194. }
  195. }
  196.  
  197. } else {
  198. new ChooseWindow();
  199. dispose();
  200. }
  201.  
  202. }
  203.  
  204. }
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211. // HELP WINDOW KLASA KLASUNIA ///////////////////////
  212.  
  213.  
  214.  
  215. import javax.swing.*;
  216. import java.awt.*;
  217. import java.awt.event.ActionEvent;
  218. import java.awt.event.ActionListener;
  219.  
  220. /** Okno wyświetlające instrukcję do gry */
  221.  
  222. public class HelpWindow extends JFrame implements ActionListener {
  223.  
  224. /** przycisk OK */
  225. private JButton okButton;
  226. private JLabel titleLabel, textLabel;
  227.  
  228. /** Konstruktor okna */
  229. public HelpWindow() {
  230. setMinimumSize(new Dimension(600, 600));
  231. Container pane = getContentPane();
  232. pane.setBackground(new Color(173, 123, 148));
  233. setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  234. setLocationRelativeTo(null);
  235. setLayout(new BorderLayout());
  236.  
  237. okButton = new JButton("OK");
  238.  
  239. //Ustawienie parametrów titleLabel
  240. titleLabel = new JLabel("Lunar Lander - instrukcja ");
  241. titleLabel.setFont(new Font("ShowCardGothic", Font.PLAIN, 27));
  242. titleLabel.setForeground(Color.black);
  243.  
  244. titleLabel.setHorizontalAlignment(JLabel.CENTER);
  245.  
  246. textLabel.setText("<html><br><br><font size=6>Sterowanie</font size>:<br>Prawa strzałka - ruch w lewo<br></html>");
  247.  
  248. add(titleLabel, BorderLayout.NORTH);
  249. add(okButton, BorderLayout.SOUTH);
  250. add(textLabel);
  251.  
  252. okButton.addActionListener(this);
  253. setVisible(true);
  254.  
  255. }
  256.  
  257. /** Obsluga zdarzenia przycisku */
  258.  
  259. public void actionPerformed(ActionEvent event) {
  260.  
  261. Object source = event.getSource();
  262.  
  263. if (source == okButton) {
  264.  
  265. dispose(); // zamknięcie okna z instrukcją
  266.  
  267. }
  268.  
  269. }
  270.  
  271.  
  272. }
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284. /////////// SERVER CLIENT GOWNO KLASA KLASUNIA ////////////////////
  285.  
  286.  
  287.  
  288. import javax.swing.*;
  289. import java.io.BufferedReader;
  290. import java.io.IOException;
  291. import java.io.InputStreamReader;
  292. import java.io.PrintWriter;
  293. import java.net.InetAddress;
  294. import java.net.Socket;
  295.  
  296. /** Klasa obsługująca stronę klienta w komunikacji z serwerem */
  297.  
  298. public class ServerClient {
  299.  
  300. /** Instrukcja klienta serwera wykorzystująca wzorzec Singleton */
  301. private static ServerClient serverClient = new ServerClient();
  302.  
  303. /** Określa czy gra jest w trybie online */
  304. boolean onlineMode = false;
  305.  
  306. /** flaga błędu */
  307. boolean errorFlag;
  308.  
  309. /** odczyt ze strumienia */
  310. private BufferedReader input;
  311.  
  312. /** zapis do strumienia */
  313. private PrintWriter output;
  314.  
  315. private ServerClient() { errorFlag=false; }
  316.  
  317. /** Zwraca instancję klienta */
  318. static ServerClient getInstance() { return serverClient; }
  319.  
  320. /** Określa czy gra jest w trybie online - true gdy online */
  321. public boolean isOnlineMode() { return onlineMode; }
  322.  
  323. /** Ustawia gracza w tryb online - onlineMode true gdy gra ma działać online */
  324. public void setOnlineMode(boolean onlineMode) { this.onlineMode = onlineMode; }
  325.  
  326.  
  327. /**
  328. * Inicjalizuje klienta /////////////////////////////////////// TU NIE WIEMY CO SIE DZIEJE
  329. * @param address adres na którym działa serwer
  330. * @param port port na którym działa serwer
  331. * @return true jeśli poprawnie zaincjalizowano
  332. */
  333. boolean init(InetAddress address, int port) {
  334. Socket socket; //errorFlag=false;
  335.  
  336. }
  337.  
  338. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement