mirror of
https://github.com/processing/processing4.git
synced 2026-01-30 11:51:54 +01:00
This commit is contained in:
100
java/examples/Topics/GUI/Button/Button.pde
Normal file
100
java/examples/Topics/GUI/Button/Button.pde
Normal file
@@ -0,0 +1,100 @@
|
||||
/**
|
||||
* Button.
|
||||
*
|
||||
* Click on one of the colored squares in the
|
||||
* center of the image to change the color of
|
||||
* the background.
|
||||
*/
|
||||
|
||||
int rectX, rectY; // Position of square button
|
||||
int circleX, circleY; // Position of circle button
|
||||
int rectSize = 50; // Diameter of rect
|
||||
int circleSize = 53; // Diameter of circle
|
||||
color rectColor, circleColor, baseColor;
|
||||
color rectHighlight, circleHighlight;
|
||||
color currentColor;
|
||||
boolean rectOver = false;
|
||||
boolean circleOver = false;
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
smooth();
|
||||
rectColor = color(0);
|
||||
rectHighlight = color(51);
|
||||
circleColor = color(255);
|
||||
circleHighlight = color(204);
|
||||
baseColor = color(102);
|
||||
currentColor = baseColor;
|
||||
circleX = width/2+circleSize/2+10;
|
||||
circleY = height/2;
|
||||
rectX = width/2-rectSize-10;
|
||||
rectY = height/2-rectSize/2;
|
||||
ellipseMode(CENTER);
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
update(mouseX, mouseY);
|
||||
background(currentColor);
|
||||
|
||||
if(rectOver) {
|
||||
fill(rectHighlight);
|
||||
} else {
|
||||
fill(rectColor);
|
||||
}
|
||||
stroke(255);
|
||||
rect(rectX, rectY, rectSize, rectSize);
|
||||
|
||||
if(circleOver) {
|
||||
fill(circleHighlight);
|
||||
} else {
|
||||
fill(circleColor);
|
||||
}
|
||||
stroke(0);
|
||||
ellipse(circleX, circleY, circleSize, circleSize);
|
||||
}
|
||||
|
||||
void update(int x, int y)
|
||||
{
|
||||
if( overCircle(circleX, circleY, circleSize) ) {
|
||||
circleOver = true;
|
||||
rectOver = false;
|
||||
} else if ( overRect(rectX, rectY, rectSize, rectSize) ) {
|
||||
rectOver = true;
|
||||
circleOver = false;
|
||||
} else {
|
||||
circleOver = rectOver = false;
|
||||
}
|
||||
}
|
||||
|
||||
void mousePressed()
|
||||
{
|
||||
if(circleOver) {
|
||||
currentColor = circleColor;
|
||||
}
|
||||
if(rectOver) {
|
||||
currentColor = rectColor;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
boolean overCircle(int x, int y, int diameter)
|
||||
{
|
||||
float disX = x - mouseX;
|
||||
float disY = y - mouseY;
|
||||
if(sqrt(sq(disX) + sq(disY)) < diameter/2 ) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
120
java/examples/Topics/GUI/Button/applet/Button.java
Normal file
120
java/examples/Topics/GUI/Button/applet/Button.java
Normal file
@@ -0,0 +1,120 @@
|
||||
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 Button extends PApplet {
|
||||
|
||||
/**
|
||||
* Button.
|
||||
*
|
||||
* Click on one of the colored squares in the
|
||||
* center of the image to change the color of
|
||||
* the background.
|
||||
*/
|
||||
|
||||
int rectX, rectY; // Position of square button
|
||||
int circleX, circleY; // Position of circle button
|
||||
int rectSize = 50; // Diameter of rect
|
||||
int circleSize = 53; // Diameter of circle
|
||||
int rectColor, circleColor, baseColor;
|
||||
int rectHighlight, circleHighlight;
|
||||
int currentColor;
|
||||
boolean rectOver = false;
|
||||
boolean circleOver = false;
|
||||
|
||||
public void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
smooth();
|
||||
rectColor = color(0);
|
||||
rectHighlight = color(51);
|
||||
circleColor = color(255);
|
||||
circleHighlight = color(204);
|
||||
baseColor = color(102);
|
||||
currentColor = baseColor;
|
||||
circleX = width/2+circleSize/2+10;
|
||||
circleY = height/2;
|
||||
rectX = width/2-rectSize-10;
|
||||
rectY = height/2-rectSize/2;
|
||||
ellipseMode(CENTER);
|
||||
}
|
||||
|
||||
public void draw()
|
||||
{
|
||||
update(mouseX, mouseY);
|
||||
background(currentColor);
|
||||
|
||||
if(rectOver) {
|
||||
fill(rectHighlight);
|
||||
} else {
|
||||
fill(rectColor);
|
||||
}
|
||||
stroke(255);
|
||||
rect(rectX, rectY, rectSize, rectSize);
|
||||
|
||||
if(circleOver) {
|
||||
fill(circleHighlight);
|
||||
} else {
|
||||
fill(circleColor);
|
||||
}
|
||||
stroke(0);
|
||||
ellipse(circleX, circleY, circleSize, circleSize);
|
||||
}
|
||||
|
||||
public void update(int x, int y)
|
||||
{
|
||||
if( overCircle(circleX, circleY, circleSize) ) {
|
||||
circleOver = true;
|
||||
rectOver = false;
|
||||
} else if ( overRect(rectX, rectY, rectSize, rectSize) ) {
|
||||
rectOver = true;
|
||||
circleOver = false;
|
||||
} else {
|
||||
circleOver = rectOver = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void mousePressed()
|
||||
{
|
||||
if(circleOver) {
|
||||
currentColor = circleColor;
|
||||
}
|
||||
if(rectOver) {
|
||||
currentColor = rectColor;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean overCircle(int x, int y, int diameter)
|
||||
{
|
||||
float disX = x - mouseX;
|
||||
float disY = y - mouseY;
|
||||
if(sqrt(sq(disX) + sq(disY)) < diameter/2 ) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "Button" });
|
||||
}
|
||||
}
|
||||
100
java/examples/Topics/GUI/Button/applet/Button.pde
Normal file
100
java/examples/Topics/GUI/Button/applet/Button.pde
Normal file
@@ -0,0 +1,100 @@
|
||||
/**
|
||||
* Button.
|
||||
*
|
||||
* Click on one of the colored squares in the
|
||||
* center of the image to change the color of
|
||||
* the background.
|
||||
*/
|
||||
|
||||
int rectX, rectY; // Position of square button
|
||||
int circleX, circleY; // Position of circle button
|
||||
int rectSize = 50; // Diameter of rect
|
||||
int circleSize = 53; // Diameter of circle
|
||||
color rectColor, circleColor, baseColor;
|
||||
color rectHighlight, circleHighlight;
|
||||
color currentColor;
|
||||
boolean rectOver = false;
|
||||
boolean circleOver = false;
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
smooth();
|
||||
rectColor = color(0);
|
||||
rectHighlight = color(51);
|
||||
circleColor = color(255);
|
||||
circleHighlight = color(204);
|
||||
baseColor = color(102);
|
||||
currentColor = baseColor;
|
||||
circleX = width/2+circleSize/2+10;
|
||||
circleY = height/2;
|
||||
rectX = width/2-rectSize-10;
|
||||
rectY = height/2-rectSize/2;
|
||||
ellipseMode(CENTER);
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
update(mouseX, mouseY);
|
||||
background(currentColor);
|
||||
|
||||
if(rectOver) {
|
||||
fill(rectHighlight);
|
||||
} else {
|
||||
fill(rectColor);
|
||||
}
|
||||
stroke(255);
|
||||
rect(rectX, rectY, rectSize, rectSize);
|
||||
|
||||
if(circleOver) {
|
||||
fill(circleHighlight);
|
||||
} else {
|
||||
fill(circleColor);
|
||||
}
|
||||
stroke(0);
|
||||
ellipse(circleX, circleY, circleSize, circleSize);
|
||||
}
|
||||
|
||||
void update(int x, int y)
|
||||
{
|
||||
if( overCircle(circleX, circleY, circleSize) ) {
|
||||
circleOver = true;
|
||||
rectOver = false;
|
||||
} else if ( overRect(rectX, rectY, rectSize, rectSize) ) {
|
||||
rectOver = true;
|
||||
circleOver = false;
|
||||
} else {
|
||||
circleOver = rectOver = false;
|
||||
}
|
||||
}
|
||||
|
||||
void mousePressed()
|
||||
{
|
||||
if(circleOver) {
|
||||
currentColor = circleColor;
|
||||
}
|
||||
if(rectOver) {
|
||||
currentColor = rectColor;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
boolean overCircle(int x, int y, int diameter)
|
||||
{
|
||||
float disX = x - mouseX;
|
||||
float disY = y - mouseY;
|
||||
if(sqrt(sq(disX) + sq(disY)) < diameter/2 ) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
BIN
java/examples/Topics/GUI/Button/applet/loading.gif
Normal file
BIN
java/examples/Topics/GUI/Button/applet/loading.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.2 KiB |
Reference in New Issue
Block a user