Internal visibility modifier (internal)

The internal visibility modifier is used to declare a member visible within the same module. A module is a collection of Kotlin files compiled together.  A module may be a Maven project, a Gradle source set, and IntelliJ IDEA module, or a set of files compiled with an Ant task invocation. Using the internal modifier is similar to using other visibility modifiers:

internal class Person { }

Having understood access and visibility modifiers, we can continue with the implementation of the Block class. The next thing we need to do is create a constructor for the class that initializes the instance variables we have created to their initial states. Constructor definitions in Java are syntactically different from Kotlin constructor definitions:

public class Block {
private int shapeIndex;
private int frameNumber;
private BlockColor color;
private Point position;

Let's see the constructor definition:

  private Block(int shapeIndex, BlockColor blockColor) {
this.frameNumber = 0;
this.shapeIndex = shapeIndex;
this.color = blockColor;
this.position = new Point(AppModel.FieldConstants
.COLUMN_COUNT.getValue() / 2, 0);
}

public enum BlockColor {
PINK(Color.rgb(255, 105, 180), (byte) 2),
GREEN(Color.rgb(0, 128, 0), (byte) 3),
ORANGE(Color.rgb(255, 140, 0), (byte) 4),
YELLOW(Color.rgb(255, 255, 0), (byte) 5),
CYAN(Color.rgb(0, 255, 255), (byte) 6);

BlockColor(int rgbValue, byte value) {
this.rgbValue = rgbValue;
this.byteValue = value;
}

private final int rgbValue;
private final byte byteValue;
}
}

Notice that the preceding constructor definition has been given private access. We did this because we do not want this constructor to be accessed outside of Block. As we still want other classes to have a means of creating a block instance, we have to define a static method that permits this. We will call this method createBlock:

public class Block {
private int shapeIndex;
private int frameNumber;
private BlockColor color;
private Point position;

Let's see the Constructor definition:


private Block(int shapeIndex, BlockColor blockColor) {
this.frameNumber = 0;
this.shapeIndex = shapeIndex;
</span> this.color = blockColor;
this.position = new Point( FieldConstants.COLUMN_COUNT
.getValue()/2, 0);
}

public static Block createBlock() {
Random random = new Random();
int shapeIndex = random.nextInt(Shape.values().length);
BlockColor blockColor = BlockColor.values()
[random.nextInt(BlockColor
.values().length)];

Block block = new Block(shapeIndex, blockColor);
block.position.x = block.position.x - Shape.values()
[shapeIndex].getStartPosition();
return block;
}

public enum BlockColor {
PINK(Color.rgb(255, 105, 180), (byte) 2),
GREEN(Color.rgb(0, 128, 0), (byte) 3),
ORANGE(Color.rgb(255, 140, 0), (byte) 4),
YELLOW(Color.rgb(255, 255, 0), (byte) 5),
CYAN(Color.rgb(0, 255, 255), (byte) 6);

BlockColor(int rgbValue, byte value) {
this.rgbValue = rgbValue;
this.byteValue = value;
}

private final int rgbValue;
private final byte byteValue;
}
}

createBlock() randomly selects the index of a tetromino shape in the Shape enum class and a BlockColor and assigns two randomly selected values to shapeIndex and blockColor. A new Block instance is created with the two values passed as arguments and the position of the block along the X axis is set. Lastly, createBlock() returns the created and initialized block.

We need to add a few getter and setter methods to Block. These methods will give access to crucial properties of instances of the block. Add the following methods to the Block class:

public static int getColor(byte value) {
for (BlockColor colour : BlockColor.values()) {
if (value == colour.byteValue) {
return colour.rgbValue;
}
}
return -1;
}

public final void setState(int frame, Point position) {
this.frameNumber = frame;
this.position = position;
}

@NonNull
public final byte[][] getShape(int frameNumber) {
return Shape.values()[shapeIndex].getFrame(frameNumber).as2dByteArray();
}

public Point getPosition() {
return this.position;
}

public final int getFrameCount() {
return Shape.values()[shapeIndex].getFrameCount();
}

public int getFrameNumber() {
return frameNumber;
}

public int getColor() {
return color.rgbValue;
}

public byte getStaticValue() {
return color.byteValue;
}

@NonNull is an annotation provided by the Android application framework that denotes that a field, parameter, or method return can never be null. In the preceding code snippet, it is used in the line prior to the getShape() method definition to denote that the method cannot return a null value. 

In Java, an annotation is a form of metadata that can be added to Java source code. Annotations can be used on classes, methods, variables, parameters, and packages. Annotations can also be declared and used in Kotlin.

The @NotNull annotation exists in the android.support.annotation package. Add the package import to the package imports at the top of Block.java:

import android.support.annotation.NonNull;

There's one last thing we should take care of in the Block class before moving on. In the final line of the Block constructor, the position of the current block instance's position instance variable is set as follows:

this.position = new Point(FieldConstants.COLUMN_COUNT.getValue()/2, 0);

The 10 is the column count of the field in which the tetrominoes will be generated. This is a constant value that will be used several times within the code for this application, and as such, is best declared as a constant. Create a package named constants in the base application source package and add a new Kotlin file with the name FieldConstants to the package. Next, add constants for the number of columns and rows that the playing field will possess. The field should possess ten columns and twenty rows:

enum class FieldConstants(val value: Int) {
COLUMN_COUNT(10), ROW_COUNT(20);
}

Import the package with the FieldConstants enum class into Block.java and replace the 10 integer with the constant value of the COLUMN_COUNT:

this.position = new Point( FieldConstants.COLUMN_COUNT.getValue()/2, 0);

That's it! We are done with the programmatic modeling of the Block class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.113.163