使用ArrayList的一个JPanel移除一个矩形 [英] removing a rectangle from a jpanel using arraylist

查看:182
本文介绍了使用ArrayList的一个JPanel移除一个矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

'package javaapplication12;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.*;

/** Bouncing Ball (Animation) via custom thread */
public class JavaApplication12 extends JFrame implements KeyListener{
// Define named-constants
private static final int CANVAS_WIDTH = 640;
private static final int CANVAS_HEIGHT = 480;
private static final int UPDATE_INTERVAL = 30; // milliseconds
int xx = 200;
int yy = 400;
Shape a1;
Shape a2;
Rectangle2D a3;

Rectangle2D a12;
Rectangle2D a4;
Rectangle2D a5;
Rectangle2D a6;
Rectangle2D a7;
Rectangle2D a8;
Rectangle2D a9;
Rectangle2D a10;
Rectangle2D a11;
Rectangle2D a13;
Rectangle2D a14;
Rectangle2D a15;
Rectangle2D a16;
Rectangle2D a17;
Rectangle2D a18;
Rectangle2D a19;
Rectangle2D a20;
Rectangle2D a21;
Rectangle2D a22;
Rectangle2D a23;
Rectangle2D a25;
ArrayList<Rectangle2D> s = new ArrayList<Rectangle2D>();

Color c2 = Color.RED;

private DrawCanvas canvas; // the drawing canvas (extends JPanel)

// Attributes of moving object
private int x = 375;     // top-left (x, y)
private int y = 355;
private int size = 20;  // width and height
private int xSpeed = 3;  // moving speed in x and y directions
private int ySpeed = 5;  // displacement per step in x and y


 /** Constructor to setup the GUI components */
public JavaApplication12() {
  canvas = new DrawCanvas();
  canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
  this.setContentPane(canvas);
  this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  this.pack();
  this.setTitle("Bouncing Ball");
  this.setVisible(true);
  addKeyListener(this);
  fun();

 // Create a new thread to run update at regular interval
  Thread updateThread = new Thread() {
     @Override
     public void run() {
        while (true) {
           update();   // update the (x, y) position

           repaint();  // Refresh the JFrame. Called back paintComponent()

           try {
              // Delay and give other thread a chance to run
              Thread.sleep(UPDATE_INTERVAL);  // milliseconds
           } catch (InterruptedException ignore) {}
        }
     }
  };
  updateThread.start(); // called back run()
}

/** Update the (x, y) position of the moving object */
public void update() {
  x += xSpeed;
  y += ySpeed;
  if (x > CANVAS_WIDTH - size || x < 0) {
     xSpeed = -xSpeed;
  }
  if (y > CANVAS_HEIGHT - size || y < 0) {
     ySpeed = -ySpeed;
  }
}

@Override
public void keyTyped(KeyEvent ke) {
}

@Override
public void keyPressed(KeyEvent ke) {
    int keycode = ke.getKeyCode();
       if(keycode == KeyEvent.VK_LEFT)
       {
          xx = xx-15;
       }

       if(keycode == KeyEvent.VK_RIGHT)
       {
         xx = xx+15;
       }

}

@Override
public void keyReleased(KeyEvent ke) {
   // throw new UnsupportedOperationException("Not supported yet.");
}

/** DrawCanvas (inner class) is a JPanel used for custom drawing */
class DrawCanvas extends JPanel {
    private static final long serialVersionUID = 1L;
    @Override

  public void paintComponent(Graphics g) {
     super.paintComponent(g);  // paint parent's background
  Graphics2D d = (Graphics2D)g; 


  for(Rectangle2D r : s){
            d.fill(r);
        }
     setBackground(Color.BLACK);

     g.setColor(Color.BLUE);
     a1 = new Ellipse2D.Double(x,y,size,size);
     a2 = new RoundRectangle2D.Double(xx,yy,120,10,20,20);

     a25 = new Rectangle2D.Double(530,120,100,20);



     d.fill(a1);
     d.setPaint(c2);
      d.fill(a2);

     if(a1.intersects(a3.getBounds()))
     {


         s.remove(a3);ySpeed = -ySpeed;

     }
     if(a1.intersects(a4.getBounds()))
     {
         s.remove(a4);ySpeed = -ySpeed;
     }
     if(a1.intersects(a5.getBounds()))
     {
         s.remove(a5);ySpeed = -ySpeed;
     }
     if(a1.intersects(a6.getBounds()))
     {
         s.remove(a6);ySpeed = -ySpeed;
     }
     if(a1.intersects(a7.getBounds()))
     {
         s.remove(a7);ySpeed = -ySpeed;
     }
     if(a1.intersects(a8.getBounds()))
     {
         s.remove(a8);ySpeed = -ySpeed;

     }
     if(a1.intersects(a9.getBounds()))
     {
         s.remove(a9);ySpeed = -ySpeed;
     }
     if(a1.intersects(a10.getBounds()))
     {
         s.remove(a10);ySpeed = -ySpeed;
     }
     if(a1.intersects(a11.getBounds()))
     {
         s.remove(a11);ySpeed = -ySpeed;
     }
     if(a1.intersects(a12.getBounds()))
     {
         s.remove(a12);ySpeed = -ySpeed;
     }
     if(a1.intersects(a13.getBounds()))
     {
         s.remove(a13);ySpeed = -ySpeed;
     }
     if(a1.intersects(a14.getBounds()))
     {
         s.remove(a14);ySpeed = -ySpeed;
     }
     if(a1.intersects(a15.getBounds()))
     {
         s.remove(a15);ySpeed = -ySpeed;
     }

     if(a1.intersects(a16.getBounds()))
     {
         s.remove(a16);ySpeed = -ySpeed;
     }

     if(a1.intersects(a17.getBounds()))
     {
         s.remove(a17);ySpeed = -ySpeed;
     }

     if(a1.intersects(a18.getBounds()))
     {
         s.remove(a18);ySpeed = -ySpeed;
     }

     if(a1.intersects(a19.getBounds()))
     {
         s.remove(a19);ySpeed = -ySpeed;
     }

     if(a1.intersects(a20.getBounds()))
     {
         s.remove(a20);ySpeed = -ySpeed;
     }

     if(a1.intersects(a21))
     {
         s.remove(a21);ySpeed = -ySpeed;
     }

     if(a1.intersects(a22))
     {

       s.remove(a21);ySpeed = -ySpeed;
      ySpeed = -ySpeed;
     }
 collide();
}
}
public final void fun()
{
    a3 = new Rectangle2D.Double(20,15,100,20);
      a4 = new Rectangle2D.Double(150,15,100,20);
       a5 = new Rectangle2D.Double(280,15,100,20);



       a6 = new Rectangle2D.Double(400,15,100,20);
         a7 = new Rectangle2D.Double(530,15,100,20);

         a8 = new Rectangle2D.Double(20,50,100,20);
           a9 = new Rectangle2D.Double(150,50,100,20);
            a10 = new Rectangle2D.Double(280,50,100,20);
             a12 = new Rectangle2D.Double(400,50,100,20);
              a11 = new Rectangle2D.Double(530,50,100,20);

          a13 = new Rectangle2D.Double(20,85,100,20);
           a14 = new Rectangle2D.Double(150,85,100,20);
            a15 = new Rectangle2D.Double(280,85,100,20);
             a16 = new Rectangle2D.Double(400,85,100,20);
              a17 = new Rectangle2D.Double(530,85,100,20);

          a18 = new Rectangle2D.Double(20,120,100,20);
           a19 = new Rectangle2D.Double(150,120,100,20);
            a20 = new Rectangle2D.Double(280,120,100,20);
             a21 = new Rectangle2D.Double(400,120,100,20);
               a22 = new Rectangle2D.Double(530,120,100,20);
        s.add(a3);
        s.add(a4);
        s.add(a5);
        s.add(a6);
        s.add(a7);
        s.add(a8);
        s.add(a9);
        s.add(a10);
        s.add(a11);
        s.add(a12);
        s.add(a13);
        s.add(a14);
        s.add(a15);
        s.add(a16);
        s.add(a17);
        s.add(a18);
        s.add(a19);
        s.add(a20);
        s.add(a21);
        s.add(a22);
}

  public void collide()
   {

   if(a2.intersects(a1.getBounds()))
     {
         xSpeed = +xSpeed;
         ySpeed = -ySpeed;

      }
    }
 public static void main(String[] args) {
  // Run GUI codes in Event-Dispatching thread for thread safety
  SwingUtilities.invokeLater(new Runnable() {
     @Override
     public void run() {
        new JavaApplication12(); // Let the constructor do the job

     }
  });

 }
} `

好,我是新来的Java游戏编程。我编程的游戏,就像是一砖断路器

Ok I'm new to Java game programming. I'm programming a game that is like a brick breaker

我有一个包含一些矩形组成的数组列表。我使用的是增强 for循环绘制。我的游戏有一个球,当球击中矩形必须消失AMD同时球必须takea反向运动。一切工作正常。但是,一旦矩形消失,如果球再一次去那个地方把球取即使矩形是不存在的反方向。

I have a array list which contains some rectangles. I have draw it using a enhanced for-loop. My game has a ball and when the ball hits the rectangle it must disappear amd at the same time the ball must takea reverse movement. Everything works fine. But once the rectangle disappears and if the ball goes again to that place the ball takes reverse direction even though the rectangle is not there.

科里森检查。

推荐答案

您code为画矩形使用ArrayList的'S',和你的逻辑正确地删除它们,所以它们消失。

Your code for painting the rectangles uses the arraylist 's', and your logic correctly removes them so they disappear.

不过,您的code扭转速度没有考虑到矩形是否仍处于S或没有。无论一个矩形已经从's被删除,逻辑:

However, your code for reversing the speed does not take into account whether that rectangle is still in 's' or not. No matter whether a rectangle has been removed from 's', the logic:

if(a1.intersects(a3.getBounds()))

如果在球传到矩形是其中的

将始终返回true。你需要做的是这样的:

will always return true if the ball gets to where the rectangle was. You need to do something like:

if(s.contains(a3) && a1.intersects(a3.getBounds()))

,使得矩形被移除之后,它将不匹配和反弹。

so that after a rectangle is removed, it will not match and bounce.

此外,你必须在'A22'一个错字:

Also, you have a typo in 'a22':

if(a1.intersects(a22))
 {

   s.remove(a21);ySpeed = -ySpeed;
  ySpeed = -ySpeed;
 }

应该是:

if(a1.intersects(a22))
 {
   s.remove(a22);ySpeed = -ySpeed;
 }

去想,另一件事情是,如果球击中右两个矩形之间,它会与他们两人相交,他们将分别扭转ySpeed​​(使球会继续直行)

Another thing to think about, is that if the ball hits right in between two rectangles, it will intersect with both of them, and they will each reverse the ySpeed (so the ball will carry on going straight)

希望帮助!

这篇关于使用ArrayList的一个JPanel移除一个矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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