2013-05-29 2 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 Painting, Painting in AWT and SwingConcurrency in Swing을 참조하시기 바랍니다.

+0

제안한 코드를 추가했으며 위의 편집. 그것은 여전히 ​​한 번에 한 블록 씩 그립니다. 나는 무엇을 잘못 할 수 있 었는가? –

+0

Opps, 나는'mouseReleased' 코드에서 실수를했고'paintComponent' 루프를 업데이트했습니다. – MadProgrammer

+0

알았어요! 고마워요! –

관련 문제