如何绘制多个矩形 [英] how to draw multiple rectangles

查看:95
本文介绍了如何绘制多个矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图制作一个简单的程序,你点击它的屏幕,它会创建一个块,它会下降并与下面的一个更大的块碰撞并坚持下去。有点像一个简单的碰撞程序。问题是当我创建一个块时,它先前删除了块。我做了一个数组,但它仍然这样做。你们有谁知道我做错了什么吗?我确定这是一个简单的修复。

so I am trying to make a simple program where you click on the screen and it creates a block that falls and collides with a larger block beneath and sticks to it. Kind of like a simple collision program. The problem is when I create one block it deletes the block previously. I made an array, but it still does this. Do any of you know what Im doing wrong? Im sure its a simple fix.

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


推荐答案

绘画是一种破坏性的过程。也就是说,当新的绘制周期运行时,应该清除 Graphics 上下文的先前内容......

Painting is a destructive process. That is, when a new paint cycle runs, the previous contents of the Graphics context should be cleared...

所以,在你的 paintComponent 方法中,你只绘制最后一个块......

So, in you paintComponent method you are only painting the last block...

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
}

不要调用任何可能导致<的方法要调用code>重绘。这将使你陷入一个潜在的死亡循环,消耗你的CPU。

DO NOT call any method that might cause repaint to be called. This will put you in a potential cycle of death that will consume your CPU.

相反,你应该遍历数组并绘制每一个...

Instead, you should loop through the blocks array and paint each one...

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

在你的 mouseReleased 方法,你应该添加新的矩形......

And in you mouseReleased method, you should be adding the new rectangles...

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

我建议你看一下自定义绘画在AWT和Swing中绘画 Swing中的并发以获取更多详细信息

I'd suggest you take a look at Custom Painting, Painting in AWT and Swing and Concurrency in Swing for more details

这篇关于如何绘制多个矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆