Java 如何制作可以通过单击按钮绘制的形状

Java how to make a shape(s) that can be draw by clicking button

我正在创建一个 2d 世界,其中有一个矩形的玩家,我试图做的是当玩家按下 space 按钮时,它会根据他的方向绘制一个方块,问题只是设置一个块,加上它在修复后将其删除,现在我知道我的错误在于绘画方法。我想知道如何让它在我每次按下 space 时绘制块,而无需在重绘后删除并且可以设置多次我知道我应该使用循环但我不知道我应该写它

private static final long serialVersionUID = -1401667790158007207L;
int playerx = 450;
int playery = 450;
int playerDirection = 0;      //north = 0, west = 1, south = 2, east = 3
boolean setBlock;

public platform(){
    super("2D game");

    JPanel panel = new JPanel();
    panel.setBackground(Color.white);

    addKeyListener(this);
    this.setContentPane(panel);
}

public void paint(Graphics g){
    super.paint(g);
    Graphics2D g2 = (Graphics2D)g;

    Rectangle player = new Rectangle(playerx, playery, 50, 50);
    g2.fill(player);

    if(setBlock){

        if(playerDirection == 0){

            g2.fillRect(playerx, playery - 50, 50, 50);
        }else if(playerDirection == 1){

            g2.fillRect(playerx + 50, playery, 50, 50);
        }else if(playerDirection == 2){

            g2.fillRect(playerx, playery + 50, 50, 50);
        }else if(playerDirection == 3){

            g2.fillRect(playerx - 50, playery, 50, 50);
        }

        setBlock = false;
    }
}

public void keyPressed(KeyEvent e) {

    if(e.getKeyCode() == KeyEvent.VK_UP){

        playerDirection = 0;
        playery -=50;
        repaint();
    }
    if(e.getKeyCode() == KeyEvent.VK_DOWN){

        playerDirection = 2;
        playery +=50;
        repaint();
    }
    if(e.getKeyCode() == KeyEvent.VK_RIGHT){

        playerDirection = 1;
        playerx += 50;
        repaint();
    }
    if(e.getKeyCode() == KeyEvent.VK_LEFT){

        playerDirection = 3;
        playerx -=50;
        repaint();
    }
    if(e.getKeyCode() == KeyEvent.VK_SPACE){

        setBlock = true;
    }
}

public void keyReleased(KeyEvent e) {


}

public void keyTyped(KeyEvent e) {


}

}

您需要将块存储在列表中。并在按下 Space 键时添加一个新的。然后你可以轻松地在 for 循环中绘制所有这些。同时删除 setBlock 布尔值,因为它没有意义。

...
// introduce this class to hold a block
class Block{
    public int x;
    public int y;
    public Block(int _x, int _y){
        x = _x;
        y = _y;
    }
}
...
// boolean setBlock;  // remove this boolean
ArrayList<Block> blocks = new ArrayList<Block>();  // list of all blocks

public platform(){...}

public void paint(Graphics g){
    ...    
    // if(setBlock){  // remove this boolean

        if(playerDirection == 0){
            g2.fillRect(playerx, playery, 50, 50);
        }else if(playerDirection == 1){
            g2.fillRect(playerx, playery, 50, 50);
        }else if(playerDirection == 2){
            g2.fillRect(playerx, playery, 50, 50);
        }else if(playerDirection == 3){
            g2.fillRect(playerx, playery, 50, 50);
        }
        // draw the blocks
        for(Block b : blocks)
            g2.fillRect(b.x, b.y, 50, 50);

        // setBlock = false; // remove this boolean
    //}  // remove this boolean
}

public void keyPressed(KeyEvent e) {
    ...
    if(e.getKeyCode() == KeyEvent.VK_SPACE){
        // setBlock = true;  // remove this boolean
        // add a new block
        blocks.add(new Block(playerx, playery));
        repaint();
    }
}
...