2015-12-08 15 views
1

即时创建一个2D球体,该球体具有矩形形状的球员,而我试图做的是在球员按空格按钮时绘制一个球阻止取决于他的方向问题是它只设置为一个块,并加上现在修复后删除它现在我知道我的错误是在涂料方法。我想知道如何使它吸引了块每次我按下空格不重新粉刷后去除,并且可以多次我知道我应该用循环设置,但我不知道到底是什么,我应该把它写Java如何制作一个可以通过点击按钮来绘制的形状

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) { 


} 

}

+2

首先,在paint方法中摆脱这个'setBlock = false;',因为程序状态永远不应该依赖于你无法控制的方法的绘画方法。这是一个Swing GUI,如果是这样的话,你应该首先避免重写paint并且赞成paintComponent。 –

+2

您必须将您的矩形块保存在列表中。因为每当你重画你基本上失去了没有保存在任何地方的一切。 –

回答

2

您需要将块存储在列表中。按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(); 
    } 
} 
... 
+0

感谢那为dummu问题感到抱歉,但即时新的@arcticlord – Heroxlegend

相关问题