I need a GUI layout for Battleship game with a 10 x10 in jav

I need a GUI layout for Battleship game with a 10 x10 in java. If you can please add some action listeners, mouse listeners, and a rollover target with the mouse.
I need a GUI layout for Battleship game with a 10 x10 in java. If you can please add some action listeners, mouse listeners, and a rollover target with the mouse.
I need a GUI layout for Battleship game with a 10 x10 in java. If you can please add some action listeners, mouse listeners, and a rollover target with the mouse.

Solution

package ships.gui; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.awt.event.MouseEvent; import ships.model.Matrix; import ships.model.Ship; import ships.model.Point; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridLayout; import java.awt.event.ActionListener; import java.awt.event.KeyListener; import java.awt.event.MouseListener; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import ships.ShipsAction; import ships.model.Logger; import ships.model.ShipStatus; /** *

The Mover is class, where is a battlefield and a {@linkplain ships.other.Ship ships}.

A * {@linkplain ships.other.Ship ships} * are moveable. You can move with tham from a mouse (click to batton) or * keyboard. Use w,a,s,d keys or arrow keys. * * If {@linkplain ships.other.Ship ships} are touching themselfs they are marked red. Active {@linkplain ships.other.Ship ships} is orange. */ public class Mover extends JPanel implements MouseListener, KeyListener { /*distance between lines*/ private int distance; /*number of culomn*/ final private int colomn=10; /* X distance between component and battlefield*/ private int fieldXstard; /*the deflaut ship color*/ private Color deflautShipColor = Color.BLUE; /*the bat location color*/ private Color badLocationColor = Color.RED; /*the active color*/ private Color activeColor = Color.ORANGE; /*the bad location active color*/ private Color active_badLocationColor = Color.RED; /**get {@linkplain ships.ShipsAction ShipsAction} instancion*/ private ShipsAction shipsAction= ShipsAction.getInstance(); /*get instance of logger*/ private Logger logger = Logger.getLogger(); /** * Add active_badLocationColor * @param normalColor * @param activeColor * @param badLocationColor * @param active_badLocationColor */ Mover(Color normalColor, Color activeColor, Color badLocationColor, Color active_badLocationColor) { this(normalColor, activeColor, badLocationColor); this.active_badLocationColor=active_badLocationColor; } /** * Set the Color of ships * @param normalColor - the deflaut ship color * @param activeColor - the active color * @param badLocationColor - the bat location color */ Mover(Color normalColor, Color activeColor, Color badLocationColor) { this(); this.deflautShipColor = normalColor; this.badLocationColor = badLocationColor; this.activeColor = activeColor; setOpaque(false); /*restart position of ships*/ shipsAction.restartShips(); } /** * Deflaut constructor. * Set mover as focusable */ Mover() { setFocusable(true); } /** * Painting mover field and {@linkplain ships.other.Ship ships}Only run test class of Mover

* @param args */ public static void main(String[] args) { new GrafikaZaklad(); } /** *

Recalculated coordinate to point in battlefied

* * First of all, must subtract coordinate component from co. of mouse. * and devide of distance * * @param mouse coordinate * @return point in battlefield */ private int coordinate2Point(int mouseCo, int componentCoPlusBattleField) { return (mouseCo-componentCoPlusBattleField)/distance; } /** * If user click to a battlefield and a {@linkplain ships.other.Ship ship} * mark it. * Must recalculated the point, where user click. * @param e */ public void mouseClicked(MouseEvent e) { //requestFocusInWindow(); shipsAction.findShip(coordinate2Point(e.getX(), getX() + fieldXstard), coordinate2Point(e.getY(), getY() + fieldXstard)); this.repaint(); } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void keyTyped(KeyEvent e) { } /** * Method represent key pressed. * @param e */ public void keyPressed(KeyEvent e) { if(!shipsAction.isActiveShip()) { System.out.println(\"Don\'t have active ship\"); return; } if(e.getKeyChar()==\'w\'|| e.getKeyCode()==38) { shipsAction.up(); } if(e.getKeyChar()==\'s\' || e.getKeyCode()==40) { shipsAction.down(); } if(e.getKeyChar()==\'a\' || e.getKeyCode()==37) { shipsAction.left(); } if(e.getKeyChar()==\'d\' || e.getKeyCode()==39) { shipsAction.right(); } if(e.getKeyCode()==32) { shipsAction.turn(); } this.repaint(); } public void keyReleased(KeyEvent e) { } } /** *

Only a test main class with main method.

* Becase this is only for test, is all writed * to constructor. I know, it is false. * @author zerog */ class GrafikaZaklad extends JFrame { private final Mover mover; GrafikaZaklad() { //ok, Name super(\"Mover test frame\"); //simple layout GridLayout layout = new GridLayout(0,1); //get content paint and set layout Container contentPane = this.getContentPane(); contentPane.setLayout(layout); //create a mover green orange red mover = new Mover(Color.GREEN, Color.ORANGE, Color.RED); mover.setBorder(BorderFactory.createLineBorder(Color.black)); //register listeners contentPane.addMouseListener(mover); mover.addKeyListener(mover); //add mover to frame contentPane.add(mover); //create panel and add to them button JPanel jPanel = new JPanel(); contentPane.add(jPanel); //set border layout BorderLayout borderLayout = new BorderLayout(5,5); jPanel.setLayout(borderLayout); //create all button JButton bLeft = new JButton(\"Left\"); JButton bUp = new JButton(\"Up\"); JButton bDown = new JButton(\"Down\"); JButton bRight = new JButton(\"Right\"); JButton bCenter = new JButton(\"Center\"); //define what doing bLeft.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { mover.keyPressed(new KeyEvent(new JFrame(), 0, 0L, 0,37,\'a\')); } }); bUp.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { mover.keyPressed(new KeyEvent(new JFrame(), 0, 0L, 0,38,\'w\')); } }); bDown.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { mover.keyPressed(new KeyEvent(new JFrame(), 0, 0L, 0,40,\'s\')); } }); bRight.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { mover.keyPressed(new KeyEvent(new JFrame(), 0, 0L, 0,39,\'d\')); } }); bCenter.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { mover.keyPressed(new KeyEvent(new JFrame(), 0, 0L, 0,32,\' \')); } }); //add button to panel jPanel.add(bRight, BorderLayout.EAST); jPanel.add(bLeft, BorderLayout.WEST); jPanel.add(bUp, BorderLayout.NORTH); jPanel.add(bDown, BorderLayout.SOUTH); jPanel.add(bCenter, BorderLayout.CENTER); //only set close operation and set visible true this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.pack(); this.setVisible(true); } }
 I need a GUI layout for Battleship game with a 10 x10 in java. If you can please add some action listeners, mouse listeners, and a rollover target with the mou
 I need a GUI layout for Battleship game with a 10 x10 in java. If you can please add some action listeners, mouse listeners, and a rollover target with the mou

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site