代码之家  ›  专栏  ›  技术社区  ›  jerome utti

我的JFrame项目不会绘制一些PNG,而是会绘制其他PNG,即使所有内容都在同一目录中

  •  0
  • jerome utti  · 技术社区  · 3 年前

    我有一个扫雷舰项目,它使用pngs为我的细胞绘制图标。出于某种原因,我的绘制/绘制方法将填充除我之外的所有单元格。所有内容都在同一个目录中。在我的代码中,我覆盖java.awt.Graphics中的paintComponent(Graphics g)。然后,我根据雷区的单元格状态“打开”/“标记”/“覆盖”等来绘制它们。 enter image description here enter image description here

    以下是我绘画过程中涉及的方法途径:

    更新:这是一个最小的可复制示例

    1. 播放.java
    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class Play
    {
        public static void main(String[] args)
        {
            EventQueue.invokeLater(new Runnable()
                {
                    public void run()
                    {
                        // game configs rows,cols,cellSize,#ofmines
                        int[] default_configs = {16,16,15,10};
                        
                        Configuration.loadParameters(default_configs);
                        JFrame frame = new JFrame();
                        JLabel statusbar = new JLabel("select a cell");
                        frame.add(statusbar, BorderLayout.SOUTH);
                        frame.add(new Board(Configuration.ROWS, Configuration.COLS, Configuration.MINES, statusbar));
                        frame.setResizable(false);
                        frame.pack();
                        frame.setTitle("Ekrem's Minefield");
                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        frame.setVisible(true);
        
                    }
                });
        }
    }
    
    

    Board.java

    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import java.awt.Graphics;
    import java.awt.Dimension;
    
    
    public class Board extends JPanel
    {
    
        private Minefield minefield;
        private JLabel status;
        private int height;
        private int width;
        private int mines;
    /**
     *  Constructor that creates a minefield, initializes values, activates mouse, and sets up size of the board
     */
        public Board(int height, int width, int mines, JLabel statusbar)
        {
            this.height = height;
            this.width = width;
            this.mines = mines;
            this.minefield = new Minefield(height, width, mines);
            status = statusbar;
            setPreferredSize(new Dimension(Configuration.BOARD_WIDTH, Configuration.BOARD_HEIGHT));
            addMouseListener(new MouseReader(this));
        }
    /**
     * @param Graphics object
     */
        @Override
        public void paintComponent(Graphics g)
        {
            minefield.draw(g);
        }
        
        public Minefield getMinefield() {
            return this.minefield;
        }
        
    /**
     * this method adjusts the statuses of cells based on user interaction through a mouse
     */
        public void mouseClickOnLocation(int x, int y, String button)
        {
            Object clickedCell = minefield.getCellByScreenCoordinates(x,y);
            if (clickedCell instanceof InfoCell) {
                if (button == "right") {
                    if (((InfoCell)(clickedCell)).getStatus().equals("opnd")) {
                        return;
                    }
                    else if (((InfoCell)(clickedCell)).getStatus().equals("mrkd")) {
                        ((InfoCell)(clickedCell)).setStatus("cvrd");
                    }
                    else {
                        ((InfoCell)(clickedCell)).setStatus("mrkd");
            
                    }
                    
                }
                else if (button == "left") {
                    //minefield.openCells((InfoCell)(clickedCell));
                }
            }
            if (clickedCell instanceof MineCell) {
                if (button == "right") {
                    if(((MineCell)(clickedCell)).getStatus().equals("cvrd")) {
                        ((MineCell)(clickedCell)).setStatus("mrkd");
                        
                    }
                    else{
                        ((MineCell)(clickedCell)).setStatus("cvrd");
            
                    }
    
                }
                else if (button == "left") {
                    ((MineCell)(clickedCell)).setStatus("opnd");
                }
            }
    
            
        }
    
    }
    
    
    
    

    配置.java

    public class Configuration {
        public static int ROWS;
        public static int COLS;
        public static int CELL_SIZE;
        public static int MINES;
        public static int BOARD_WIDTH;
        public static int BOARD_HEIGHT;
        public static int UNOPENED_INFOCELL_COUNT;
        
        public static void loadParameters(int[] parameters){
            ROWS = parameters[0];
            COLS = parameters[1];
            CELL_SIZE = parameters[2];
            MINES = parameters[3];
            UNOPENED_INFOCELL_COUNT = ROWS * COLS - MINES;
            BOARD_WIDTH = COLS * CELL_SIZE + 1;
            BOARD_HEIGHT = ROWS * CELL_SIZE + 1;
        }
    
    }
    
    
    

    InfoCell.java

    import javax.swing.ImageIcon;
    
    import java.awt.Graphics;
    import java.awt.Image;
    
    public class InfoCell {
        private int row;
        private int column;
        private int numOfAdjacentMines = 0;
        private String status = "cvrd";
        //covered, uncovered, marked, wrongly_marked
    
        public InfoCell(int row, int column, int numOfAdjacentMines) {
            this.row = row;
            this.column = column;
            this.numOfAdjacentMines = numOfAdjacentMines;
        }
        public int getRow() {
            return this.row;
        }
        public int getCol() {
            return this.column;
        }
        public void setMines(int mineCount) {
            this.numOfAdjacentMines = mineCount;
        }
        public int getNumOfAdjacentMines() {
            return this.numOfAdjacentMines;
        }
    
        public void draw(Graphics g) {
            g.drawImage(this.getImage(), this.getHorizontalPosition(), this.getVerticalPosition(), null);
        }
        
        public Image getImage() {
            Image image;
            if (status.equals("cvrd")) {
                ImageIcon a = new ImageIcon("img/covered_cell.png");
                image = a.getImage();
            }
            else if(status.equals("mrkd")) {
                ImageIcon a = new ImageIcon("img/marked_cell.png");
                image = a.getImage();
            }
            else if(status.equals("wmrkd")){
                ImageIcon a = new ImageIcon("img/wrong_mark.png");
                image = a.getImage();
            }
            else /*(status.equals(Configuration.STATUS_OPENED))*/ {
                ImageIcon a = new ImageIcon("img/info_" + this.numOfAdjacentMines + ".png");
                image = a.getImage();
            }
            return image; 
                    
        }
        
        public int getHorizontalPosition() {
            return (column) * Configuration.CELL_SIZE;
            //Calculates and returns the pixel-level horizontal position of the top-left corner of the cell
        }
        
        public int getVerticalPosition() {
            return (row) * Configuration.CELL_SIZE;
            //Calculates and returns the pixel-level vertical position of the top-left corner of the cell
        }
        public String getStatus() {
            return this.status;
        }
        public void setStatus(String status) {
            //Setter method for the current status of the cell. Do not hard-code the value;
            //use a Configuration parameter instead.
            this.status = status;
        }
    
    }
    
    

    MineCell.java

    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.awt.Graphics;
    
    /**
     * 
     * @author Ekrem Kaya
     * @version 9:44 03/11
     */
    public class MineCell {
    /**
     * row, columnm, and status of each cell
     */
        private int row;
        private int column;
        private String status = "cvrd";
        //covered - cvrd, uncovered - opnd, marked - mrkd
    /**
     * 
     * @param row vertical location of the Cell object in Minefield
     * @param column horizantal poisition of the Cell object in Minefield
     */
        public MineCell(int row, int column) {
            this.row = row;
            this.column = row;
        }
    /**
     * 
     * @param g the Graphics object passed to the cell by the Minefield class
     */
        public void draw(Graphics g) {
            g.drawImage(this.getImage(), this.getHorizontalPosition(), this.getVerticalPosition(), null);
        }
    /**
     * 
     * @return this method returns an in representing the horizantal pixel location
     */
        public int getHorizontalPosition() {
            return (column) * Configuration.CELL_SIZE;
            //Calculates and returns the pixel-level horizontal position of the top-left corner of the cell
        }
        /**
         * 
         * @return this method returns an in representing the vertical pixel location
         */
        public int getVerticalPosition() {
            return (row) * Configuration.CELL_SIZE;
            //Calculates and returns the pixel-level vertical position of the top-left corner of the cell
        }
    /**
     * 
     * @return this method returns the status attribute of the Cell which can be covered, open, wrongly marked, or marked
     */
        public String getStatus() {
            return this.status;
        }
    /**
     * 
     * @param status the string that represents the status of the cell
     */
        public void setStatus(String status) {
            //Setter method for the current status of the cell. Do not hard-code the value;
            //use a Configuration parameter instead.
            this.status = status;
        }
    /**
     * 
     * @return this returns the image from the folder that will represent the status of the cell
     */
        public Image getImage() {
            
            Image image;
            
            if(this.status.equals("mrkd")) {
                ImageIcon a = new ImageIcon("img/marked_cell.png");
                image = a.getImage();
            }
            else if(this.status.equals("opnd")) {
                ImageIcon a = new ImageIcon("img/mine_cell.png");
                image = a.getImage();
            }
            else /*(status.equals(Configuration.STATUS_COVERED))*/ {
                ImageIcon a = new ImageIcon("img/covered_cell.png");
                image = a.getImage();
            }
            return image;
                    
        }
    }
    
    

    鼠标阅读器.java

    /**
     * DO NOT EDIT ANYTHING IN THIS FILE
    */
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    public class MouseReader extends MouseAdapter
    {
        private Board board;
    
        public MouseReader(Board board)
        {
            this.board = board;
        }
    
        @Override
        public void mouseClicked(MouseEvent e)
        {
            String button = "";
            if (e.getButton() == MouseEvent.BUTTON1)
                button = "left";
            else if (e.getButton() == MouseEvent.BUTTON3)
                button = "right";
            board.mouseClickOnLocation(e.getX(), e.getY(), button);
        }
    }
    
    

    非水雷细胞和水雷细胞的方法几乎相同,代码最终会绘制除水雷细胞外的所有细胞。我检查了一下,发现上面的每个方法最终都会被调用,并且minecells的状态也被正确标记。

    0 回复  |  直到 3 年前