0

im really new in Java. I just need to explain how to declare 2D array of objects, i have something like:

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

i need to inicialize 2d array of brick, but it says that first for is unexpected token. How to write it? Thank you.

1
  • 2
    This does not answer your question directly, but the way your code is formatted looks like you want all members to be private, all methods to be public? The modifiers to not work as "sections", as it is, you're only modifying the visibility of the first member/method, and everything else is package private. Also, you might want to move your more extensive inner classes into separate files to improve your code structure Commented Apr 1, 2014 at 15:53

3 Answers 3

1

Unless if I have miscounted your opening and closing braces, your for loop is not inside any method, it's directly in your class body. That's why you're getting unexpected token. You will probably want to move it into the Breakout constructor.

Sign up to request clarification or add additional context in comments.

Comments

0

In order to create a 2D array in Java, you can create such an array like this:

Object testArray = new Object[10][10];

This array is now a 10*10 array with memory allocated for 100 Object references.

You can create pointers two Objects with a double for-loop:

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

Comments

0

Move the logic from setUpBrick to a constructor.

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

Then change

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

to

brick[i][j] = new Brick(j*101, i*51, 100, 50, true);

Also note that access modifiers don't apply to a whole section of your class. In your example,

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

means that only x is going to be private. The rest of the members will get the default access modifier.

One more tip. A couple of your methods can be simplified.

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

can be written as:

boolean isInAreaY() {
    return inY;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.