class and object
in
Programming Questions
•
2 years ago
Hi,
I am new to processing and trying to change my code with define a class and use an object from that class.
I was following all the steps from the book"Learning Processing" but still having problems with it. Here is my code, I would really appreciate your help. Thank you.
class TV {
// declare all global variables
boolean button = false; //the Breaking Bad logo is set
PImage img; //declares PImage variable img - screen image / Breaking Bad logo
PImage img2; //declares PImage variable img2 - wall image
PFont font; //declares font
int a = 155;
int b = 125;
//constructore gets called once in the begining when a new TV is made
TV () {
}
// a function to draw a table
void drawTable() {
// Table
strokeWeight(1); //set the stroke weight
beginShape(); //begin the shape
fill(#F0D8C6); //color of the table
vertex(0,500);
vertex(260,330);
vertex(700,330);
vertex(700,500);
endShape(); //end the shape
}
// a function to draw a TV
void drawTV() {
//TV body
strokeWeight(1); //set the stroke weight to 1
fill(50); //color of the round stand
ellipse(345,394,125,50); //round stand
strokeWeight(1);
fill(0); //color of the stand
rect(295,358,100,50); //stand
//TV screen
strokeWeight(26); //set the stroke weight
strokeJoin(ROUND); //round the stroke corners
rect(150,125,400,225); //screen
//Blue Button
strokeWeight(1); //set stroke weight to 1
fill(#4A65E5); //buttons color - blue
rect(530,352,15,10); //blue button
//message on the screen
textSize(20); //text size
text ("Press the blue button! This show is not good for you!", 240,470); //this message will appear on the screen x=240, y=470
fill(0); //color black
}
void fuzzyPicture() { //a function to fuzzy picture
// If the mouse is pressed the fuzzy picture will appear
if (button) {
//nested FOR loop block
for (int a = 155; a < 542; a = a+10) {
for (int b = 125; b < 342; b = b+10) {
fill(random(255), random(255), random(255)); //sets random RGB colors
rect(a,b,10,10); //draws 10x10 rectangle based on random x,y, coordinates declared above
}
}
}else{ //if the button is not pressed draw the Breaking Bad logo
//draw the Breaking Bad logo
image(img,150,125,400,225); //set the image
}
}
// mouse pressed
void mousePressed() { //defines input if mouse pressed
// if the blue button is pressed the fuzzy screen will appear
if (mouseX > 530 && mouseX < 545 && mouseY > 348 && mouseY < 360) {
button = !button;
}
}
TV tvBB;
void setup() {
size(700,500); //set screen size
smooth(); //smooth the edges
frameRate(10); //frame rate 10
tvBB = new TV() ;
//messages and instructions // will print inside the message board
println ("Season Finale of Breaking Bad");
println ("-- Press the blue button");
println ("-- Press the blue button again to return to Breaking Bad logo");
font = loadFont ("ArialNarrow-15.vlw"); //load the font
textFont(font);
//images included
img = loadImage("breaking-bad-logo.png"); //loads JPEG of Breaking Bad logo
img2 = loadImage("red_wallpaper.jpg"); //loads JPEG of red wallpaper
}
void draw() {
background(img2); //set background
tvBB.drawTable(); //draw a table
tvBB.drawTV(); //draw a TV
tvBB.fuzzyPicture(); //draw a fuzzy picture
}
I am new to processing and trying to change my code with define a class and use an object from that class.
I was following all the steps from the book"Learning Processing" but still having problems with it. Here is my code, I would really appreciate your help. Thank you.
class TV {
// declare all global variables
boolean button = false; //the Breaking Bad logo is set
PImage img; //declares PImage variable img - screen image / Breaking Bad logo
PImage img2; //declares PImage variable img2 - wall image
PFont font; //declares font
int a = 155;
int b = 125;
//constructore gets called once in the begining when a new TV is made
TV () {
}
// a function to draw a table
void drawTable() {
// Table
strokeWeight(1); //set the stroke weight
beginShape(); //begin the shape
fill(#F0D8C6); //color of the table
vertex(0,500);
vertex(260,330);
vertex(700,330);
vertex(700,500);
endShape(); //end the shape
}
// a function to draw a TV
void drawTV() {
//TV body
strokeWeight(1); //set the stroke weight to 1
fill(50); //color of the round stand
ellipse(345,394,125,50); //round stand
strokeWeight(1);
fill(0); //color of the stand
rect(295,358,100,50); //stand
//TV screen
strokeWeight(26); //set the stroke weight
strokeJoin(ROUND); //round the stroke corners
rect(150,125,400,225); //screen
//Blue Button
strokeWeight(1); //set stroke weight to 1
fill(#4A65E5); //buttons color - blue
rect(530,352,15,10); //blue button
//message on the screen
textSize(20); //text size
text ("Press the blue button! This show is not good for you!", 240,470); //this message will appear on the screen x=240, y=470
fill(0); //color black
}
void fuzzyPicture() { //a function to fuzzy picture
// If the mouse is pressed the fuzzy picture will appear
if (button) {
//nested FOR loop block
for (int a = 155; a < 542; a = a+10) {
for (int b = 125; b < 342; b = b+10) {
fill(random(255), random(255), random(255)); //sets random RGB colors
rect(a,b,10,10); //draws 10x10 rectangle based on random x,y, coordinates declared above
}
}
}else{ //if the button is not pressed draw the Breaking Bad logo
//draw the Breaking Bad logo
image(img,150,125,400,225); //set the image
}
}
// mouse pressed
void mousePressed() { //defines input if mouse pressed
// if the blue button is pressed the fuzzy screen will appear
if (mouseX > 530 && mouseX < 545 && mouseY > 348 && mouseY < 360) {
button = !button;
}
}
TV tvBB;
void setup() {
size(700,500); //set screen size
smooth(); //smooth the edges
frameRate(10); //frame rate 10
tvBB = new TV() ;
//messages and instructions // will print inside the message board
println ("Season Finale of Breaking Bad");
println ("-- Press the blue button");
println ("-- Press the blue button again to return to Breaking Bad logo");
font = loadFont ("ArialNarrow-15.vlw"); //load the font
textFont(font);
//images included
img = loadImage("breaking-bad-logo.png"); //loads JPEG of Breaking Bad logo
img2 = loadImage("red_wallpaper.jpg"); //loads JPEG of red wallpaper
}
void draw() {
background(img2); //set background
tvBB.drawTable(); //draw a table
tvBB.drawTV(); //draw a TV
tvBB.fuzzyPicture(); //draw a fuzzy picture
}
1