How to perform despeckle operation without using JAI,ImageJ,jhlab libraries? - java

I am making an app in Netbeans using Java Swing. I want to achieve some image processing functionality (like in ImageJ) in my app without using the ImageJ, JAI and jhlab libraries.
For example: ImageJ>>Process>>Noise>>Despeckle.
So, how can I do this?

This might helpful
private void removeNoise() {
int iterator;
for (int row = 0; row < x1; row++) {
for (int column = 0; column < y1; column++) {
if (row == 0 || row == x1 - 1 || column == 0
|| column == y1 - 1) {
result[row][column] = result[row][column];
continue;
} else {
iterator = 0;
for (int r = row - 1; r < row + 2; r++) {
for (int c = column - 1; c < column + 2; c++) {
surround[iterator] = result[r][c];
iterator++;
}
}
result[row][column] =
sortMedian(surround, 9); //calls the sorting method
}
}
}
setPixel();
}
private int sortMedian(int[] surround1, int x) {
int i, j, t = 0;
for (i = 0; i < 9; i++) {
for (j = 1; j < (9 - i); j++) {
if (surround1[j - 1] > surround1[j]) {
t = surround1[j - 1];
surround1[j - 1] = surround1[j];
surround1[j] = t;
}
}
}
return surround1[4];
}
private void setPixel() {
int[] pixel = new int[1];
for (int x = 0; x < bi.getWidth(); x++) {
for (int y = 0; y < bi.getHeight(); y++) {
pixel[0] = (int) result[x][y];
rnImage.getRaster().setPixel(x, y, pixel);
}
}
}

Related

Why is the output always converging to 0.5?

I was trying to solve a XOR problem, but the output always converged to 0.5, so i tried a simpler problem like NOT and the same thing happened.
I really don't know what's going on, i checked the code a million times and everything seems to be right, when i debugged it saving the neural network info I saw that the either the weight values or the biases values were getting really large. To do that I followed the 3 blue 1 brown youtube series about neural network and some other videos, too.
this is my code:
PS: I put the entire code here but I think the main problem is inside the bakpropag function
class NeuralNetwork {
int inNum, hiddenLayersNum, outNum, netSize;
int[] hiddenLayerSize;
Matrix[] weights;
Matrix[] biases;
Matrix[] sums;
Matrix[] activations;
Matrix[] error;
Matrix inputs;
long samples = 0;
float learningRate;
//Constructor------------------------------------------------------------------------------------------------------
NeuralNetwork(int inNum, int hiddenLayersNum, int[] hiddenLayerSize, int outNum, float learningRate) {
this.inNum = inNum;
this.hiddenLayersNum = hiddenLayersNum;
this.hiddenLayerSize = hiddenLayerSize;
this.outNum = outNum;
this.netSize = hiddenLayersNum + 1;
this.learningRate = learningRate;
//output layer plus the hidden layer size
//Note: I'm not adding the input layer because it doesn't have weights
weights = new Matrix[netSize];
//no biases added to the output layer
biases = new Matrix[netSize - 1];
sums = new Matrix[netSize];
activations = new Matrix[netSize];
error = new Matrix[netSize];
initializeHiddenLayer();
initializeOutputLayer();
}
//Initializing Algorithms------------------------------------------------------------------------------------------
void initializeHiddenLayer() {
for (int i = 0; i < hiddenLayersNum; i++) {
if (i == 0) {//only the first hidden layer takes the inputs
weights[i] = new Matrix(hiddenLayerSize[i], inNum);
} else {
weights[i] = new Matrix(hiddenLayerSize[i], hiddenLayerSize[i - 1]);
}
biases[i] = new Matrix(hiddenLayerSize[i], 1);
sums[i] = new Matrix(hiddenLayerSize[i], 1);
activations[i] = new Matrix(hiddenLayerSize[i], 1);
error[i] = new Matrix(hiddenLayerSize[i], 1);
}
}
void initializeOutputLayer() {
//the output layer takes the last hidden layer activation values
weights[netSize - 1] = new Matrix(outNum, hiddenLayerSize[hiddenLayerSize.length - 1]);
activations[netSize - 1] = new Matrix(outNum, 1);
sums[netSize - 1] = new Matrix(outNum, 1);
error[netSize - 1] = new Matrix(outNum, 1);
for (Matrix m : weights) {
for (int i = 0; i < m.i; i++) {
for (int j = 0; j < m.j; j++) {
m.values[i][j] = random(-1, 1);
}
}
}
for (Matrix m : biases) {
for (int i = 0; i < m.i; i++) {
for (int j = 0; j < m.j; j++) {
m.values[i][j] = 1;
}
}
}
for (Matrix m : sums) {
for (int i = 0; i < m.i; i++) {
for (int j = 0; j < m.j; j++) {
m.values[i][j] = 0;
}
}
}
}
//Calculation------------------------------------------------------------------------------------------------------
void calculate(float[] inputs) {
this.inputs = new Matrix(0, 0);
this.inputs = this.inputs.arrayToCollumn(inputs);
sums[0] = (weights[0].matrixMult(this.inputs)).sum(biases[0]);
activations[0] = sigM(sums[0]);
for (int i = 1; i < netSize - 1; i++) {
sums[i] = weights[i].matrixMult(activations[i - 1]);
activations[i] = sigM(sums[i]).sum(biases[i]);
}
//there's no biases in the output layer
//And the output layer uses sigmoid function
sums[netSize - 1] = weights[netSize - 1].matrixMult(activations[netSize - 1 - 1]);
activations[netSize - 1] = sigM(sums[netSize - 1]);
}
//Sending outputs--------------------------------------------------------------------------------------------------
Matrix getOuts() {
return activations[netSize - 1];
}
//Backpropagation--------------------------------------------------------------------------------------------------
void calcError(float[] exp) {
Matrix expected = new Matrix(0, 0);
expected = expected.arrayToCollumn(exp);
//E = (output - expected)
error[netSize - 1] = this.getOuts().diff(expected);
samples++;
}
void backPropag(int layer) {
if (layer == netSize - 1) {
error[layer].scalarDiv(samples);
for (int i = layer - 1; i >= 0; i--) {
prevLayerCost(i);
}
weightError(layer);
backPropag(layer - 1);
} else {
weightError(layer);
biasError(layer);
if (layer != 0)
backPropag(layer - 1);
}
}
void weightError(int layer) {
if (layer != 0) {
for (int i = 0; i < weights[layer].i; i++) {
for (int j = 0; j < weights[layer].j; j++) {
float changeWeight = 0;
if (layer != netSize - 1)
changeWeight = activations[layer - 1].values[j][0] * deriSig(sums[layer].values[i][0]) * error[layer].values[i][0];
else
changeWeight = activations[layer - 1].values[j][0] * deriSig(sums[layer].values[i][0]) * error[layer].values[i][0];
weights[layer].values[i][j] += -learningRate * changeWeight;
}
}
} else {
for (int i = 0; i < weights[layer].i; i++) {
for (int j = 0; j < weights[layer].j; j++) {
float changeWeight = this.inputs.values[j][0] * deriSig(sums[layer].values[i][0]) * error[layer].values[i][0];
weights[layer].values[i][j] += -learningRate * changeWeight;
}
}
}
}
void biasError(int layer) {
for (int i = 0; i < biases[layer].i; i++) {
for (int j = 0; j < biases[layer].j; j++) {
float changeBias = 0;
if (layer != netSize - 1)
changeBias = deriSig(sums[layer].values[i][0]) * error[layer].values[i][0];
biases[layer].values[i][j] += -learningRate * changeBias;
}
}
}
void prevLayerCost(int layer) {
for (int i = 0; i < activations[layer].i; i++) {
for (int j = 0; j < activations[layer + 1].j; j++) {//for all conections of that neuron to the next layer
if (layer != netSize - 1)
error[layer].values[i][0] += weights[layer + 1].values[j][i] * deriSig(sums[layer + 1].values[j][0]) * error[layer + 1].values[j][0];
else
error[layer].values[i][0] += weights[layer + 1].values[j][i] * deriSig(sums[layer + 1].values[j][0]) * error[layer + 1].values[j][0];
}
}
}
//Activation Functions---------------------------------------------------------------------------------------------
Matrix reLUM(Matrix m) {
Matrix temp = m.copyM();
for (int i = 0; i < temp.i; i++) {
for (int j = 0; j < temp.j; j++) {
temp.values[i][j] = ReLU(m.values[i][j]);
}
}
return temp;
}
float ReLU(float x) {
return max(0, x);
}
float deriReLU(float x) {
if (x <= 0)
return 0;
else
return 1;
}
Matrix sigM(Matrix m) {
Matrix temp = m.copyM();
for (int i = 0; i < temp.i; i++) {
for (int j = 0; j < temp.j; j++) {
temp.values[i][j] = sig(m.values[i][j]);
}
}
return temp;
}
float sig(float x) {
return 1 / (1 + exp(-x));
}
float deriSig(float x) {
return sig(x) * (1 - sig(x));
}
//Saving Files-----------------------------------------------------------------------------------------------------
void SaveNeuNet() {
for (int i = 0; i < weights.length; i++) {
weights[i].saveM("weights\\weightLayer" + i);
}
for (int i = 0; i < biases.length; i++) {
biases[i].saveM("biases\\biasLayer" + i);
}
for (int i = 0; i < activations.length; i++) {
activations[i].saveM("activations\\activationLayer" + i);
}
for (int i = 0; i < error.length; i++) {
error[i].saveM("errors\\errorLayer" + i);
}
}
}
and this is the Matrix code:
class Matrix {
int i, j, size;
float[][] values;
Matrix(int i, int j) {
this.i = i;
this.j = j;
this.size = i * j;
values = new float[i][j];
}
Matrix sum (Matrix other) {
if (other.i == this.i && other.j == this.j) {
for (int x = 0; x < this.i; x++) {
for (int z = 0; z < this.j; z++) {
values[x][z] += other.values[x][z];
}
}
return this;
}
return null;
}
Matrix diff(Matrix other) {
if (other.i == this.i && other.j == this.j) {
for (int x = 0; x < this.i; x++) {
for (int z = 0; z < this.j; z++) {
values[x][z] -= other.values[x][z];
}
}
return this;
}
return null;
}
Matrix scalarMult(float k) {
for (int i = 0; i < this.i; i++) {
for (int j = 0; j < this.j; j++) {
values[i][j] *= k;
}
}
return this;
}
Matrix scalarDiv(float k) {
if (k != 0) {
for (int i = 0; i < this.i; i++) {
for (int j = 0; j < this.j; j++) {
values[i][j] /= k;
}
}
return this;
} else
return null;
}
Matrix matrixMult(Matrix other) {
if (this.j != other.i)
return null;
else {
Matrix temp = new Matrix(this.i, other.j);
for (int i = 0; i < temp.i; i++) {
for (int j = 0; j < temp.j; j++) {
for (int k = 0; k < this.j; k++) {
temp.values[i][j] += this.values[i][k] * other.values[k][j];
}
}
}
return temp;
}
}
Matrix squaredValues(){
for (int i = 0; i < this.i; i++){
for (int j = 0; j < this.j; j++){
values[i][j] = sq(values[i][j]);
}
}
return this;
}
void printM() {
for (int x = 0; x < this.i; x++) {
print("| ");
for (int z = 0; z < this.j; z++) {
print(values[x][z] + " | ");
}
println();
}
}
void saveM(String name) {
String out = "";
for (int x = 0; x < this.i; x++) {
out += "| ";
for (int z = 0; z < this.j; z++) {
out += values[x][z] + " | ";
}
out += "\n";
}
saveStrings("outputs\\" + name + ".txt", new String[] {out});
}
Matrix arrayToCollumn(float[] array) {
Matrix temp = new Matrix(array.length, 1);
for (int i = 0; i < array.length; i++)
temp.values[i][0] = array[i];
return temp;
}
Matrix arrayToLine(float[] array) {
Matrix temp = new Matrix(1, array.length);
for (int j = 0; j < array.length; j++)
temp.values[0][j] = array[j];
return temp;
}
Matrix copyM(){
Matrix temp = new Matrix(i, j);
for (int i = 0; i < this.i; i++){
for (int j = 0; j < this.j; j++){
temp.values[i][j] = this.values[i][j];
}
}
return temp;
}
}
As I said, the outputs are always converging to 0.5 instead of the actual value 1 or 0
I rewrote the code and it is working now! I have no idea what was wrong with the code before but this one works:
class NeuralNetwork {
int netSize;
float learningRate;
Matrix[] weights;
Matrix[] biases;
Matrix[] activations;
Matrix[] sums;
Matrix[] errors;
NeuralNetwork(int inNum, int hiddenNum, int[] hiddenLayerSize, int outNum, float learningRate) {
netSize = hiddenNum + 1;
this.learningRate = learningRate;
weights = new Matrix[netSize];
biases = new Matrix[netSize - 1];
activations = new Matrix[netSize];
sums = new Matrix[netSize];
errors = new Matrix[netSize];
initializeMatrices(inNum, hiddenNum, hiddenLayerSize, outNum);
}
//INITIALIZING MATRICES
void initializeMatrices(int inNum, int hiddenNum, int[] layerSize, int outNum) {
for (int i = 0; i < hiddenNum; i++) {
if (i == 0)
weights[i] = new Matrix(layerSize[0], inNum);
else
weights[i] = new Matrix(layerSize[i], layerSize[i - 1]);
biases[i] = new Matrix(layerSize[i], 1);
activations[i] = new Matrix(layerSize[i], 1);
errors[i] = new Matrix(layerSize[i], 1);
sums[i] = new Matrix(layerSize[i], 1);
weights[i].randomize(-1, 1);
biases[i].randomize(-1, 1);
activations[i].randomize(-1, 1);
}
weights[netSize - 1] = new Matrix(outNum, layerSize[layerSize.length - 1]);
activations[netSize - 1] = new Matrix(outNum, 1);
errors[netSize - 1] = new Matrix(outNum, 1);
sums[netSize - 1] = new Matrix(outNum, 1);
weights[netSize - 1].randomize(-1, 1);
activations[netSize - 1].randomize(-1, 1);
}
//---------------------------------------------------------------------------------------------------------------
void forwardPropag(float[] ins) {
Matrix inputs = new Matrix(0, 0);
inputs = inputs.arrayToCollumn(ins);
sums[0] = (weights[0].matrixMult(inputs)).sum(biases[0]);
activations[0] = sigM(sums[0]);
for (int i = 1; i < netSize - 1; i++) {
sums[i] = (weights[i].matrixMult(activations[i - 1])).sum(biases[i]);
activations[i] = sigM(sums[i]);
}
//output layer does not have biases
sums[netSize - 1] = weights[netSize - 1].matrixMult(activations[netSize - 2]);
activations[netSize - 1] = sigM(sums[netSize - 1]);
}
Matrix predict(float[] inputs) {
forwardPropag(inputs);
return activations[netSize - 1].copyM();
}
//SUPERVISED LEARNING - BACKPROPAGATION
void train(float[] inps, float[] expec) {
Matrix expected = new Matrix(0, 0);
expected = expected.arrayToCollumn(expec);
errors[netSize - 1] = predict(inps).diff(expected);
calcErorrPrevLayers();
adjustWeights(inps);
adjustBiases();
for (Matrix m : errors){
m.reset();
}
}
void calcErorrPrevLayers() {
for (int l = netSize - 2; l >= 0; l--) {
for (int i = 0; i < activations[l].i; i++) {
for (int j = 0; j < activations[l + 1].i; j++) {
errors[l].values[i][0] += weights[l + 1].values[j][i] * dSig(sums[l + 1].values[j][0]) * errors[l + 1].values[j][0];
}
}
}
}
void adjustWeights(float[] inputs) {
for (int l = 0; l < netSize; l++) {
if (l == 0) {
//for ervery neuron n in the first layer
for (int n = 0; n < activations[l].i; n++) {
//for every weight w of the first layer
for (int w = 0; w < inputs.length; w++) {
float weightChange = inputs[w] * dSig(sums[l].values[n][0]) * errors[l].values[n][0];
weights[l].values[n][w] += -learningRate * weightChange;
}
}
} else {
//for ervery neuron n in the first layer
for (int n = 0; n < activations[l].i; n++) {
//for every weight w of the first layer
for (int w = 0; w < activations[l - 1].i; w++) {
float weightChange = activations[l - 1].values[w][0] * dSig(sums[l].values[n][0]) * errors[l].values[n][0];
weights[l].values[n][w] += -learningRate * weightChange;
}
}
}
}
}
void adjustBiases() {
for (int l = 0; l < netSize - 1; l++) {
//for ervery neuron n in the first layer
for (int n = 0; n < activations[l].i; n++) {
float biasChange = dSig(sums[l].values[n][0]) * errors[l].values[n][0];
biases[l].values[n][0] += -learningRate * biasChange;
}
}
}
//ACTIVATION FUNCTION
float sig(float x) {
return 1 / (1 + exp(-x));
}
float dSig(float x) {
return sig(x) * (1 - sig(x));
}
Matrix sigM(Matrix m) {
Matrix temp = m.copyM();
for (int i = 0; i < m.i; i++) {
for (int j = 0; j < m.j; j++) {
temp.values[i][j] = sig(m.values[i][j]);
}
}
return temp;
}
}

Java Minesweeper - ArrayIndexOutOfBounds Exception

I am new to Java programming and would like to seek your help.
I'm trying to develop a simple minesweeper game using Java. However, I keep getting the error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at practice.week.pkg4.PracticeWeek4.main(PracticeWeek4.java:55)"
This occurs when I'm trying to place digits around the square which has a bomb. I understand that perhaps the 1 has went out of the array, causing the exception to occur. However, I'm not sure how to go about catching the error. Would appreciate any kind help.
Eg: Sample Output
1 1 1
1 B 1
1 1 1
Here is my code snippet:
public static void main(String[] args) {
// TODO code application logic here
int rows = 9;
int cols = 9;
char[][] map = new char[rows][cols];
int count = 0;
for(int i = 0; i<map.length; i++)
{
for(int j = 0; j<map[i].length; j++)
{
map[i][j] = '.';
}
}
Random rnd = new Random();
do
{
int x = rnd.nextInt(rows);
int y = rnd.nextInt(cols);
for(int i = 0; i<map.length; i++)
{
for(int j = 0; j<map[i].length; j++)
{
if(map[x][y] != 'B' && x > 0 & y > 0)
{
map[x][y] = 'B';
map[x-1][y-1] = '1';
map[x-1][y] = '1';
map[x-1][y+1] = '1';
map[x][y-1] = '1';
map[x][y+1] = '1';
map[x+1][y-1] = '1';
map[x+1][y] = '1';
map[x+1][y+1] = '1';
count++;
}
}
}
}
while(count < 10);
for(int x = 0; x<map.length; x++)
{
for(int y = 0; y <map[x].length; y++)
{
}
}
for(int x = 0; x<map.length; x++)
{
for(int y = 0; y<map[x].length; y++)
{
System.out.print(map[x][y] + " ");
}
System.out.println("");
}
}
The do-while loop for setting the mines is on the right track, but the way you are updating the counts for surrounding blocks is causing the IndexOutOfBoundsException. And these two loops
for(int i = 0; i < map.length; i++)
{
for(int j = 0; j < map[i].length; j++)
serve no purpose. You need to rearrange it to handle multiple mines, etc, so why not set all the mines first:
do
{
int x = rnd.nextInt(rows);
int y = rnd.nextInt(cols);
if (map[x][y] != 'B')
{
map[x][y] = 'B';
count++;
}
} while(count < 10);
Then go through the map, and count the number of mines surrounding each block:
for (int x = 0; x < map.length; x++)
{
for (int y = 0; y < map[x].length; y++)
{
if (map[x][y] == 'B')
continue;
// Count the number of mines around map[x][y]
int mines = 0;
for (int xOffset = -1; xOffset <= 1; xOffset++)
{
// This is an important step - without it, we will access elements off the edge of the map
if (x + xOffset < 0 || x + xOffset >= map.length)
continue;
for (int yOffset = -1; yOffset <= 1; yOffset++)
{
// Another check for the edge of the map
if (y + yOffset < 0 || y + yOffset >= map[x].length)
continue;
if (map[x + xOffset][y + yOffset] == 'B')
mines++;
}
}
map[x][y] = "012345678".charAt(mines); // Get the number as a character
}
}

searching same items in row in array

i am trying to make a game like a candy crush or bejeweled, but i had a problem with find a same items in row.
i have this
public void mapfinder() {
int typ = -1;
int pocet = 0;
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
if (plocha[x][y].getgem() == typ) {
pocet++;
} else if (pocet > 2) {
while (pocet > 0) {
pocet--;
plocha[x][y - pocet] = new Actor(x, y, nogem);
}
} else {
typ = plocha[x][y].getgem();
}
}
}
}
acctualy, its slow and i often get outOfArrayExeption
any ideas?

How to break a square matrix into square submatrices?

I am trying to create a program that returns the maximum square submatrix of 1's from a square matrix of 0's and 1's. Right now I have figured out how to break the square up into a square submatrix starting at each number that equals 1. The problem is, as the program starts to get farther from the starting point of the matrix, it suddenly goes out of bounds, which I am suspecting has to do with how it calculates what part of the matrix to start from for each submatrix.
Here is my code:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of rows and columns in the matrix (only one input, this is a square matrix): ");
int dimensions = input.nextInt();
int[][] matrix = new int[dimensions][dimensions];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
int n = input.nextInt();
if (n == 0 || n == 1)
matrix[i][j] = n;
else
System.out.print("Input only 0 or 1");
}
}
int[] largestBlock = findLargestBlock(matrix);
}
public static int[] findLargestBlock(int[][] m) {
int[] solution = new int[3];
//find rows with most consecutive 1's, then find columns with the same # of consecutive 1's
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[i].length; j++) {
//"origin" for each iteration is (i, j)
if (m[i][j] == 1)
if (isSquare(m, i, j) == true) {
solution[0] = i; solution[1] = j; solution[2] = getSize(m, i, j);
}
}
}
return solution;
}
public static boolean isSquare(int[][] m, int i, int j) {
int k = m.length - i;
if (m[0].length - j < k)
k = m.length - j;
if (k < 2)
return false;
int[][] testSquare = new int[k][k];
for (int y = i; y < m.length - i; y++) {
for (int x = j; x < m[i].length - j; x++) {
testSquare[y - i][x - j] = m[y][x];
}
}
for (int y = 0; y < testSquare.length; y++) {
for (int x = 1; x < testSquare[y].length; x++) {
if (testSquare[y][x] != testSquare[y][x - 1])
return false;
}
}
for (int x = 0; x < testSquare[0].length; x++) {
for (int y = 1; y < testSquare.length; y++) {
if (testSquare[y][x] != testSquare[y - 1][x])
return false;
}
}
return true;
}
public static int getSize(int[][] m, int i, int j) {
int k = m.length - i;
if (m[0].length - j < k)
k = m.length - j;
return k;
}
I determined that this part of the program was causing the issue, apparently there is some flaw in it that sends the array x- or y- value out of bounds:
public static boolean isSquare(int[][] m, int i, int j) {
int k = m.length - i;
if (m[0].length - j < k)
k = m.length - j;
if (k < 2)
return false;
int[][] testSquare = new int[k][k];
for (int y = i; y < m.length - i; y++) {
for (int x = j; x < m[i].length - j; x++) {
**testSquare[y - i][x - j] = m[y][x];**
}
}
I'm very confused regarding the line in stars/in bold font, as I think this is the line causing the issue. However, I'm not sure how its causing the issue.
I think the loop you are looking for is this - since testSquare is square just start from it make sure its enumerated from 0 to k then find the other matrix indexes - m will never go more than k since k is the minimum so it starts from i and j and goes to i+k and j+k max.
if (m[i].length - j < k)
k = m[i].length - j;
for (int y = 0; y < k; y++) {
for (int x = 0; x < k; x++) {
testSquare[y][x] = m[i+y][j+x];
}
}

Printing a shape based on inputs from the user

I am trying to print a shape based on input; the shape is an "x". The inputs must be positive odd ints, and an arbitrary brush character. I have the code completed for the user input, but I need help with the code that actually prints the shape. Here is what I have so far:
public class TestProgram {
public static void main(String[] args) {
int height = 5;//Any positive odd int but 5 does not work correctly. Not sure what is going on.
char brush = '*';
for (int row = 0; row < height/2; row++) {
for (int i = row; i > 0; i--) {
System.out.print(" ");
}
System.out.print(brush);
for (int i = (height/2); i >= 2*row; i--) {
System.out.print(" ");
}
System.out.print(brush);
System.out.print("\n");
}
for (int row = 1; row < (height/2)+1; row++ ) {
System.out.print(" ");
}
System.out.print(brush);
System.out.print("\n");
for (int row = (height/2)-1; row >= 0; row--) {
for (int i = row; i > 0; i--) {
System.out.print(" ");
}
System.out.print(brush);
for (int i = (height/2); i >= 2*row; i--) {
System.out.print(" ");
}
System.out.print(brush);
System.out.print("\n");
}
for (int row = 1; row < (height/2)+1; row++ ) {
System.out.print(" ");
}
}
}
I would start with a routine to repeat your char n times. Something like
private static String repeat(char ch, int count) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < count; i++) {
sb.append(ch);
}
return sb.toString();
}
Then, I would prefer to build the String with a StringBuilder and the repeat routine. Like
int height = 5;
char brush = '*';
char space = ' ';
int half = height / 2;
StringBuilder sb = new StringBuilder();
for (int row = 0; row < half; row++) {
int cols = height - ((row + 1) / 2);
sb.append(repeat(space, row)).append(brush);
sb.append(repeat(space, cols)).append(brush);
sb.append(System.lineSeparator());
}
sb.append(repeat(space, half)).append(brush);
sb.append(System.lineSeparator());
for (int row = half - 1; row >= 0; row--) {
int cols = height - ((row + 1) / 2);
sb.append(repeat(space, row)).append(brush);
sb.append(repeat(space, cols)).append(brush);
sb.append(System.lineSeparator());
}
System.out.println(sb.toString());
Since you haven't learned about buffered io yet, that could also be expressed as
int height = 5;
char brush = '*';
char space = ' ';
int half = height / 2;
for (int row = 0; row < half; row++) {
int cols = height - ((row + 1) * 2);
System.out.print(repeat(space, row));
System.out.print(brush);
System.out.print(repeat(space, cols));
System.out.println(brush);
}
System.out.print(repeat(space, height / 2));
System.out.println(brush);
for (int row = (height / 2) - 1; row >= 0; row--) {
int cols = height - ((row + 1) * 2);
System.out.print(repeat(space, row));
System.out.print(brush);
System.out.print(repeat(space, cols));
System.out.println(brush);
}
And if you really don't know how to create a method,
int height = 5;
char brush = '*';
char space = ' ';
int half = height / 2;
for (int row = 0; row < half; row++) {
int cols = height - ((row + 1) * 2);
for (int t = 0; t < row; t++) {
System.out.print(space);
}
System.out.print(brush);
for (int t = 0; t < cols; t++) {
System.out.print(space);
}
System.out.println(brush);
}
for (int t = 0; t < height / 2; t++) {
System.out.print(space);
}
System.out.println(brush);
for (int row = (height / 2) - 1; row >= 0; row--) {
int cols = height - ((row + 1) * 2);
for (int t = 0; t < row; t++) {
System.out.print(space);
}
System.out.print(brush);
for (int t = 0; t < cols; t++) {
System.out.print(space);
}
System.out.println(brush);
}

Categories