@maddoxfenomeno At first I thought this was a "please do my assignment for me" type question, but having looked at your other forums posts I can see you are actually trying to learn Processing.
Here is my offering for the cause ;)
// Building a brick wall using a Brick class
// Niels J-L
final int brickWidth = 54;
final int brickHeight = 20;
final int numOfRows = 10;
final int numOfBricks = 8; // per row
Brick[] bricks = new Brick[(numOfRows * numOfBricks)];
void setup() {
size(800, 800);
// Create Brick objects
for ( int j = 0; j < numOfRows; j++ ) { //Row
for ( int i = 0; i < numOfBricks; i++ ) { // Brick (Column)
int xOffset = 0, idx;
if (j % 2 == 1) xOffset = brickWidth / 2;
bricks[j * numOfBricks + i] = new Brick( i * brickWidth + xOffset, j * brickHeight, brickWidth, brickHeight);
} // end for - i
} // end for - j
}
void draw() {
background(0);
translate(width / 2 - (numOfBricks * brickWidth) / 2, height / 2 - (numOfRows * brickHeight) / 2); // Translate origin (0,0) to middle of render window
stroke(255, 255, 0); // Yellow
strokeWeight(2);
fill(127, 0, 0); // Maroon
// Render brick wall
for ( int i = 0; i < bricks.length; i++ ) {
bricks[i].showBrick();
}
// After several frames random change the color of one brick
if (frameCount % 20 == 0) {
bricks[(int)random(0, bricks.length)].paintBrick( color(random(127,255), random(127,255), random(127,255) ) );
}
}
class Brick {
PVector pos;
PVector size;
color colour;
boolean flagBrickMade = false;
// Default constructor
Brick() {
flagBrickMade = false;
colour = color(127, 0, 0); // Maroon
}
// Constructor with parameters
Brick(int xPos, int yPos, int w, int h) {
makeBrick(xPos, yPos, w, h);
}
void makeBrick(int xPos, int yPos, int w, int h) {
pos = new PVector(xPos, yPos);
size = new PVector(w, h);
colour = color(127, 0, 0); // Maroon
flagBrickMade = true;
}
void showBrick() {
if (flagBrickMade == false) return;
pushMatrix();
fill(colour);
rect(pos.x, pos.y, size.x, size.y);
popMatrix();
}
void paintBrick(color c) {
if (flagBrickMade == false) return;
this.colour = c;
showBrick();
}
}
Even so, posting a full answer when the original poster has shown no effort is a bad idea.
Plus the code you posted is imo terrible. It's easily twice as long as it needs to be and the extra bits add confusion rather that clarity or adaptability.
@maddoxfenomeno Chrisir assessment and overview of the sample sketch are completely correct. It is long, though it is long because I am taking a simple task and demonstrating object-oriented programming (OOP).
I figure that you are learning Processing and it is a good idea to get comfy with OOP. It is one of Processing's core strengths. Doing just procedural programming and not learning to use OOP would be a bit like using a motorbike as a push-bike, sure going down hill is easy, the flat road is a bit hard, but the up-hill part is really arduous and unpleasant.
I'd suggest you also watch some of Daniel Shiffman's coding challenges on his "The Coding Train" on YouTube: https://youtube.com/user/shiffman.
Answers
Post what you have.
Draw a row of rectangles at the top of the screen.
@maddoxfenomeno At first I thought this was a "please do my assignment for me" type question, but having looked at your other forums posts I can see you are actually trying to learn Processing.
Here is my offering for the cause ;)
Even so, posting a full answer when the original poster has shown no effort is a bad idea.
Plus the code you posted is imo terrible. It's easily twice as long as it needs to be and the extra bits add confusion rather that clarity or adaptability.
@Niels what does j means?
j and i are x and y
It‘s like a grid to place the bricks
Actually that code is pretty long.
Niels stored the data in an array of bricks (in
setup()
) and displays it indraw()
.One brick in turn is an object derived from the class named Brick.
See tutorial objects :
https://www.processing.org/tutorials/objects/
The class holds the data and functions for ONE brick.
here is a simple version, just drawing in
draw()
, no storing datathe two
+ 60
just are the distance from the screen border (left side and upper side)xOffset determines whether the brick row is further left or right
Chrisir
@maddoxfenomeno Chrisir assessment and overview of the sample sketch are completely correct. It is long, though it is long because I am taking a simple task and demonstrating object-oriented programming (OOP).
I figure that you are learning Processing and it is a good idea to get comfy with OOP. It is one of Processing's core strengths. Doing just procedural programming and not learning to use OOP would be a bit like using a motorbike as a push-bike, sure going down hill is easy, the flat road is a bit hard, but the up-hill part is really arduous and unpleasant.
I'd suggest you also watch some of Daniel Shiffman's coding challenges on his "The Coding Train" on YouTube: https://youtube.com/user/shiffman.
@maddoxfenomeno Just to add to your learning I came across a free online resource for learning Processing (you can also download it as a PDF): https://legacy.gitbook.com/book/kdoore/cs1335-java-and-processing/details