2013-05-29 156 views
0

所以我試圖做一個簡單的程序,您在屏幕上單擊它,它會創建一個塊落下並與下面的較大塊發生碰撞並堅持下去。有點像一個簡單的碰撞程序。問題是當我創建一個塊時,它先前刪除了該塊。我做了一個數組,但它仍然這樣做。你們有誰知道我做錯了什麼?我確定它是一個簡單的修復。如何繪製多個矩形

public class Screen extends JPanel implements Runnable { 

public static JLabel statusbar; //displays a status bar showing what mouse movements are taking place 
private Image cat; //image of the cat 
public int xCoord ; //get the coordinates of the mouse pressed 
public int yCoord ; 
public int xCreate; 
public int yCreate; 
public Rectangle Ground; 
public Rectangle Block; 
public boolean isClicked = false; 
public int clickCount = 0; 
Rectangle blocks[] = new Rectangle[10]; 

int blocknum = 0; 

    public Screen(Frame frame) { 
      loadPic(); //calls the loadPic method above 
       Handlerclass handler = new Handlerclass(); //creates a new class to use the mouse motion listener 
        System.out.println("mouse works!"); 
     addMouseListener(handler); 
     addMouseMotionListener(handler); 
     statusbar = new JLabel("default"); 
      add(statusbar); 
    } 
    public void run(){ //this is the game run loop 
     System.out.println("this is running"); 
     try{ 
     } catch(Exception e) {} //exception handling 

    } 
    public void loadPic(){ //loads the picture from the other project but its the same pic 
     cat = new ImageIcon("C:\\Users\\Camtronius\\Documents\\NetBeansProjects\\Moving Block Proj\\src\\MovingBlock\\catIcon1.png").getImage(); //gets the image 
     System.out.println("Image Loaded!"); 
    } 

    @Override public void paintComponent(Graphics g){ 
     super.paintComponent(g); //paints the component, the picture, on top 
      Graphics2D g2d = (Graphics2D) g.create(); 

      g2d.drawImage(cat, xCoord, yCoord, null);  

      g2d.setColor(Color.BLUE); 
      Ground = new Rectangle(0,450,550,50); 
      g2d.fillRect(0,450, 550, 50); 

       for(Rectangle blocknum : blocks){ 
        if (blocks != null) {  
         g2d.setColor(Color.RED); 
         g2d.fillRect(xCreate,yCreate,50,50); 
         System.out.println(blocknum); 
        } 

       } 
       //move();   
    } 

    public void move(){ 
    if(yCreate<400){ 
     yCreate+=1; 
    }else{ 
     } 
    if(Ground.intersects(blocks[blocknum])){ 
      yCreate=400; 
      System.out.println("contains!"); 
     } 
    }  
    private class Handlerclass implements MouseListener, MouseMotionListener{ 
     public void mouseClicked(MouseEvent event){ 

     } 
     public void mousePressed(MouseEvent event){ 

     } 
     public void mouseReleased(MouseEvent event){ 

     if(blocknum<blocks.length){ 
      xCreate=event.getX(); 
      yCreate=event.getY(); 
      blocks[blocknum] = new Rectangle(50,50, xCreate, yCreate); 
      repaint(); 
     } 
     blocknum=blocknum+1; 
     } 
     public void mouseEntered(MouseEvent event){ 

     } 
     public void mouseExited(MouseEvent event){ 

     } 
     public void mouseDragged(MouseEvent event){ 

     } 
     public void mouseMoved(MouseEvent event){ 
      statusbar.setText(String.format("Coordinates are: %d, %d", event.getX(),event.getY())); 
      xCoord=event.getX(); 
      yCoord=event.getY(); 
     } 
    } 
} 

回答

2

繪畫是一個破壞性的過程。也就是說,當一個新的油漆週期運行時,Graphics背景下以前的內容應該被清除......

所以,在你paintComponent方法,你只畫上一個塊...

if(isClicked = true){ 
    blocks[blocknum] = new Rectangle(50,50, xCreate, yCreate); 
    g2d.setColor(Color.RED); 
    g2d.fillRect(xCreate,yCreate,50,50); 
    System.out.println(blocknum); 
    repaint(); // THIS IS A BAD IDEA 
} 

不要調用任何可能導致repaint被調用的方法。這會讓你陷入潛在的死亡週期,這會消耗你的CPU。

相反,你應該遍歷blocks陣列和油漆每一個...

for (Rectangle block : blocks) { 
    if (block != null) { 
     g2d.setColor(Color.RED); 
     g2d.fill(block); 
    } 
} 

而且在你mouseReleased方法,你應該添加新的矩形...

public void mouseReleased(MouseEvent event){ 
    blocknum=blocknum+1; 
    if (blocknum < blocks.length) { 
     xCreate=event.getX(); 
     yCreate=event.getY(); 
     blocks[blocknum] = new Rectangle(xCreate, yCreate, 50, 50); 
    } 
} 

我建議你看看Custom PaintingPainting in AWT and SwingConcurrency in Swing瞭解更多詳情

+0

我添加了你建議的代碼和在上面編輯它。儘管如此,它仍然只能吸引一個街區。我可能做錯了什麼? –

+0

Opps,我在'mouseReleased'代碼中有個錯誤,並且更新了'paintComponent'循環 – MadProgrammer

+0

我明白了!感謝一羣人! –