如何绘制多个矩形

时间:2013-05-29 04:53:37

标签: java java-2d paintcomponent

因此,我试图制作一个简单的程序,您点击屏幕,它会创建一个块,它会下降并与下方的较大块碰撞并粘在它上面。有点像一个简单的碰撞程序。问题是当我创建一个块时,它先前删除了块。我做了一个数组,但它仍然这样做。你们中的任何人都知道我做错了什么吗?我确定它是一个简单的解决方案。

    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(); 
        }
    }
}

1 个答案:

答案 0 :(得分: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了解详情