mirror of
https://github.com/processing/processing4.git
synced 2026-01-29 19:31:16 +01:00
This commit is contained in:
108
java/examples/Topics/GUI/ImageButton/ImageButton.pde
Normal file
108
java/examples/Topics/GUI/ImageButton/ImageButton.pde
Normal file
@@ -0,0 +1,108 @@
|
||||
/**
|
||||
* Image button.
|
||||
*
|
||||
* Loading images and using them to create a button.
|
||||
*/
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
128
java/examples/Topics/GUI/ImageButton/applet/ImageButton.java
Normal file
128
java/examples/Topics/GUI/ImageButton/applet/ImageButton.java
Normal file
@@ -0,0 +1,128 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class ImageButton extends PApplet {
|
||||
|
||||
/**
|
||||
* Image button.
|
||||
*
|
||||
* Loading images and using them to create a button.
|
||||
*/
|
||||
|
||||
|
||||
ImageButtons button;
|
||||
|
||||
public 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);
|
||||
}
|
||||
|
||||
public void draw()
|
||||
{
|
||||
button.update();
|
||||
button.display();
|
||||
}
|
||||
|
||||
class Button
|
||||
{
|
||||
int x, y;
|
||||
int w, h;
|
||||
int basecolor, highlightcolor;
|
||||
int currentcolor;
|
||||
boolean over = false;
|
||||
boolean pressed = false;
|
||||
|
||||
public void pressed() {
|
||||
if(over && mousePressed) {
|
||||
pressed = true;
|
||||
} else {
|
||||
pressed = false;
|
||||
}
|
||||
}
|
||||
|
||||
public 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;
|
||||
}
|
||||
|
||||
public void update()
|
||||
{
|
||||
over();
|
||||
pressed();
|
||||
if(pressed) {
|
||||
currentimage = down;
|
||||
} else if (over){
|
||||
currentimage = roll;
|
||||
} else {
|
||||
currentimage = base;
|
||||
}
|
||||
}
|
||||
|
||||
public void over()
|
||||
{
|
||||
if( overRect(x, y, w, h) ) {
|
||||
over = true;
|
||||
} else {
|
||||
over = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void display()
|
||||
{
|
||||
image(currentimage, x, y);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "ImageButton" });
|
||||
}
|
||||
}
|
||||
108
java/examples/Topics/GUI/ImageButton/applet/ImageButton.pde
Normal file
108
java/examples/Topics/GUI/ImageButton/applet/ImageButton.pde
Normal file
@@ -0,0 +1,108 @@
|
||||
/**
|
||||
* Image button.
|
||||
*
|
||||
* Loading images and using them to create a button.
|
||||
*/
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
BIN
java/examples/Topics/GUI/ImageButton/applet/loading.gif
Normal file
BIN
java/examples/Topics/GUI/ImageButton/applet/loading.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.2 KiB |
BIN
java/examples/Topics/GUI/ImageButton/data/base.gif
Normal file
BIN
java/examples/Topics/GUI/ImageButton/data/base.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 774 B |
BIN
java/examples/Topics/GUI/ImageButton/data/down.gif
Normal file
BIN
java/examples/Topics/GUI/ImageButton/data/down.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 656 B |
BIN
java/examples/Topics/GUI/ImageButton/data/roll.gif
Normal file
BIN
java/examples/Topics/GUI/ImageButton/data/roll.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 774 B |
Reference in New Issue
Block a user