Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class BattleshipGame
- {
- static JFrame intro;
- static JFrame rulesframe;
- static JFrame scoresframe;
- static JFrame selection ;
- static JFrame grid;
- static JPanel intropanel;
- static JPanel rulespanel;
- static JPanel scorespanel;
- static JPanel gridselection;
- static JPanel gridpanel;
- public static void main (String [] args)
- {
- initialize ();
- }//Main Method
- public static void gridmethod (int size)
- {
- grid = new JFrame ();
- grid.setTitle("Player 1");
- grid.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
- grid.setLocationRelativeTo(null);
- gridpanel = new JPanel (new GridLayout(0, size));
- grid.getContentPane().add(gridpanel,BorderLayout.CENTER); //Puts the frame in the center of the screen
- JButton none = new JButton ();
- gridpanel.add (none);
- none.setPreferredSize(new Dimension(30, 45));
- JButton a = new JButton (" A ");
- gridpanel.add (a);
- a.setPreferredSize(new Dimension(30, 45));
- JButton b = new JButton (" B ");
- gridpanel.add (b);
- b.setPreferredSize(new Dimension(30, 45));
- JButton c = new JButton (" C ");
- gridpanel.add (c);
- c.setPreferredSize(new Dimension(30, 45));
- JButton d = new JButton (" D ");
- gridpanel.add (d);
- d.setPreferredSize(new Dimension(30,45));
- JButton e2 = new JButton (" E ");
- gridpanel.add (e2);
- e2.setPreferredSize(new Dimension(30, 45));
- JButton f = new JButton (" F ");
- gridpanel.add (f);
- f.setPreferredSize(new Dimension(30, 45));
- JButton g = new JButton (" G ");
- gridpanel.add (g);
- g.setPreferredSize(new Dimension(30, 45));
- JButton h = new JButton (" H ");
- gridpanel.add (h);
- h.setPreferredSize(new Dimension(30, 45));
- JButton i = new JButton (" I ");
- gridpanel.add (i);
- i.setPreferredSize(new Dimension(30, 45));
- JButton j = new JButton (" J ");
- gridpanel.add (j);
- i.setPreferredSize(new Dimension(30, 45));
- if (size == 13)
- {
- JButton k = new JButton (" K ");
- gridpanel.add (k);
- k.setPreferredSize(new Dimension(30, 45));
- JButton l = new JButton (" L ");
- gridpanel.add (l);
- l.setPreferredSize(new Dimension(30, 45));
- }
- if (size == 16)
- {
- JButton k = new JButton (" K ");
- gridpanel.add (k);
- k.setPreferredSize(new Dimension(30, 45));
- JButton l = new JButton (" L ");
- gridpanel.add (l);
- l.setPreferredSize(new Dimension(30, 45));
- JButton m = new JButton (" M ");
- gridpanel.add (m);
- m.setPreferredSize(new Dimension(30, 45));
- JButton n = new JButton (" N ");
- gridpanel.add (n);
- n.setPreferredSize(new Dimension(30, 45));
- JButton o = new JButton (" O ");
- gridpanel.add (o);
- o.setPreferredSize(new Dimension(30, 45));
- }
- for (int i2 = 0; i2 < size-1; i2++) //Loop for the buttons having a 10x10 grid.
- {
- JButton one = new JButton (Integer.toString (i2 + 1));
- gridpanel.add(one);
- one.setPreferredSize(new Dimension(30, 60));
- for (int j2= 0;j2 < size-1 ; j2++)
- {
- JButton button = new JButton (Integer.toString(j2) + ", " + Integer.toString(i2));
- gridpanel.add (button);
- button.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- ((JButton)e.getSource()).setEnabled(false);
- ((JButton)e.getSource()).setBackground(Color.BLUE);
- grid.validate();
- grid.repaint();
- }
- });
- }
- }
- }
- public static void initialize ()
- {
- intro = new JFrame ();
- intro.setTitle ("BattleShip Game");
- intro.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- intropanel = new JPanel ();
- intropanel.setLayout (new GridBagLayout ());
- GridBagConstraints gbc = new GridBagConstraints ();
- gbc.insets = new Insets (20,20,20,20);
- gbc.fill = GridBagConstraints.HORIZONTAL;
- JButton play = new JButton ("Play");
- gbc.gridx =3;
- gbc.gridy = 2;
- gbc.ipady = 40;
- gbc.ipadx = 40;
- intropanel.add(play,gbc);
- play.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- intro.dispose (); //Removed the orignal frame
- selection = new JFrame ("Grid Selection");
- selection.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
- gridselection = new JPanel ();
- gridselection.setLayout (new GridBagLayout ());
- GridBagConstraints gbc = new GridBagConstraints ();
- gbc.insets = new Insets (20,20,20,20);
- JLabel text = new JLabel ("Choose Your Level Of Difficulty");
- text.setFont(new Font("Verdana",1,20));
- gbc.gridx = 1;
- gbc.gridy = 0;
- gridselection.add (text,gbc);
- JButton selec1 = new JButton ("Easy (10x10)");
- gbc.gridx = 1;
- gbc.gridy = 1;
- gridselection.add (selec1,gbc);
- selec1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- ((JButton)e.getSource()).setEnabled(false);
- selection.dispose ();
- int size = 11;
- gridmethod (size);
- grid.pack();
- grid.setLocationRelativeTo (null);
- grid.setVisible (true);
- intro.validate();
- intro.repaint();
- }
- });
- JButton selec2 = new JButton ("Medium (12x12)");
- gbc.gridx = 1;
- gbc.gridy = 2;
- gridselection.add (selec2,gbc);
- selec2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- ((JButton)e.getSource()).setEnabled(false);
- selection.dispose ();
- int size = 13;
- gridmethod (size);
- grid.pack();
- grid.setLocationRelativeTo (null);
- grid.setVisible (true);
- intro.validate();
- intro.repaint();
- }
- });
- JButton selec3 = new JButton ("Hard (15x15)");
- gbc.gridx = 1;
- gbc.gridy = 3;
- gridselection.add (selec3,gbc);
- selec3.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- ((JButton)e.getSource()).setEnabled(false);
- selection.dispose ();
- int size = 16;
- gridmethod (size);
- grid.pack();
- grid.setLocationRelativeTo (null);
- grid.setVisible (true);
- intro.validate();
- intro.repaint();
- }
- });
- selection.add (gridselection);
- selection.pack ();
- selection.setLocationRelativeTo (null);
- selection.setVisible (true);
- //Showing Both Frames
- intro.validate();
- intro.repaint();
- }
- });
- JButton rules = new JButton ("Rules");
- gbc.gridx = 2;
- gbc.gridy = 2;
- gbc.ipady = 40;
- gbc.ipadx = 40;
- intropanel.add(rules,gbc);
- rules.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- intro.dispose ();
- rulesframe = new JFrame ("Rules");
- rulesframe.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
- rulespanel = new JPanel ();
- rulespanel.setLayout (new GridBagLayout ());
- GridBagConstraints gbc = new GridBagConstraints ();
- gbc.insets = new Insets (20,20,20,20);
- JLabel text = new JLabel ("Welcome to Battleship Game");
- gbc.gridx= 0;
- gbc.gridy = 0;
- text.setFont(new Font("Verdana",1,30));
- text.setHorizontalAlignment (JLabel.CENTER);
- text.setVerticalAlignment (JLabel.CENTER);
- rulespanel.add(text,gbc);
- JLabel text2 = new JLabel ("The rules are very simple and are as follows:");
- gbc.gridx= 0;
- gbc.gridy = 1;
- text2.setFont(new Font("Verdana",1,20));
- text2.setHorizontalAlignment (JLabel.CENTER);
- text2.setVerticalAlignment (JLabel.CENTER);
- rulespanel.add(text2,gbc);
- JLabel text3 = new JLabel ("1. There are two players who have seprate playing areas");
- gbc.gridx= 0;
- gbc.gridy = 2;
- text3.setFont(new Font("Verdana",1,20));
- text3.setHorizontalAlignment (JLabel.CENTER);
- text3.setVerticalAlignment (JLabel.CENTER);
- rulespanel.add(text3,gbc);
- JLabel text4 = new JLabel ("2. You organize your fleet of ships by clicking a posistion on the grid");
- gbc.gridx= 0;
- gbc.gridy = 3;
- text4.setFont(new Font("Verdana",1,20));
- text4.setHorizontalAlignment (JLabel.CENTER);
- text4.setVerticalAlignment (JLabel.CENTER);
- rulespanel.add(text4,gbc);
- JLabel text5 = new JLabel ("3.A hit is represented by green, a Miss is represented by red");
- gbc.gridx= 0;
- gbc.gridy = 4;
- text5.setFont(new Font("Verdana",1,20));
- text5.setHorizontalAlignment (JLabel.CENTER);
- text5.setVerticalAlignment (JLabel.CENTER);
- rulespanel.add(text5,gbc);
- JLabel text6 = new JLabel ("4.To attack the enemy when it is your turn, simply click the tile on the enemy screen");
- gbc.gridx= 0;
- gbc.gridy = 5;
- text6.setFont(new Font("Verdana",1,20));
- text6.setHorizontalAlignment (JLabel.CENTER);
- text6.setVerticalAlignment (JLabel.CENTER);
- rulespanel.add(text6,gbc);
- JLabel text7 = new JLabel ("5. A win is 100 points and bonus points will be awared for less shots taken");
- gbc.gridx= 0;
- gbc.gridy = 6;
- text7.setFont(new Font("Verdana",1,20));
- text7.setHorizontalAlignment (JLabel.CENTER);
- text7.setVerticalAlignment (JLabel.CENTER);
- rulespanel.add(text7,gbc);
- JLabel text8 = new JLabel ("Under 15 shots taken a bonus of 50 points will be added");
- gbc.gridx= 0;
- gbc.gridy = 7;
- text8.setFont(new Font("Verdana",1,20));
- text8.setHorizontalAlignment (JLabel.CENTER);
- text8.setVerticalAlignment (JLabel.CENTER);
- rulespanel.add(text8,gbc);
- JLabel text9 = new JLabel ("Under 20 shots taken a bonus of 25 points will be added");
- gbc.gridx= 0;
- gbc.gridy = 8;
- text9.setFont(new Font("Verdana",1,20));
- text9.setHorizontalAlignment (JLabel.CENTER);
- text9.setVerticalAlignment (JLabel.CENTER);
- rulespanel.add(text9,gbc);
- JLabel text10 = new JLabel ("6. The most important rule is to have fun!!");
- gbc.gridx= 0;
- gbc.gridy = 9;
- text10.setFont(new Font("Verdana",1,20));
- text10.setHorizontalAlignment (JLabel.CENTER);
- text10.setVerticalAlignment (JLabel.CENTER);
- rulespanel.add(text10,gbc);
- JButton goback = new JButton ("Go Back");
- gbc.gridx = 0;
- gbc.gridy = 10;
- rulespanel.add(goback,gbc);
- goback.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- rulesframe.dispose ();
- ((JButton)e.getSource()).setEnabled(false);
- initialize ();
- }
- });
- rulesframe.add(rulespanel);
- rulesframe.pack ();
- rulesframe.setLocationRelativeTo (null);
- rulesframe.setVisible (true);
- }
- });
- JButton scores = new JButton ("HighScores");
- gbc.gridx = 4;
- gbc.gridy = 2;
- gbc.ipady = 40;
- gbc.ipadx = 40;
- intropanel.add(scores,gbc);
- scores.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- ((JButton)e.getSource()).setEnabled(false);
- intro.dispose ();
- scoresframe = new JFrame ("Rules");
- scoresframe.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
- scorespanel = new JPanel ();
- scorespanel.setLayout (new GridBagLayout ());
- GridBagConstraints gbc = new GridBagConstraints ();
- gbc.insets = new Insets (20,20,20,20);
- JLabel highscores = new JLabel ("Highscores");
- highscores.setFont (new Font ("Verdana",1,20));
- gbc.gridx = 2;
- gbc.gridy = 1;
- scorespanel.add (highscores,gbc);
- JButton goback2 = new JButton ("Go Back ");
- gbc.gridx = 2;
- gbc.gridy = 2;
- scorespanel.add (goback2,gbc);
- goback2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- scoresframe.dispose ();
- ((JButton)e.getSource()).setEnabled(false);
- initialize ();
- }
- });
- scoresframe.add(scorespanel);
- scoresframe.pack ();
- scoresframe.setLocationRelativeTo (null);
- scoresframe.setVisible (true);
- }
- }); JLabel label = new JLabel("Battleship Game");
- label.setFont(new Font("Verdana",1,20));
- label.setHorizontalAlignment (JLabel.CENTER);
- label.setVerticalAlignment (JLabel.CENTER);
- gbc.gridx = 3;
- gbc.gridy = 1;
- gbc.ipady = 0;
- gbc.ipadx = 0;
- intropanel.add(label,gbc);
- JLabel background = new JLabel(new ImageIcon("242-2420673_battleships-will-be-used-in-a-similar-fashion.png"));
- gbc.gridx = 3;
- gbc.gridy = 0;
- intropanel.add (background, gbc);
- intro.add(intropanel);
- intro.pack ();
- intro.setLocationRelativeTo (null);
- intro.setVisible (true);
- }
- }//Public Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement