1 Replies - 196 Views - Last Post: 22 January 2013 - 05:47 AM Rate Topic: -----

#1 skyerz  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 211
  • Joined: 14-October 10

problem getting a image inline with a grid

Posted 22 January 2013 - 02:53 AM

I have a grid class used for a battleship game, all is well however i wish to get the grid to sit ontop of a image so it gives the impression to the player that its an actual battleship grid, however my efforts have proven uselss as its either the image doesnt show or the grid becomes to small
/**
 * 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);
			}*/
		}
		
	}
	
	

}



Is This A Good Question/Topic? 0
  • +

Replies To: problem getting a image inline with a grid

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2717
  • View blog
  • Posts: 11,428
  • Joined: 20-September 08

Re: problem getting a image inline with a grid

Posted 22 January 2013 - 05:47 AM

You might be better off setting the grid component as the glass pane of your UI. See
http://docs.oracle.c...s/rootpane.html

Would also help were you to attach your image file(s)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1