I am creating a reversi game. I decided to create my board using JLabels. The problem I am having is getting the four starting pieces onto the board. This is only a 2 player game, and it is 1v1. At the beginning of the game each player starts off with 2 pieces in the very middle of the board. In my program I go to create 2 blue and 2 red pieces for the 2 players but no matter what I do I can't get the pieces to show up on the game board. When I ran my debugger it looked like i was never giving each BoardSquare the correct x and y values. I am not sure how to get the x and y values of the BoardSquares. Any and all help/criticism is welcome. I have been stuck trying to fix this for hours. Thanks.
public class ReversiRemake {
public static void main(String[] args) {
PlayGame game = new PlayGame();
}
}
class BoardSquare extends JLabel {
private int x;
private int y;
private int row;
private int column;
private MyShape circle;
private Border b = BorderFactory.createLineBorder(Color.BLACK, 2);
BoardSquare(int row, int column) {
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == 1) {
}
}
});
this.row = row;
this.column = column;
setBorder(b);
}
public void setShape(Player p){
if(p instanceof HumanPlayer){
circle = new MyShape(x, y, Color.blue);
}
if(p instanceof aiPlayer){
circle = new MyShape(x, y, Color.red);
}
}
public void setX(int x){
this.x = x;
}
public void setY(int y){
this.y = y;
}
public MyShape getShape() {
return circle;
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
circle.drawShape(g2);
}
}
class MyShape {
private Color color;
private int x;
private int y;
MyShape(int x, int y, Color color) {
this.x = x;
this.y = y;
this.color = color;
}
MyShape(Color color) {
this.color = color;
}
public void drawShape(Graphics2D g) {
g.setPaint(color);
g.drawOval(x + 5, y + 2, 50, 50);
g.fillOval(x + 5, y + 2, 50, 50);
}
public void setColor(Player p) {
if (p instanceof HumanPlayer) {
color = Color.BLUE;
}
if (p instanceof aiPlayer) {
color = Color.red;
}
}
}
class GameBoard extends JPanel {
BoardSquare[][] BoardSquares = new BoardSquare[8][8];
private BoardSquare bs;
GameBoard() {
for (int row = 0; row < 8; row++) {
for (int column = 0; column < 8; column++) {
BoardSquares[row][column] = new BoardSquare(row, column);
add(BoardSquares[row][column]);
}
}
}
public void StartingPieces(HumanPlayer p1, aiPlayer p2) {
for (int row = 0; row < 8; row++) {
for (int column = 0; column < 8; column++) {
BoardSquares[row][column] = bs;
if (row == 3 && column == 3) {
bs.setShape(p1);
}
if (row == 3 && column == 4) {
bs.setShape(p2);
}
if (row == 4 && column == 3) {
bs.setShape(p2);
}
if (row == 4 && column == 4) {
bs.setShape(p1);
}
}
}
}
}
abstract class Player {
int playerType;
Color color;
int score;
Player(int playerType, Color color, int score) {
this.playerType = playerType;
this.color = color;
this.score = score;
}
}
class HumanPlayer extends Player {
HumanPlayer() {
super(1, Color.BLUE, 0);
}
}
class aiPlayer extends Player {
aiPlayer() {
super(2, Color.RED, 0);
}
}
class PlayGame {
private HumanPlayer p1 = new HumanPlayer();
private aiPlayer p2 = new aiPlayer();
private GameBoard gameBoard = new GameBoard();
private boolean p1Turn;
private boolean p2Turn;
PlayGame() {
setStartingPieces();
}
public void setStartingPieces() {
gameBoard.StartingPieces(p1, p2);
}
}
I couldn't run your code as it is posted, but I believe upon visual inspection the line
BoardSquares[row][column] = bs;
should be
bs = BoardSquares[row][column];
As it is written above, a NullPointerException would occur and bs is never actually getting set. The revision I have offered would allow the shape to be set in the GameBoard class.
Related
Whenever I tell the rectangle to go left, it moves al the way to the left side of the screen when I only want it to move a little bit. And whenever I tell it to move right, it wont move at all. What am I doing wrong?
public class Balls {
private static final int MOVE_SPEED_X = 2;
private int x, y, width, height, velX, velY;
private Rectangle rect;
public Balls(int x, int y, int height, int width) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
velX = 0;
}
public void update() {
y += 3;
}
public void reset() {
y = 0;
}
public void accelLeft() {
x = -MOVE_SPEED_X;
}
public void accelRight() {
x = +MOVE_SPEED_X;
}
}
Second Block of relevant code
public class PlayState extends State {
private Paddle paddleRight, paddleLeft;
private static final int PADDLE_WIDTH = 30;
private static final int PADDLE_HEIGHT = 60;
private Ball ball;
private Balls balls;
// bDiam stands for ball diameter
private static int bDiam = 30;
private int playerScore = 0;
private Font scoreFont;
#Override
public void init() {
paddleLeft = new Paddle(0, 195, PADDLE_WIDTH, PADDLE_HEIGHT);
paddleRight = new Paddle(785, 195, PADDLE_WIDTH, PADDLE_HEIGHT);
scoreFont = new Font("SansSerif", Font.BOLD, 25);
//ball = new Ball(300, 200, bDiam, bDiam);
balls = new Balls(500, 0, bDiam, bDiam);
}
#Override
public void onClick(MouseEvent e) {
// bDiam = bDiam + 20;
}
#Override
public void update() {
balls.update();
if (balls.getY() > GameMain.GAME_HEIGHT) {
balls.reset();
}
}
#Override
public void onKeyPress (KeyEvent e){
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
balls.accelRight();
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
balls.accelLeft();
}
}
}
In accelLeft it should be x = x - MOVE_SPEED_X (and similar in accelRight)
I (A novice programmer) am trying to paint an oval over a JPanel. I am trying to use the method paint. However, it requires a Graphics argument. I get a NullPointerException when I include my Graphics as a argument because it is null, but I do not know how else to paint the oval. I tried repaint instead but nothing happened. Any help would be appreciated. Here is my main class:
public class Checkers extends JPanel{
public static final int BOARDSQUARES = 8;
public static final int BOARDSIZE = 75;
private JPanel[][] board;
private final Point point1;
private final LineBorder border1;
public JFrame frame;
checkerPiece piece = new checkerPiece(this);
Graphics g;
public Checkers(){
board = new JPanel[BOARDSQUARES][BOARDSQUARES];
point1 = new Point (BOARDSIZE,BOARDSIZE);
border1=new LineBorder(Color.BLACK, 1);
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Checkers checkers = new Checkers();
checkers.frame=checkers.createJFrame();
checkers.board =checkers.createBoard(checkers.board, checkers.point1, checkers.border1);
for (int y=0;y<BOARDSQUARES;y++){
for (int x = 0;x<BOARDSQUARES;x++){
checkers.frame.getContentPane().add(checkers.board[y][x]);
}
}
checkers.paint(checkers.g);
}
private JPanel[][] createBoard (JPanel[][] board, Point point, LineBorder border1){
for (int row = 0; row<BOARDSQUARES;row++){
point.y=BOARDSIZE;
for (int col = 0; col <BOARDSQUARES;col++){
board[row][col] = new JPanel();
board[row][col].setLocation(point);
board[row][col].setVisible(true);
board[row][col].setSize(BOARDSIZE,BOARDSIZE);
board[row][col].setOpaque(true);
if ((row%2==0&&col%2==0)||(row%2==1&&col%2==1)){
board[row][col].setBackground(new Color (230,200,150));
} else if ((row%2==0&&col%2==1)||(row%2==1&&col%2==0)) {
board[row][col].setBackground(new Color (165, 42,42) );
}
board[row][col].setBorder(border1);
point.y = point.y+BOARDSIZE;
}
point.x=point.x+BOARDSIZE;
}
return board;
}
private JFrame createJFrame (){
JFrame mainFrame = new JFrame("Checkers");
mainFrame.setSize(1000,1000);
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.add(new Checkers());
return mainFrame;
}
#Override
public void paint (Graphics g){
System.out.println("hi");
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
piece.paint(g2d,board[0][0].getLocation().x,board[0][0].getLocation().y,Color.BLACK);
}
}
A necessary snippet from my other class (cherkerPiece piece):
public void paint (Graphics2D g, int x, int y, Color color){
g.setColor(color);
g.fillOval(x, y, DIAMETER, DIAMETER);
}
Thank you for your help
To create something as complex as a checkers game, you should follow the model / view / controller pattern when creating your Java Swing GUI.
I created a model class for a checker board square and another model class for a checker piece. I created a model class for the checker board and another model class to hold all of the checker pieces.
I created a view class to draw the checker board. I created another view class to create the main window.
I did not create any controller classes. This code just shows you how to draw a checker board. I put all of the classes together to make it easier to paste the code. You should separate the classes into separate Java files.
package com.ggl.testing;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class CheckerBoard implements Runnable {
private Board board;
private CheckerBoardPanel checkerBoardPanel;
private JFrame frame;
private Pieces pieces;
public static void main(String[] args) {
SwingUtilities.invokeLater(new CheckerBoard());
}
public CheckerBoard() {
this.board = new Board();
this.pieces = new Pieces();
}
#Override
public void run() {
frame = new JFrame("Checker Board");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
checkerBoardPanel = new CheckerBoardPanel(board, pieces);
frame.add(checkerBoardPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public class CheckerBoardPanel extends JPanel {
private static final long serialVersionUID = 3770663347384271771L;
private Board board;
private Pieces pieces;
public CheckerBoardPanel(Board board, Pieces pieces) {
this.board = board;
this.pieces = pieces;
this.setPreferredSize(board.getPreferredSize());
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Square[][] checkerBoard = board.getBoard();
for (int row = 0; row < checkerBoard.length; row++) {
for (int column = 0; column < checkerBoard[row].length; column++) {
Square square = checkerBoard[row][column];
Rectangle drawingRectangle = square.getDrawingRectangle();
g.setColor(square.getColor());
g.fillRect(drawingRectangle.x, drawingRectangle.y,
drawingRectangle.width, drawingRectangle.height);
}
}
List<Piece> checkerPieces = pieces.getPieces();
for (Piece checkerPiece : checkerPieces) {
Point coordinate = checkerPiece.getCoordinate();
Rectangle drawingRectangle = checkerBoard[coordinate.x][coordinate.y]
.getDrawingRectangle();
int x = drawingRectangle.x + drawingRectangle.width / 2;
int y = drawingRectangle.y + drawingRectangle.height / 2;
int radius = board.getSquareWidth() * 2 / 6;
g.setColor(checkerPiece.getColor());
g.fillOval(x - radius, y - radius, radius + radius, radius
+ radius);
}
}
}
public class Board {
private static final int BOARD_WIDTH = 8;
private static final int SQUARE_WIDTH = 64;
private Square[][] board;
public Board() {
this.board = initalizeBoard(BOARD_WIDTH, SQUARE_WIDTH);
}
private Square[][] initalizeBoard(int boardWidth, int squareWidth) {
Square[][] board = new Square[boardWidth][boardWidth];
int x = 0;
int y = 0;
for (int row = 0; row < boardWidth; row++) {
for (int column = 0; column < boardWidth; column++) {
Square square = new Square();
if (isLightSquare(row, column)) {
square.setColor(Color.WHITE);
} else {
square.setColor(Color.LIGHT_GRAY);
}
square.setCoordinate(new Point(row, column));
square.setDrawingRectangle(new Rectangle(x, y, squareWidth,
squareWidth));
board[row][column] = square;
x += squareWidth;
}
x = 0;
y += squareWidth;
}
return board;
}
public boolean isLightSquare(int row, int column) {
if (row % 2 == 0) {
if (column % 2 == 0) {
return true;
} else {
return false;
}
} else {
if (column % 2 == 0) {
return false;
} else {
return true;
}
}
}
public Dimension getPreferredSize() {
int width = SQUARE_WIDTH * 8 + 1;
return new Dimension(width, width);
}
public Square[][] getBoard() {
return board;
}
public int getSquareWidth() {
return SQUARE_WIDTH;
}
}
public class Square {
private Color color;
private Point coordinate;
private Rectangle drawingRectangle;
public Point getCoordinate() {
return coordinate;
}
public void setCoordinate(Point coordinate) {
this.coordinate = coordinate;
}
public Rectangle getDrawingRectangle() {
return drawingRectangle;
}
public void setDrawingRectangle(Rectangle drawingRectangle) {
this.drawingRectangle = drawingRectangle;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
}
public class Pieces {
private List<Piece> pieces;
public Pieces() {
this.pieces = addPieces();
}
private List<Piece> addPieces() {
List<Piece> pieces = new ArrayList<Piece>();
Piece piece = new Piece();
piece.setColor(Color.RED);
piece.setCoordinate(new Point(2, 1));
pieces.add(piece);
piece = new Piece();
piece.setColor(Color.BLACK);
piece.setCoordinate(new Point(5, 0));
pieces.add(piece);
// Add the rest of the red and black pieces here
return pieces;
}
public List<Piece> getPieces() {
return pieces;
}
public void addPiece(Piece piece) {
this.pieces.add(piece);
}
}
public class Piece {
private Color color;
private Point coordinate;
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public Point getCoordinate() {
return coordinate;
}
public void setCoordinate(Point coordinate) {
this.coordinate = coordinate;
}
}
}
you need to add a class that extends Canvas and do all your painting in that class. like this
public class FirstWindow extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;
public FirstWindow()
{
super("Kayte does not go to water parks");
setSize(500, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
}
}
public class Start extends Canvas implements Runnable
public class Main
{
public static void main(String arg[])
{
FirstWindow fw = new FirstWindow();
Start game = new Start(500, 500);
fw.add(game);
fw.setVisible(true);
new Thread(game).start();
}
}
Main class this is main class of my program
public class Main {
public static void main(String[] args) {
BallWorld frm = new BallWorld(3);
frm.setVisible(true);
for (int i=0; i<1000; i++){
frm.stepTheBall();
}
}
}
BallWorld.java class this class related with JFrame
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.JFrame;
public class BallWorld extends JFrame {
public final int FRAMEWIDTH = 600;
public final int FRAMEHEIGHT = 400;
private Ball[] ballArr;
private int ballCnt;
public BallWorld(int ballCnt){
super();
setSize(FRAMEWIDTH, FRAMEHEIGHT);
setTitle("My Bouncing Ball Application");
ballArr = new Ball[ballCnt];
this.ballCnt = ballCnt;
for (int i=0; i < ballCnt; i++){
ballArr[i] = new Ball(new Point(50,50), 5);
int ddx = (int) (5*Math.random()); //Exercise 1
int ddy = (int) (4*Math.random()); //Exercise 1
ballArr[i].setMotion(ddx, ddy);
}
}
public void paint(Graphics g){
super.paint(g);
for (int i=0; i < ballCnt; i++){
ballArr[i].paint(g);
}
}
public void stepTheBall(){
for (int i=0; i < ballCnt; i++){
ballArr[i].move();
Point loc = ballArr[i].getLocation();
if (loc.x < ballArr[i].getRadius() ||
loc.x > FRAMEWIDTH-ballArr[i].getRadius()){
ballArr[i].reclectVert();
}
if (loc.y < ballArr[i].getRadius() ||
loc.y > FRAMEHEIGHT-ballArr[i].getRadius()){
ballArr[i].reclectHoriz();
}
}
repaint();
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Ball.java class this class related with balls information
public class Ball {
private Point location;
private int radius;
private Color color;
private int dx, dy;
public Ball(Point l, int r, Color c){
location = l;
radius = r;
color = c;
}
public Ball(Point l, int r){
location = l;
radius = r;
color = Color.RED;
}
public Point getLocation() {
return location;
}
public void setLocation(Point location) {
this.location = location;
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public void setMotion(int dx, int dy){
this.dx = dx;
this.dy = dy;
}
public void move(){
location.translate(dx, dy);
}
public void moveTo(int x, int y){
location.move(x, y);
}
public void paint (Graphics g) {
g.setColor (color);
g.fillOval (location.x-radius, location.y-radius, 2*radius, 2*radius);
}
public void reclectHoriz() {
dy = -dy;
}
public void reclectVert() {
dx = -dx;
}
}
I want to add yellow,blue,red balls and different radius that includes. How can i write that informations
These are the lines at fault:
ballArr[i] = new Ball(new Point(50,50), 5);
Here, you call the two-argument constructor from Ball. It looks like this:
public Ball(Point l, int r){
location = l;
radius = r;
color = Color.RED;
}
So your balls will all be red, have a radius of 5 and be at position 50,50. You do have a three-argument constructor that also sets the color of a ball. If you want these things to be random, use a Random object, select a random color, radius and point for each ball, and there you go.
It should be clear how you get random numbers for radius and point. In case you're wondering about Color, here's a way: Define an array containing Color objects.
Color[] colors = {Color.Red, Color.Blue, Color.Yellow};
Get a random number based on the size of the array,
int colornumber = random.nextInt(colors.length);
and retrieve the color
Color c = colors[colornumber]
Then, create balls with random properties.
EDIT
public class BallWorld {
....
private Random random = new Random();
private Color[] colors={Color.red,Color.blue,Color.yellow};
public BallWorld(int ballCnt){
super();
setSize(FRAMEWIDTH, FRAMEHEIGHT);
setTitle("My Bouncing Ball Application");
ballArr = new Ball[ballCnt];
this.ballCnt = ballCnt;
for (int i=0; i < ballCnt; i++){
----> // Create attributes here
int bcn = random.nextInt(colors.length);
Color ballcolor = colors[bcn];
int ballradius = random.nextInt(10); // change to suit your needs
----> int posx = random.nextInt(200); // change to suit your needs
----> int posy = random.nextInt(200); // change to suit your needs
// this creates a ball given the above calculated parameters
----> ballArr[i] = new Ball(new Point(posx,posy), ballradius, ballcolor);
int ddx = (int) (5*Math.random()); //Exercise 1
int ddy = (int) (4*Math.random()); //Exercise 1
ballArr[i].setMotion(ddx, ddy);
}
}
Instead of this:
ballArr[i] = new Ball(new Point(50,50), 5);
use another constructor:
ballArr[i] = new Ball(new Point(50,50), 5, Color.xxx);
I have a problem with my program. I'm trying to move trains over a rail, these trains are represented by ellipses. When I try to move these objects, they are drawn in the new position, but they are also drawn in the previous position.
trensPretos is a linkedlist of trem class:
'public LinkedList trensPretos = new LinkedList();'
Here is my code:
public void AtualizaTrensPretos()
{
int currentX;
int currentY;
// trens pretos se movem
for (Trem t:trensPretos)
{
while(t.get_x() < 1100)
{
currentX = t.get_x();
currentY = t.get_y();
t.setNew_x(currentX + moveX);
// antes da linha reta
if (t.get_x() < 270)
{
t.setNew_y(currentY + moveY);
}
else if(t.get_x() > 730)
{// depois da linha reta
t.setNew_y(currentY - (moveY+1));
}
setChanged();
notifyObservers(t);
}
// removo o trem após ele passar pelo cruzamento
// trensPretos.remove(t);
}
}
// Observer
// recebo trem e desenho
g2d = (Graphics2D) this.getGraphics();
if (arg instanceof Trem)
{
if (g2d != null)
{
g2d.setColor(((Trem) arg).getColor());
g2d.fill(((Trem) arg).getEllipse());
}
}
// Trem class
public class Trem {
private int posX;
private int posY;
private Color corTrem;
private Ellipse2D formaDoTrem;
private int sizeX = 30;
private int sizeY = 30;
public Trem(Color corDoTrem)
{
formaDoTrem = new Ellipse2D.Double();
this.corTrem = corDoTrem;
}
public Color getColor()
{
return this.corTrem;
}
public void setNew_x(int x)
{
this.posX = x;
}
public void setNew_y(int y)
{
this.posY = y;
}
public int get_x()
{
return this.posX;
}
public int get_y()
{
return this.posY;
}
public Ellipse2D getEllipse()
{
this.formaDoTrem.setFrame(posX, posY, sizeX, sizeY);
return this.formaDoTrem;
}
}
What could be the problem?
they are drawn in the new position, but they are also drawn in the previous position.
You need to clear the background area first before repainting the trains.
Im currently am working on a game in Java that spawns blocks on the top of the screen and they fall down (you have to dodge them). Currently I have the game spawning blocks on the top of the screen from an array but I do not know how to make their y position go down by (int).
Basically I just need help making a public void that checks an object array and with every instance of a object it finds it drops the y position of it by 12.
Selected snippits from code:
static Object[][] food = new Object[7][700];
public void draw() {
try {
Graphics g = bufferStrategy.getDrawGraphics();
g.clearRect(0, 0, this.getSize().width, this.getSize().height);
// Draw to back buffer
g.setColor(Color.WHITE);
g.fillRect(0, 0, this.getSize().width, this.getSize().height);
g.setColor(Color.BLUE);
g.fillRect(Player.getX(), Player.getY(), Player.WIDTH, Player.HEIGHT);
g.setColor(Color.GRAY);
for(int x = 0; x < food.length; x++) {
for(int y = 0; y < food[x].length; y ++) {
Object o = food[x][y];
if(o instanceof Hamburger) {
new Hamburger(x * 100, y, g);
}
else if(o instanceof Salad) {
new Salad(x * 100, y, g);
}
}
}
GUI.draw(g);
} catch(Exception e) {
e.printStackTrace();
} finally {
bufferGraphics.dispose();
}
}
public void dropBlocks() {
}
public void addBlock() {
if(new Random().nextInt(2) == 1) {
food[new Random().nextInt(7)][0] = new Hamburger(0, 0);
} else food[new Random().nextInt(7)][0] = new Salad(0, 0);
}
public void drawBackbufferToScreen() {
bufferStrategy.show();
Toolkit.getDefaultToolkit().sync();
}
public void run() {
int i = 0;
while(running) {
i++;
draw();
Player.update();
drawBackbufferToScreen();
if(i == 50) {
addBlock();
i = 0;
}
dropBlocks();
Thread.currentThread();
try {
Thread.sleep(10);
} catch(Exception e) {
e.printStackTrace();
}
}
}
Food.java (What Hamburger and Salad extend)
public class Food {
public static int x;
public static int y;
public static int HEIGHT;
public static int WIDTH;
public int type;
private static Image blockImage;
// Default constructor
public Food() {
Food.x = 0;
Food.y = 0;
Food.HEIGHT = 80;
Food.WIDTH = 80;
}
public Food(int x, int y) {
Food.x = x;
Food.y = y;
Food.HEIGHT = 80;
Food.WIDTH = 80;
}
// Getters and setters
I would do something like this:
Object[][] board = new Object[7][700]; //Game board.
//you can also switch the width and height
//Initialization
for (int x = 699; x >= 0; x--) {
for (int y = 0; y < 7; y++) {
if (x == 699) {
board[y][x] = BLANK; //set spot to open if it is the bottom row
continue;
}
board[y][x+1] = board[y][x]; //move row down
}
}
//Generate new Objects if needed for the top row of the board.
Clearly Object does not have a getY() method. You could do ugly things with casting and instanceof, but this design is rather putrid. Try moving towards an object oriented model.
interface Drawable {
int getX();
int getY();
void moveDown(int numSpaces);
}
class Hamburger implements Drawable {
private int x;
private int y;
public Hamburger(final int xPos, final int yPos) {
x = xPos;
y = yPos;
}
#Override
public void moveDown(final int numSpaces) {
// eg negative number, off the screen
if(isValid(numSpaces)) {
setY(getY() - numSpaces);
}
}
}
class Positioner {
public static void moveMyObjects(final Drawable[][] objs) {
for(Drawable[] rows : objs) {
for(Drawable col : rows) {
// clearly Object doesn't have a getY() method, so you should create your own interface and implement it
col.moveDown(12);
}
}
}
}