Matriz em um AWT Canvas

时间:2018-05-31 02:14:41

标签: 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;

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("");
        }
    }
}

0 个答案:

没有答案