これは非常に単純だと思います。見方を変えるだけです。通常の戦艦レイアウトのjpgがあります。また、ゲームをよりファンシーにするために、Jpanelで作成した実際のグリッドもあります。jpanelが必要です。グリッドがjpegに重なって、よりリアルに見えます。jpegをインポートすると、各グリッド内に画像が配置されました。これは理解できることですが、グリッドクラスの新しいインスタンスを作成します。6 x 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
{
}
}
これは私のグリッドクラスです。この場所では機能しないため、画像をインポートするコードはコメント化されています
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();
}
}
なぜそれが機能しないのかはわかりますが、グリッドと一致するようにどのように機能させるのか理解できないようです。