/**
* this is the image panel class, currently not in use as it doesnt work
*/
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class ImagePanels extends JPanel {
BufferedImage img;
public Grid [][] grid = new Grid[6][6];
BattleShipsClient parent;
public ImagePanels(BattleShipsClient gui)
{
try
{
setLayout(new GridLayout(6,6));
img = ImageIO.read(new File("Battleships.jpg"));
for (int i=0;i<6;i++)
for(int j=0;j<6;j++)
add(grid[i][j] = new Grid(i,j,gui));
}catch (IOException e)
{System.err.println(e);}
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(img, 0, 0, getWidth(),getHeight(),null);
}
}
the code is within my client class, the constructore of the class is where i run my gui
public BattleShipsClient()
{
//panels for grid
JPanel p = new JPanel();
JPanel jp = new JPanel();
p.setLayout(new GridLayout(6,6,0,0));
for (int i=0;i<6;i++)
for(int j=0;j<6;j++)
p.add(grid[i][j] = new Grid(i,j,this));
/*
* a new client is created therefore they will be automatically assigned grid
*/
jp.setLayout(new GridLayout(6,6,0,0));
for (int i=0;i<6;i++)
for(int j=0;j<6;j++)
jp.add(pgrid[i][j] = new BGrid(i,j,this));
/*
* a new grid is created, so from here once the ship is drawn in one
* must create a x in another grid
*/
p.setPreferredSize(new Dimension (320,320));
jp.setPreferredSize(new Dimension (320,320));
p.setBorder(new LineBorder(Color.black,1));
jlbTitle.setHorizontalAlignment(JLabel.CENTER);
jlbTitle.setFont(new Font("SansSerif", Font.BOLD, 16));
jlbTitle.setBorder(new LineBorder(Color.black, 1));
jlbStatus.setBorder(new LineBorder(Color.black, 1));
jlbStatus.setForeground(Color.RED);
j.add(jlbTitle,BorderLayout.NORTH);
j.add(p,BorderLayout.NORTH);
j.add(jlbStatus,BorderLayout.CENTER);
j.add(jp,BorderLayout.SOUTH);
/**
* Image panels was meant to display at the background of the grid however unable to work
*/
ImagePanels ip = new ImagePanels(this);
jtb.add(ip,BorderLayout.CENTER);
jf.add(jtb);//replace j with ip
jf.setSize(1100,480);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
connectToServer();
}
This is my grid class whihc simply activates when a user clicks
/**
* this class is used to store the grid, its also used to hold the current ships
*/
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class Grid extends JPanel {
private int row;
private int column;
/*
* create anaother boolean within the ship
* once grid has been used for a ship in the choosing it cant be selected again
*/
public boolean used = false;
private BattleShipsClient parent;
private char token =' ';
public Grid(int row , int column, BattleShipsClient gui)//only grid is called everytime something is clicked
{
this.row = row;
this.column = column;
this.parent = gui;
//may have to be incorperated with the constructor
setBorder(new LineBorder(Color.black,1));
addMouseListener(new ClickListener());
}
protected void paintComponent(Graphics g)
{//each grid is treated individually, so once one box is selected it draws inside the width of that box
super.paintComponent(g);
if(parent.continueToPlay==false)
{
if(token =='x'){
g.drawOval(10, 10, getWidth()-20, getHeight()-20);//waiting here
}
else if (token =='o')
{
g.setColor(Color.green);
g.fillOval(10, 10, getWidth() -20,getHeight() - 20);
}
}
else if(parent.continueToPlay)
{
if (token == 'X') {
g.drawLine(10, 10, getWidth() - 10, getHeight() - 10);
g.drawLine(getWidth() - 10, 10, 10, getHeight() - 10);
}
else if (token == 'O') {
g.setColor(Color.RED);
g.drawLine(10, 10, getWidth() - 10, getHeight() - 10);
g.drawLine(getWidth() - 10, 10, 10, getHeight() - 10);
}
}
}
public char getToken()
{
return token;
}
public void setToken(char c)
{
token = c;
repaint();
}
private class ClickListener extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
if(parent.clickCount > 2)
{
parent.setLocation(false);
}
if(parent.getLocations()&& token ==' ')
{
parent.setRowSelected(row);
parent.setColumnSelected(column);
parent.proceed=true;
//setToken(parent.getMyToken());
}
else if(!(token==' '))
{
JOptionPane.showMessageDialog(null,"your being naughty, chose another square");
}
else
{
JOptionPane.showMessageDialog(null, "please use the attack Grid to launch an attack");
}
//commented out because clicking will not be prohibited
/*else if(token == ' '&&parent.getMyTurn())
{
//called on the click
setToken(parent.getMyToken());
parent.setMyTurn(false);
parent.setRowSelected(row);
parent.setColumnSelected(column);
//parent.setStatusMessage("Waiting for the other player to strike");
parent.setWaiting(false);
}*/
}
}
}

New Topic/Question
Reply




MultiQuote




|