Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9104023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:45:58+00:00 2026-06-17T01:45:58+00:00

This i believe is pretty simple maybe im just looking at it differently, i

  • 0

This i believe is pretty simple maybe im just looking at it differently, i have a jpg of a normal battleships layout, i also have a actual grid i done with a Jpanel, in order to make my game more fancy i wish to have the jpanel with the grid overlaying the jpeg making it seem more realistic. When i imported the jpeg it placed the image within each grid . which is understandable, i create a new instance of a grid class. 6 by 6

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class Grid extends JPanel {
private int row;
private int column;
BufferedImage img;

private BattleShipsClient parent;
public Grid(int row , int column, BattleShipsClient gui)
{
    this.row = row;
    this.column = column;
    this.parent = gui;

    setBorder(new LineBorder(Color.black,1));
    addMouseListener(new ClickListener());
    /*
     try {
           img = ImageIO.read(new File("Battleships.jpg"));
       } catch (IOException e) {
       }*/  
}
protected void paintComponent(Graphics g)
{
    super.paintComponent(g);
    //g.drawImage(img, 0, 0, null);
}
private class ClickListener extends MouseAdapter
{

}


}

This is my grid class, the code to import the image has been commented out as it doesnt work in this place

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.*;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.*;


public class BattleShipsClient implements Runnable, BattleShipConstants{
//6 rows and 6 collunms
private Grid [][] grid = new Grid[6][6];

private DataInputStream fromServer;
private DataOutputStream toServer;
BufferedImage img;
JFrame j = new JFrame();

public BattleShipsClient()
{
    JPanel p = 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));

    p.setBorder(new LineBorder(Color.black,1));

    j.add(p,BorderLayout.CENTER);
    j.setSize(320,320);
    j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    j.setVisible(true);



}
public void run(){
    Thread thread = new Thread(this);
    thread.start();
}
public static void main(String [] args){
    BattleShipsClient bs = new BattleShipsClient(); 
}


}

I realise why its not working but cant seem to figure out how i will get it to work so its inline with the grid.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T01:46:00+00:00Added an answer on June 17, 2026 at 1:46 am

    As you’re probably aware the reason its not working (image inlined) is that you’re adding 6 x 6 (36) Grid JPanels each of which is drawing a separate Image of the battleship. What you need is to paint a single image and to overlay the individual cells on top.

    One way to do this is to create another custom JPanel that paints just the image and then add your Grid panels to that instead:

    class ImagePanel extends JPanel {
       private final BufferedImage img;
    
       public ImagePanel() throws IOException {
         setLayout(new GridLayout(6, 6));
         img = ImageIO.read(new File("Battleships.jpg"));
       }
    
       @Override
       protected void paintComponent(Graphics g) {
         super.paintComponent(g);
         g.drawImage(img, 0, 0, getWidth(), getHeight(), null);
       }
    }
    

    One last important note: Don’t forget to call

    setOpaque(false);
    

    in each Grid panel to make each of the cells transparent. This will allow the image in the parent container to be displayed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is pretty much just a simple join statemnet I believe. I've not worked
I believe this is a pretty simple thing, and I swear I've done it
I believe my question is pretty simple so this should be a piece of
This is probably pretty simple for most... I have this line in Magento which
A long explanation but I believe this is a pretty simple question. I think
I believe this is a fairly simple question but it is something I am
I believe this question might have been previously attempted in 2006 on a different
--EDIT-- I believe this is a valid question that may have multiple answers (as
I'm looking for some backup solution. My request is pretty simple: Source - FTP
I'm pretty stumped here... I've got what I believe to be a really simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.