Java Matriz em um AWT帆布
我正在用SWING做一个战舰游戏。程序读取包含以下数据的文件:高度、长度、由船只数量填充的位置矩阵。问题是鼠标捕捉到的矩阵与文件中的矩阵相反,我不知道该怎么办。我将感谢任何帮助 代码如下: 框架:Java Matriz em um AWT帆布,java,swing,awt,Java,Swing,Awt,我正在用SWING做一个战舰游戏。程序读取包含以下数据的文件:高度、长度、由船只数量填充的位置矩阵。问题是鼠标捕捉到的矩阵与文件中的矩阵相反,我不知道该怎么办。我将感谢任何帮助 代码如下: 框架: import Model.ArcMap; import java.awt.BorderLayout; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JFrame; p
import Model.ArcMap;
import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
public class GameFrame extends JFrame {
private GameCanvas canvas;
// CanvasThread updateScreenThread = new CanvasThread(canvas);
private ArcMap archive;
private int width;
private int hight;
public static final int AREA = 60;
public GameFrame(ArcMap archve) {
this.archive = archve;
this.width = archve.getArcWidth();
this.hight = archive.getArcHeight();
canvas = new GameCanvas(archive);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setTitle("Stellar Battle");
add(BorderLayout.CENTER, canvas);
setResizable(false);
// Define largura e altura da janela principal
setSize(AREA * width, canvas.AREA * hight);
setLocationRelativeTo(null);
// setVisible(true);
// Inicia Thread com timer para redesenhar a tela.
// updateScreenThread.start();
canvas.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
int x = e.getX();
int y = e.getY();
int x_pos = x / canvas.AREA;
int y_pos = y / canvas.AREA;
System.out.println(canvas.getShot(x_pos, y_pos);
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
}
}
画布:
import Model.ArcMap;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.Buffer;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class GameCanvas extends Canvas {
public static final int AREA = 40;
private int margin = 0;
private int rows;
private int cols;
private ArcMap achive;
private int[][] explosionMatrix = new int[rows][cols];
public GameCanvas(ArcMap archive) {
this.achive = archive;
this.rows = archive.getArcHeight();
this.cols = archive.getArcWidth();
explosionMatrix = archive.getArcMatrix();
setSize(AREA * rows, AREA * cols);
}
//@Override
public void paint(Graphics g) {
int lenthI = rows;
int lenthJ = cols;
g.setColor(new Color(131, 209, 232));
g.fillRect(0, 0, cols * AREA, rows * AREA);
g.setColor(Color.white);
for (int i = 0; i < cols ; i++) {
g.drawLine(i * AREA, 0, i * AREA, AREA * rows);
for (int j = 0; j < rows; j++) {
g.drawLine(0, j * AREA, AREA * cols, j * AREA);
}
}
this.oque();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(explosionMatrix[i][j]);
}
System.out.println("");
}
// Prepare an ImageIcon
ImageIcon icon = new ImageIcon("images/ondas_1.jpg");
ImageIcon iconShot = new ImageIcon("images/explosion.png");
// Prepare an Image object to be used by drawImage()
final Image img = icon.getImage();
final Image imgShot = iconShot.getImage();
this.oque();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
g.drawImage(img, i * AREA, j * AREA, AREA, AREA, null);
if (explosionMatrix[i][j] == 1) {
g.drawImage(imgShot, i * AREA, j * AREA, AREA, AREA, null);
}
}
}
this.oque();
}
public void setShot(int x, int y) {
explosionMatrix[x][y] = 1;
}
public int getShot(int x, int y) {
return explosionMatrix[x][y];
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
public int getCols() {
return cols;
}
public void setCols(int cols) {
this.cols = cols;
}
public int[][] getExplosionMatrix() {
return explosionMatrix;
}
public void setExplosionMatrix(int[][] explosionMatrix) {
this.explosionMatrix = explosionMatrix;
}
public void oque() {
System.out.println("");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(explosionMatrix[i][j]);
}
System.out.println("");
}
}
}
import Model.ArcMap;
导入java.awt.Canvas;
导入java.awt.Color;
导入java.awt.Graphics;
导入java.awt.Image;
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.nio.Buffer;
导入javax.imageio.imageio;
导入javax.swing.ImageIcon;
公共类GameCanvas扩展画布{
公共静态最终内部面积=40;
私人内部利润率=0;
私有int行;
私人公司;
私有ArcMap achive;
私有int[][]分解矩阵=新int[行][cols];
公共游戏画布(ArcMap存档){
this.achive=存档;
this.rows=archive.getArcHeight();
this.cols=archive.getArcWidth();
explosionMatrix=archive.getArcMatrix();
设置大小(面积*行,面积*列);
}
//@凌驾
公共空间涂料(图g){
int lenthI=行;
int lenthJ=cols;
g、 setColor(新颜色(131209232));
g、 fillRect(0,0,列*面积,行*面积);
g、 setColor(Color.white);
for(int i=0;i
English请。不要覆盖绘制,覆盖绘制组件并确保绘制链保持接触,不要在绘制中加载图像什么是ArcMap
?你有样本数据吗?你的问题是什么?