topical media & game development
#javascript-processing-example-topic-gui-imagebutton.htm / htm
<!DOCTYPE html>
<html><head>
<script src="javascript-processing-example-processing.js"></script>
<script src="javascript-processing-example-init.js"></script>
<link rel="stylesheet" href="javascript-processing-example-style.css">
</head><body><h1><a href="http://ejohn.org/blog/processingjs/">Processing.js</a></h1>
<h2>ImageButton</h2>
<p>Loading images and using them to create a button.</p>
<p><a href="http://processing.org/learning/topics/imagebutton.html"><b>Original Processing.org Example:</b> ImageButton</a><br>
<script type="application/processing">
ImageButtons button;
void setup()
{
size(200, 200);
background(102, 102, 102);
// Define and create image button
PImage b = loadImage("base.gif");
PImage r = loadImage("roll.gif");
PImage d = loadImage("down.gif");
int x = width/2 - b.width/2;
int y = height/2 - b.height/2;
int w = b.width;
int h = b.height;
button = new ImageButtons(x, y, w, h, b, r, d);
}
void draw()
{
button.update();
button.display();
}
class Button
{
int x, y;
int w, h;
color basecolor, highlightcolor;
color currentcolor;
boolean isOver = false;
boolean isPressed = false;
void pressed() {
if(isOver && mousePressed) {
isPressed = true;
} else {
isPressed = false;
}
}
boolean overRect(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}
}
class ImageButtons extends Button
{
PImage base;
PImage roll;
PImage down;
PImage currentimage;
ImageButtons(int ix, int iy, int iw, int ih, PImage ibase, PImage iroll, PImage idown)
{
x = ix;
y = iy;
w = iw;
h = ih;
base = ibase;
roll = iroll;
down = idown;
currentimage = base;
}
void update()
{
over();
pressed();
if(isPressed) {
currentimage = down;
} else if (over){
currentimage = roll;
} else {
currentimage = base;
}
}
void over()
{
if( overRect(x, y, w, h) ) {
isOver = true;
} else {
isOver = false;
}
}
void display()
{
image(currentimage, x, y);
}
}
</script><canvas width="200" height="200"></canvas></p>
<div style="overflow: hidden; height: 0px; width: 0px;"><img src="javascript-processing-example-base.gif" id="base.gif"><img src="javascript-processing-example-down.gif" id="down.gif"><img src="javascript-processing-example-roll.gif" id="roll.gif"></div>
<pre><b>// All Examples Written by <a href="http://reas.com/">Casey Reas</a> and <a href="http://benfry.com/">Ben Fry</a>
// unless otherwise stated.</b>
ImageButtons button;
void setup()
{
size(200, 200);
background(102, 102, 102);
// Define and create image button
PImage b = loadImage("base.gif");
PImage r = loadImage("roll.gif");
PImage d = loadImage("down.gif");
int x = width/2 - b.width/2;
int y = height/2 - b.height/2;
int w = b.width;
int h = b.height;
button = new ImageButtons(x, y, w, h, b, r, d);
}
void draw()
{
button.update();
button.display();
}
class Button
{
int x, y;
int w, h;
color basecolor, highlightcolor;
color currentcolor;
boolean over = false;
boolean pressed = false;
void pressed() {
if(over && mousePressed) {
pressed = true;
} else {
pressed = false;
}
}
boolean overRect(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}
}
class ImageButtons extends Button
{
PImage base;
PImage roll;
PImage down;
PImage currentimage;
ImageButtons(int ix, int iy, int iw, int ih, PImage ibase, PImage iroll, PImage idown)
{
x = ix;
y = iy;
w = iw;
h = ih;
base = ibase;
roll = iroll;
down = idown;
currentimage = base;
}
void update()
{
over();
pressed();
if(pressed) {
currentimage = down;
} else if (over){
currentimage = roll;
} else {
currentimage = base;
}
}
void over()
{
if( overRect(x, y, w, h) ) {
over = true;
} else {
over = false;
}
}
void display()
{
image(currentimage, x, y);
}
}</pre>
</body></html>
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.