Java 二维对象数组

Java 二维对象数组,java,arrays,2d-games,Java,Arrays,2d Games,我对Java真的很陌生。我只需要解释如何声明对象的2D数组,我有如下内容: package breakout; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.Color; import java.awt.F

我对Java真的很陌生。我只需要解释如何声明对象的2D数组,我有如下内容:

package breakout;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JOptionPane;

public class Breakout extends JPanel {

    public class Ball {
      private
        int x = 400;
        int y = 300;
        int speed = 2;
        int dirx = 1;
        int diry = -1;

      public
        void bounce(int px, int py, int lx, int ly) {
            if ((x + 10 >= 800 && dirx == 1) || (x <= 0 && dirx == -1))
                dirx *= -1;
            if (y <= 0 && diry == -1)
                diry *= -1;

            if (y + 10 >= py && y <= py + ly && diry == 1 && x + 10 >= px && x <= px + lx)
                diry *= -1;
        }

        int getX() {
            return x;
        }

        int getY() {
            return y;
        }

        void setDirx(){
            dirx *= -1;
        }

        void setDiry(){
            diry *= -1;
        }

        void move() {
            x += speed*dirx;
            y += speed*diry;
        }


        void paint(Graphics2D g) {
            g.fillOval(x,y,10,10);
        }
    }

    public class Paddle {
      private
        int x = 400;
        int y = 520;
        int width = 100;
        int height = 6;
        int speed = 6;
        int dirL = 0;
        int dirR = 0;

      public
        void move() {
            x -= speed*dirL;
            x += speed*dirR;
        }

        void stop() {
            if (x <= 0)
                x = 0;
            if (x + width >= 800)
                x = 800 - width;
        }

        int getX() {
            return x;
        }

        int getY() {
            return y;
        }

        int getWidth() {
            return width;
        }

        int getHeight() {
            return height;
        }


        void paint(Graphics2D g) {
            g.fillRect(x,y,width,height);
        }

        public void keyReleased(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_LEFT)
                dirL = 0;
            else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
                dirR = 0;
            else {
                dirL = 0;
                dirR = 0;
            }

        }

        public void keyPressed(KeyEvent e) {
            switch(e.getKeyCode()) {
                case KeyEvent.VK_LEFT:
                    dirL = 1;
                    break;
                case KeyEvent.VK_RIGHT:
                    dirR = 1;
                    break;
            }
        }
    }

    public class Brick {
      private
        int x;
        int y;
        int width;
        int height;
        boolean alive;
        boolean inX = false,inY = false;

      public

        void setUpBrick(int px, int py, int w, int h, boolean al) {
          x = px;
          y = py;
          width = w;
          height = h;
          alive = al;
      }

        void setAlive(boolean alive) {
            this.alive = alive;
        }

        void paint(Graphics2D g) {
            if (alive)
                g.fillRect(x,y,width,height);
        }

        boolean collision(int bx, int by) {
            if (alive) {
                if (bx + 10 >= x && bx <= x + width && by + 10 >= y && by <= y + height) {
                    setAlive(false);
                    return true;
                } else return false;
            }
            else return false;
        }

        void  inAreaX(int bx) {
            if (bx + 10 >= x && bx <= x + width) {
                System.out.println("inAreaX");
                inX = true;
            } else {
                inX = false;
            }
        }

        void  inAreaY(int by) {
            if (by + 10 >= y && by <= y + height) {
                System.out.println("inAreaY");
                inY = true;
            } else {
                inY = false;
            }
        }

        boolean isInAreaX () {
            if (inX)
                return true;
            else return false;
        }

        boolean isInAreaY () {
            if (inY)
                return true;
            else return false;
        }
    }

    Ball ball = new Ball();
    Paddle paddle = new Paddle();
    Brick[][] brick = new Brick[8][4];

    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 4; j++) {
            brick[i][j].setUpBrick(j * 101, i * 51, 100, 50, true);
        }
    }

    void bounce() {
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 4; j++) {
                brick[i][j].inAreaX(ball.getX());
                brick[i][j].inAreaY(ball.getY());
                if (brick[i][j].collision(ball.getX(), ball.getY())) {
                    if (brick[i][j].isInAreaX()) {
                        ball.setDiry();
                    } else if (brick[i][j].isInAreaY()) {
                        ball.setDirx();
                    }
                }
            }
        }
    }

    void move() {
        ball.bounce(paddle.getX(), paddle.getY(), paddle.getWidth(),paddle.getHeight());
        ball.move();
        paddle.move();
        paddle.stop();
        bounce();
    }

    public Breakout() {
        addKeyListener(new KeyListener() {

            public void keyTyped(KeyEvent e) {
            }

            public void keyReleased(KeyEvent e) {
                paddle.keyReleased(e);
            }

            public void keyPressed(KeyEvent e) {
                paddle.keyPressed(e);
            }
        });
        setFocusable(true);
    }

    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(Color.RED);
        ball.paint(g2d);
        g2d.setColor(Color.BLACK);
        paddle.paint(g2d);
        g2d.setColor(Color.ORANGE);
        for (int i = 0; i < 8; i++)
            for (int j = 0; j < 4; j++)
                brick[i][j].paint(g2d);
    }

    public static void main(String[] args) throws InterruptedException {
        JFrame window = new JFrame("Tennis");
        Breakout game = new Breakout();

        window.add(game);
        window.setSize(800,600);
        window.setVisible(true);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        while (true) {
            game.move();
            game.repaint();
            Thread.sleep(10);
        }
    }
包断开;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.RenderingHints;
导入java.awt.event.KeyEvent;
导入java.awt.event.KeyListener;
导入java.awt.Color;
导入java.awt.Font;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.JOptionPane;
公共类突破扩展了JPanel{
公共班级舞会{
私有的
int x=400;
int y=300;
内速度=2;
int-dirx=1;
int-diry=-1;
公众的
无效反弹(int px、int py、int lx、int ly){

如果((x+10>=800&&dirx==1)| |(x将逻辑从
setUpBrick
移动到构造函数

 public class Brick {

    private int x;
    private int y;
    private int width;
    private int height;
    private boolean alive;
    private boolean inX = false,inY = false;

  public Brick(int px, int py, int w, int h, boolean al) {
      x = px;
      y = py;
      width = w;
      height = h;
      alive = al;
  }
  ...
}
然后改变

brick[i][j].setUpBrick(j * 101, i * 51, 100, 50, true);

还要注意,访问修饰符不适用于类的整个部分

private
    int x;
    int y;
    int width;
    int height;
    boolean alive;
    boolean inX = false,inY = false;
意味着只有
x
private
。其余成员将获得默认访问修饰符

还有一个提示。你可以简化一些方法

boolean isInAreaY () {
    if (inY)
        return true;
    else return false;
}
可以写为:

boolean isInAreaY() {
    return inY;
}

为了在Java中创建2D数组,您可以创建如下数组:

Object testArray = new Object[10][10];
这个数组现在是一个10*10的数组,内存分配给100个对象引用

可以使用双for循环创建两个对象的指针:

for (int i = 0; i < testArray.length(); i++) {
        for (int j = 0; j < testArray.length; j++) {
       testArray[i][j] = Object; //here you input the objects that you want to point to     
    }
}
for(int i=0;i


除非我错误地计算了开始和结束括号,否则for循环不在任何方法中,而是直接在类主体中。这就是为什么您会得到意外的标记。您可能会希望将其移动到
突破
构造函数中。

这并不能直接回答您的问题,但代码的格式是像您这样的人希望所有成员都是私有的,所有方法都是公共的?修饰符不能作为“节”使用,因为您只是在修改第一个成员/方法的可见性,其他所有内容都是包私有的。此外,您可能希望将更广泛的内部类移动到单独的文件中,以改进代码结构
for (int i = 0; i < testArray.length(); i++) {
        for (int j = 0; j < testArray.length; j++) {
       testArray[i][j] = Object; //here you input the objects that you want to point to     
    }
}