HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Simple random falling object animation in Java

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
randomanimationsimplefallingjavaobject

Problem

I am new to programming but I tried to make my code as readable as possible! Hopefully you can see what it does before you run the program. I did not implement an ups or fps controller, the animation may be fast for some computers and slower for others.

```
import javax.swing.*;
import java.awt.*;
import java.util.Random;

public class App extends JPanel {

//changing these values will change the size of the game, while still remaining functional
//within the size limit specified.
static int windowWidth = 1300;
static int windowHeight = 800;

int randNumb = 0;
int squareWidth = 25;
int squareHeight = 25;
int squareYLocation = -squareHeight;
boolean numberCreated = false;
static boolean gameRunning = false;

//generates a random Y value inside the window for the square to spawn at
public void generateRandomNumber() {
Random rand = new Random();
randNumb = rand.nextInt(windowWidth - squareWidth);
numberCreated = true;
}

//paints a black screen, then paints a rectangle on top of the black screen
public void paint(Graphics g) {
g.setColor(Color.black);
g.fillRect(0, 0, windowWidth, windowHeight);
g.setColor(Color.BLUE);
g.fillRect(randNumb, squareYLocation, squareWidth, squareHeight);
}

public void update() {

//calls the generateRandomNumber() method which gives the square a random x value inside the screen
if (!numberCreated) {
generateRandomNumber();
}
//moves the squares y coordinate towards the bottom of the screen and stops once it hits the bottom
if (squareYLocation <= windowHeight) {
squareYLocation++;

//resets the x and y location to a new position
} else {
numberCreated = false;
squareYLocation = -squareHeight;
}
}

//sets the while loop to true to start the game
public void start() {
gameRunning = tr

Solution

Here is my list of possible improvements:

-
Constants should be made final and if possible also private

private static final int WINDOW_WIDTH = 1300;
private static final int WINDOW_HEIGHT = 800;
private static final int SQUARE_WIDTH = 25;
private static final int SQUARE_HEIGHT = 25;


-
Class members should remain private, if accessible from other classes use setters/getters

private int randNumb = 0;
private int squareYLocation = -SQUARE_HEIGHT;
private boolean numberCreated;
private boolean gameRunning;


'

-
Limit class methods visibility, if no other class uses them

private void generateRandomNumber() {
    Random rand = new Random();
    randNumb = rand.nextInt(windowWidth - squareWidth);
    numberCreated = true;
}

private void paint(Graphics g) {
    g.setColor(Color.black);
    g.fillRect(0, 0, windowWidth, windowHeight);
    g.setColor(Color.BLUE);
    g.fillRect(randNumb, squareYLocation, squareWidth, squareHeight);
}


-
Don't use one letter names for object instances, use the full name or give an appropriate name that is easy to understand or reflects something relevant.

private void paint(Graphics graphics) {
    graphics.setColor(Color.black);
    graphics.fillRect(0, 0, windowWidth, windowHeight);
    graphics.setColor(Color.BLUE);
    graphics.fillRect(randNumb, squareYLocation, squareWidth, squareHeight);
}


-
Consistent use of Whitespace, it ok to add extras white lines but be consistent about it. Eg. update/main have a whiteline, other methods don't.

-
The naming of numberCreated was a bit confusing, I first thought it was only generated once but after looking through the code I saw that the flag could be set to false again. I would suggest changing to a different name eg invalidateNumber or something else that makes it more clear.

-
App extends JFrame is not a good name for the class, GameFrame extends JFrame would be better. And even better would be the use of the composition pattern to have an App class. (You can separate your game logic from its View (MVC)).

-
Commenting style is not really conform the guidelines. Your style for inline comments is ok. But normally method comments are something like this:

/**
  * Your comment goes here, also use 
  * @param for your params
  */
 public void paint(Graphics g) {
     g.setColor(Color.black);
     g.fillRect(0, 0, windowWidth, windowHeight);
     g.setColor(Color.BLUE);
     g.fillRect(randNumb, squareYLocation, squareWidth, squareHeight);
 }

Code Snippets

private static final int WINDOW_WIDTH = 1300;
private static final int WINDOW_HEIGHT = 800;
private static final int SQUARE_WIDTH = 25;
private static final int SQUARE_HEIGHT = 25;
private int randNumb = 0;
private int squareYLocation = -SQUARE_HEIGHT;
private boolean numberCreated;
private boolean gameRunning;
private void generateRandomNumber() {
    Random rand = new Random();
    randNumb = rand.nextInt(windowWidth - squareWidth);
    numberCreated = true;
}

private void paint(Graphics g) {
    g.setColor(Color.black);
    g.fillRect(0, 0, windowWidth, windowHeight);
    g.setColor(Color.BLUE);
    g.fillRect(randNumb, squareYLocation, squareWidth, squareHeight);
}
private void paint(Graphics graphics) {
    graphics.setColor(Color.black);
    graphics.fillRect(0, 0, windowWidth, windowHeight);
    graphics.setColor(Color.BLUE);
    graphics.fillRect(randNumb, squareYLocation, squareWidth, squareHeight);
}
/**
  * Your comment goes here, also use 
  * @param for your params
  */
 public void paint(Graphics g) {
     g.setColor(Color.black);
     g.fillRect(0, 0, windowWidth, windowHeight);
     g.setColor(Color.BLUE);
     g.fillRect(randNumb, squareYLocation, squareWidth, squareHeight);
 }

Context

StackExchange Code Review Q#73847, answer score: 10

Revisions (0)

No revisions yet.