Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.28 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5.  
  6. public class BattleshipGame
  7. {
  8. static JFrame intro;
  9. static JFrame rulesframe;
  10. static JFrame scoresframe;
  11. static JFrame selection ;
  12. static JFrame grid;
  13. static JPanel intropanel;
  14. static JPanel rulespanel;
  15. static JPanel scorespanel;
  16. static JPanel gridselection;
  17. static JPanel gridpanel;
  18.  
  19.  
  20. public static void main (String [] args)
  21. {
  22.  
  23. initialize ();
  24.  
  25. }//Main Method
  26.  
  27. public static void gridmethod (int size)
  28. {
  29. grid = new JFrame ();
  30. grid.setTitle("Player 1");
  31. grid.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  32. grid.setLocationRelativeTo(null);
  33. gridpanel = new JPanel (new GridLayout(0, size));
  34. grid.getContentPane().add(gridpanel,BorderLayout.CENTER); //Puts the frame in the center of the screen
  35.  
  36. JButton none = new JButton ();
  37. gridpanel.add (none);
  38. none.setPreferredSize(new Dimension(30, 45));
  39. JButton a = new JButton (" A ");
  40. gridpanel.add (a);
  41. a.setPreferredSize(new Dimension(30, 45));
  42. JButton b = new JButton (" B ");
  43. gridpanel.add (b);
  44. b.setPreferredSize(new Dimension(30, 45));
  45. JButton c = new JButton (" C ");
  46. gridpanel.add (c);
  47. c.setPreferredSize(new Dimension(30, 45));
  48. JButton d = new JButton (" D ");
  49. gridpanel.add (d);
  50. d.setPreferredSize(new Dimension(30,45));
  51. JButton e2 = new JButton (" E ");
  52. gridpanel.add (e2);
  53. e2.setPreferredSize(new Dimension(30, 45));
  54. JButton f = new JButton (" F ");
  55. gridpanel.add (f);
  56. f.setPreferredSize(new Dimension(30, 45));
  57. JButton g = new JButton (" G ");
  58. gridpanel.add (g);
  59. g.setPreferredSize(new Dimension(30, 45));
  60. JButton h = new JButton (" H ");
  61. gridpanel.add (h);
  62. h.setPreferredSize(new Dimension(30, 45));
  63. JButton i = new JButton (" I ");
  64. gridpanel.add (i);
  65. i.setPreferredSize(new Dimension(30, 45));
  66. JButton j = new JButton (" J ");
  67. gridpanel.add (j);
  68. i.setPreferredSize(new Dimension(30, 45));
  69. if (size == 13)
  70. {
  71. JButton k = new JButton (" K ");
  72. gridpanel.add (k);
  73. k.setPreferredSize(new Dimension(30, 45));
  74. JButton l = new JButton (" L ");
  75. gridpanel.add (l);
  76. l.setPreferredSize(new Dimension(30, 45));
  77. }
  78. if (size == 16)
  79. {
  80. JButton k = new JButton (" K ");
  81. gridpanel.add (k);
  82. k.setPreferredSize(new Dimension(30, 45));
  83. JButton l = new JButton (" L ");
  84. gridpanel.add (l);
  85. l.setPreferredSize(new Dimension(30, 45));
  86. JButton m = new JButton (" M ");
  87. gridpanel.add (m);
  88. m.setPreferredSize(new Dimension(30, 45));
  89. JButton n = new JButton (" N ");
  90. gridpanel.add (n);
  91. n.setPreferredSize(new Dimension(30, 45));
  92. JButton o = new JButton (" O ");
  93. gridpanel.add (o);
  94. o.setPreferredSize(new Dimension(30, 45));
  95. }
  96.  
  97. for (int i2 = 0; i2 < size-1; i2++) //Loop for the buttons having a 10x10 grid.
  98. {
  99. JButton one = new JButton (Integer.toString (i2 + 1));
  100. gridpanel.add(one);
  101. one.setPreferredSize(new Dimension(30, 60));
  102. for (int j2= 0;j2 < size-1 ; j2++)
  103. {
  104.  
  105.  
  106. JButton button = new JButton (Integer.toString(j2) + ", " + Integer.toString(i2));
  107. gridpanel.add (button);
  108. button.addActionListener(new ActionListener() {
  109. public void actionPerformed(ActionEvent e) {
  110.  
  111.  
  112. ((JButton)e.getSource()).setEnabled(false);
  113. ((JButton)e.getSource()).setBackground(Color.BLUE);
  114. grid.validate();
  115. grid.repaint();
  116. }
  117. });
  118. }
  119. }
  120.  
  121. }
  122. public static void initialize ()
  123. {
  124.  
  125. intro = new JFrame ();
  126. intro.setTitle ("BattleShip Game");
  127. intro.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  128. intropanel = new JPanel ();
  129. intropanel.setLayout (new GridBagLayout ());
  130. GridBagConstraints gbc = new GridBagConstraints ();
  131. gbc.insets = new Insets (20,20,20,20);
  132. gbc.fill = GridBagConstraints.HORIZONTAL;
  133.  
  134.  
  135.  
  136. JButton play = new JButton ("Play");
  137. gbc.gridx =3;
  138. gbc.gridy = 2;
  139. gbc.ipady = 40;
  140. gbc.ipadx = 40;
  141. intropanel.add(play,gbc);
  142. play.addActionListener(new ActionListener() {
  143. public void actionPerformed(ActionEvent e) {
  144. intro.dispose (); //Removed the orignal frame
  145.  
  146. selection = new JFrame ("Grid Selection");
  147. selection.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  148. gridselection = new JPanel ();
  149. gridselection.setLayout (new GridBagLayout ());
  150. GridBagConstraints gbc = new GridBagConstraints ();
  151. gbc.insets = new Insets (20,20,20,20);
  152. JLabel text = new JLabel ("Choose Your Level Of Difficulty");
  153. text.setFont(new Font("Verdana",1,20));
  154. gbc.gridx = 1;
  155. gbc.gridy = 0;
  156. gridselection.add (text,gbc);
  157. JButton selec1 = new JButton ("Easy (10x10)");
  158. gbc.gridx = 1;
  159. gbc.gridy = 1;
  160. gridselection.add (selec1,gbc);
  161. selec1.addActionListener(new ActionListener() {
  162. public void actionPerformed(ActionEvent e) {
  163.  
  164.  
  165. ((JButton)e.getSource()).setEnabled(false);
  166. selection.dispose ();
  167. int size = 11;
  168. gridmethod (size);
  169.  
  170.  
  171. grid.pack();
  172. grid.setLocationRelativeTo (null);
  173. grid.setVisible (true);
  174. intro.validate();
  175. intro.repaint();
  176. }
  177. });
  178. JButton selec2 = new JButton ("Medium (12x12)");
  179. gbc.gridx = 1;
  180. gbc.gridy = 2;
  181. gridselection.add (selec2,gbc);
  182. selec2.addActionListener(new ActionListener() {
  183. public void actionPerformed(ActionEvent e) {
  184.  
  185.  
  186. ((JButton)e.getSource()).setEnabled(false);
  187. selection.dispose ();
  188. int size = 13;
  189. gridmethod (size);
  190.  
  191. grid.pack();
  192. grid.setLocationRelativeTo (null);
  193. grid.setVisible (true);
  194. intro.validate();
  195. intro.repaint();
  196. }
  197. });
  198. JButton selec3 = new JButton ("Hard (15x15)");
  199. gbc.gridx = 1;
  200. gbc.gridy = 3;
  201. gridselection.add (selec3,gbc);
  202. selec3.addActionListener(new ActionListener() {
  203. public void actionPerformed(ActionEvent e) {
  204.  
  205.  
  206. ((JButton)e.getSource()).setEnabled(false);
  207. selection.dispose ();
  208. int size = 16;
  209. gridmethod (size);
  210.  
  211. grid.pack();
  212. grid.setLocationRelativeTo (null);
  213. grid.setVisible (true);
  214. intro.validate();
  215. intro.repaint();
  216. }
  217. });
  218.  
  219.  
  220.  
  221.  
  222. selection.add (gridselection);
  223. selection.pack ();
  224. selection.setLocationRelativeTo (null);
  225. selection.setVisible (true);
  226.  
  227.  
  228.  
  229. //Showing Both Frames
  230.  
  231.  
  232. intro.validate();
  233. intro.repaint();
  234. }
  235. });
  236.  
  237. JButton rules = new JButton ("Rules");
  238. gbc.gridx = 2;
  239. gbc.gridy = 2;
  240. gbc.ipady = 40;
  241. gbc.ipadx = 40;
  242.  
  243. intropanel.add(rules,gbc);
  244. rules.addActionListener(new ActionListener() {
  245. public void actionPerformed(ActionEvent e) {
  246. intro.dispose ();
  247. rulesframe = new JFrame ("Rules");
  248. rulesframe.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  249.  
  250. rulespanel = new JPanel ();
  251. rulespanel.setLayout (new GridBagLayout ());
  252. GridBagConstraints gbc = new GridBagConstraints ();
  253. gbc.insets = new Insets (20,20,20,20);
  254. JLabel text = new JLabel ("Welcome to Battleship Game");
  255. gbc.gridx= 0;
  256. gbc.gridy = 0;
  257. text.setFont(new Font("Verdana",1,30));
  258. text.setHorizontalAlignment (JLabel.CENTER);
  259. text.setVerticalAlignment (JLabel.CENTER);
  260. rulespanel.add(text,gbc);
  261. JLabel text2 = new JLabel ("The rules are very simple and are as follows:");
  262. gbc.gridx= 0;
  263. gbc.gridy = 1;
  264. text2.setFont(new Font("Verdana",1,20));
  265. text2.setHorizontalAlignment (JLabel.CENTER);
  266. text2.setVerticalAlignment (JLabel.CENTER);
  267. rulespanel.add(text2,gbc);
  268. JLabel text3 = new JLabel ("1. There are two players who have seprate playing areas");
  269. gbc.gridx= 0;
  270. gbc.gridy = 2;
  271. text3.setFont(new Font("Verdana",1,20));
  272. text3.setHorizontalAlignment (JLabel.CENTER);
  273. text3.setVerticalAlignment (JLabel.CENTER);
  274. rulespanel.add(text3,gbc);
  275. JLabel text4 = new JLabel ("2. You organize your fleet of ships by clicking a posistion on the grid");
  276. gbc.gridx= 0;
  277. gbc.gridy = 3;
  278. text4.setFont(new Font("Verdana",1,20));
  279. text4.setHorizontalAlignment (JLabel.CENTER);
  280. text4.setVerticalAlignment (JLabel.CENTER);
  281.  
  282. rulespanel.add(text4,gbc);
  283. JLabel text5 = new JLabel ("3.A hit is represented by green, a Miss is represented by red");
  284. gbc.gridx= 0;
  285. gbc.gridy = 4;
  286. text5.setFont(new Font("Verdana",1,20));
  287. text5.setHorizontalAlignment (JLabel.CENTER);
  288. text5.setVerticalAlignment (JLabel.CENTER);
  289. rulespanel.add(text5,gbc);
  290. JLabel text6 = new JLabel ("4.To attack the enemy when it is your turn, simply click the tile on the enemy screen");
  291. gbc.gridx= 0;
  292. gbc.gridy = 5;
  293. text6.setFont(new Font("Verdana",1,20));
  294. text6.setHorizontalAlignment (JLabel.CENTER);
  295. text6.setVerticalAlignment (JLabel.CENTER);
  296. rulespanel.add(text6,gbc);
  297. JLabel text7 = new JLabel ("5. A win is 100 points and bonus points will be awared for less shots taken");
  298. gbc.gridx= 0;
  299. gbc.gridy = 6;
  300. text7.setFont(new Font("Verdana",1,20));
  301. text7.setHorizontalAlignment (JLabel.CENTER);
  302. text7.setVerticalAlignment (JLabel.CENTER);
  303. rulespanel.add(text7,gbc);
  304. JLabel text8 = new JLabel ("Under 15 shots taken a bonus of 50 points will be added");
  305. gbc.gridx= 0;
  306. gbc.gridy = 7;
  307. text8.setFont(new Font("Verdana",1,20));
  308. text8.setHorizontalAlignment (JLabel.CENTER);
  309. text8.setVerticalAlignment (JLabel.CENTER);
  310. rulespanel.add(text8,gbc);
  311. JLabel text9 = new JLabel ("Under 20 shots taken a bonus of 25 points will be added");
  312. gbc.gridx= 0;
  313. gbc.gridy = 8;
  314. text9.setFont(new Font("Verdana",1,20));
  315. text9.setHorizontalAlignment (JLabel.CENTER);
  316. text9.setVerticalAlignment (JLabel.CENTER);
  317. rulespanel.add(text9,gbc);
  318. JLabel text10 = new JLabel ("6. The most important rule is to have fun!!");
  319. gbc.gridx= 0;
  320. gbc.gridy = 9;
  321. text10.setFont(new Font("Verdana",1,20));
  322. text10.setHorizontalAlignment (JLabel.CENTER);
  323. text10.setVerticalAlignment (JLabel.CENTER);
  324. rulespanel.add(text10,gbc);
  325. JButton goback = new JButton ("Go Back");
  326. gbc.gridx = 0;
  327. gbc.gridy = 10;
  328. rulespanel.add(goback,gbc);
  329. goback.addActionListener(new ActionListener() {
  330. public void actionPerformed(ActionEvent e) {
  331.  
  332. rulesframe.dispose ();
  333. ((JButton)e.getSource()).setEnabled(false);
  334. initialize ();
  335.  
  336. }
  337. });
  338.  
  339. rulesframe.add(rulespanel);
  340.  
  341. rulesframe.pack ();
  342. rulesframe.setLocationRelativeTo (null);
  343. rulesframe.setVisible (true);
  344.  
  345. }
  346. });
  347.  
  348. JButton scores = new JButton ("HighScores");
  349. gbc.gridx = 4;
  350. gbc.gridy = 2;
  351. gbc.ipady = 40;
  352. gbc.ipadx = 40;
  353. intropanel.add(scores,gbc);
  354. scores.addActionListener(new ActionListener() {
  355. public void actionPerformed(ActionEvent e) {
  356. ((JButton)e.getSource()).setEnabled(false);
  357. intro.dispose ();
  358. scoresframe = new JFrame ("Rules");
  359. scoresframe.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  360.  
  361. scorespanel = new JPanel ();
  362. scorespanel.setLayout (new GridBagLayout ());
  363. GridBagConstraints gbc = new GridBagConstraints ();
  364. gbc.insets = new Insets (20,20,20,20);
  365.  
  366. JLabel highscores = new JLabel ("Highscores");
  367. highscores.setFont (new Font ("Verdana",1,20));
  368. gbc.gridx = 2;
  369. gbc.gridy = 1;
  370. scorespanel.add (highscores,gbc);
  371. JButton goback2 = new JButton ("Go Back ");
  372. gbc.gridx = 2;
  373. gbc.gridy = 2;
  374. scorespanel.add (goback2,gbc);
  375. goback2.addActionListener(new ActionListener() {
  376. public void actionPerformed(ActionEvent e) {
  377.  
  378. scoresframe.dispose ();
  379. ((JButton)e.getSource()).setEnabled(false);
  380. initialize ();
  381.  
  382. }
  383. });
  384.  
  385.  
  386. scoresframe.add(scorespanel);
  387. scoresframe.pack ();
  388. scoresframe.setLocationRelativeTo (null);
  389. scoresframe.setVisible (true);
  390. }
  391. }); JLabel label = new JLabel("Battleship Game");
  392. label.setFont(new Font("Verdana",1,20));
  393. label.setHorizontalAlignment (JLabel.CENTER);
  394. label.setVerticalAlignment (JLabel.CENTER);
  395. gbc.gridx = 3;
  396. gbc.gridy = 1;
  397. gbc.ipady = 0;
  398. gbc.ipadx = 0;
  399. intropanel.add(label,gbc);
  400.  
  401. JLabel background = new JLabel(new ImageIcon("242-2420673_battleships-will-be-used-in-a-similar-fashion.png"));
  402. gbc.gridx = 3;
  403. gbc.gridy = 0;
  404.  
  405. intropanel.add (background, gbc);
  406.  
  407. intro.add(intropanel);
  408. intro.pack ();
  409. intro.setLocationRelativeTo (null);
  410. intro.setVisible (true);
  411. }
  412. }//Public Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement