簡體   English   中英

PaintComponent不會繪制圖塊或繪制字符串

[英]PaintComponent doesn't paint the tiles or draws the strings

我為我的學校項目寫了一些代碼,以使用2d數組和函數paintcomponent創建10 x 10的網格。

我的問題是,它不顯示網格或顯示字符串。 (編譯器未顯示任何錯誤。)

這是我的代碼:

Board.java

public class Board extends JComponent implements KeyListener{

    public Board() {

    }

    public static String[] gameElements = new String[100];


    private String[][] map = new String[10][10];
    private String currentLevel = "1";
    private boolean paintComponentExecuted = false;
    Player hero;


    @Override
    public void paintComponent(Graphics g) {
        if(paintComponentExecuted == false) {
            loadLevel();
            int i = 0;
            int positionX = 50;
            int positionY = 50;
            for (int y = 0; y < map.length; y++) {
                for (int x = 0; x < map.length; x++) {
                    new Tile(x, y).paintComponent(g);
                    g.drawString(gameElements[i], positionY, positionX);
                    map[y][x] = gameElements[i];
                    positionY += 50;
                    System.out.print("[" + map[y][x] + "]");
                    i++;
                }
                positionY = 50;
                positionX += 50;
                System.out.println();
                paintComponentExecuted = true;

            }
        }
    }

    public void readTextFile(String fileName) {
        try {
            FileReader fileReader = new FileReader(fileName + ".txt");
            BufferedReader buffer = new BufferedReader(fileReader);
            String splitBy = ",";
            String line = buffer.readLine();

            for (int i = 0; i < gameElements.length; i++) {
                gameElements = line.split(splitBy);
            }

        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void loadLevel() {
        readTextFile(currentLevel);
    }

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setSize(600, 600);
        frame.setTitle("SleutelBarricade");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JComponent chart = new Board();
        frame.add(chart);

        frame.setVisible(true);


    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}

Tile.java

public class Tile extends Board {

    public Tile() {

    }

    final private static int CELL_WIDTH = 50;
    final private static int CELL_HEIGHT = 50;

    final private static int BOARD_X_OFFSET = 25;
    final private static int BOARD_Y_OFFSET = 25;

    private int x;
    private int y;

    private int getScreenX(int x, int y) {
        return BOARD_X_OFFSET + x * CELL_WIDTH;
    }

    private int getScreenY(int x, int y) {
        return BOARD_Y_OFFSET + y * CELL_HEIGHT;
    }

    public Tile(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public void paintComponent(Graphics g) {
        g.setColor(Color.BLACK);
        g.drawRect(
                getScreenX(x, y),
                getScreenY(x, y),
                CELL_WIDTH,
                CELL_HEIGHT);

    }

}

提前致謝!

您必須刪除此條件:

if(paintComponentExecuted == false) {


  paintComponentExecuted = true;
}

保留該塊的其余內容。 不要阻塞油漆以重新油漆; 否則,您將一次又一次看到它。

您需要在構造函數中創建圖塊:

假設您有這個全局變量

Tile tiles=new Tile[10][10];

然后在構造函數中

   for (int y = 0; y < tiles[0].length; y++)
      for (int x = 0; x < tiles.length; x++)
        tiles[x][y]=new Tile(x, y);

然后在paintComponent中調用

tiles[x][y].paintComponent(g);

另外,由於您在板上繪圖,因此Tile最好不要僅擴展本身。 也許其中一些會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM