Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using Processing IDE in JAVA Following the class example of making a button in Processing, write a Processing program to draw a chessboard/checkers board. These

Using Processing IDE in JAVA

Following the class example of making a button in Processing, write a Processing program to draw a chessboard/checkers board. These boards are typically drawn in black and white, but you can choose any combination of dark and light colors. When you click on a square, the program should draw a colored circle centered in the square you clicked on (choose a different fill color for the circle from what you used for the dark and light squares). Try not to change the buttons array.

Here is the provided code:

Button[] buttons;

void setup(){ buttons = new Button[4]; size(1000,1000); // HINT instead of drawing each button one by one, you can // write a loop and do some math to create your chessboard. // A chessboard has 64 squares - 8 rows and 8 columns buttons[0] = new Button( 200, 200, 200, 100); buttons[1] = new Button( 500, 200, 200, 100); buttons[2] = new Button( 200, 400, 200, 100); buttons[3] = new Button( 500, 400, 200, 100); }

class Button { float leftPos; float topPos; float btnWidth; float btnHeight; boolean on = false; Button(float leftPos, float topPos, float btnWidth, float btnHeight){ this.leftPos = leftPos; this.topPos = topPos; this.btnWidth = btnWidth; this.btnHeight = btnHeight; } boolean isClicked(){ return (mouseX > leftPos && mouseY > topPos && mouseX < leftPos + btnWidth && mouseY < topPos + btnHeight); } void toggleOnOff(){ this.on = !this.on; } void draw(){ // HINT this is how we changed the color of the button. // For the checkers example, you should use this // if statement to draw or not draw a filled in circle // HINT think about how to keep track of the alternating colors // of each square. if(on){ fill(0,0,255); } else { fill(0); } rect(leftPos, topPos, btnWidth, btnHeight); } }

int x = 10; int y = 10;

void mousePressed(){ for(int i = 0; i < 4; i++){ if(buttons[i].isClicked()){ buttons[i].toggleOnOff(); } } }

void draw(){ fill(0); background(255); for(int i = 0; i < 4; i++){ buttons[i].draw(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 19 21 2012 Proceedings Part 3 Lnai 7198

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284922, 978-3642284922

More Books

Students also viewed these Databases questions