Processing9 GUI
/*--GUI--*/
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;
color circleColor;
color baseColor;
boolean rectOver = false;
boolean circleOver = false;
void setup()
{
void draw()
{
void update(int x, int y)
{
boolean overRect(int x, int y, int width, int height)
{
boolean overCircle(int x, int y, int diameter)
{
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()
{
void draw()
{
void update(int x, int y)
{
void mousePressed()
{
boolean overRect(int x, int y, int width, int height)
{
boolean overCircle(int x, int y, int diameter)
{
CircleButton circle1, circle2, circle3;
RectButton rect1, rect2;
boolean locked = false;
void setup()
{
void draw()
{
void update(int x, int y)
{
class Button
{
class CircleButton extends Button
{
class RectButton extends Button
{
boolean overRect(int x, int y, int width, int height)
{
boolean overCircle(int x, int y, int diameter)
{
ImageButtons button;
void setup()
{
void draw()
{
class Button
{
}
class ImageButtons extends Button
{
Handle[] handles;
int num;
void setup()
{
void draw()
{
void mouseReleased()
{
class Handle
{
boolean overRect(int x, int y, int width, int height)
{
int lock(int val, int minv, int maxv)
{
PImage top, bottom; // Two image to load
int topWidth, bottomWidth; // The width of the top and bottom images
void setup()
{
void draw()
{
void setImage() {
}
class HScrollbar
{
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;
color circleColor;
color baseColor;
boolean rectOver = false;
boolean circleOver = false;
void setup()
{
size(200, 200); rectColor = color(0); circleColor = color(255); baseColor = color(102); 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);
noStroke(); if (rectOver) { background(rectColor); } else if (circleOver) { background(circleColor); } else { background(baseColor); }
stroke(255); fill(rectColor); rect(rectX, rectY, rectSize, rectSize); stroke(0); fill(circleColor); 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; }}
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; }}
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); 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; }}
color currentcolor;
CircleButton circle1, circle2, circle3;
RectButton rect1, rect2;
boolean locked = false;
void setup()
{
size(200, 200);
color baseColor = color(102, 102, 102); currentcolor = baseColor; // Define and create circle button int x = 30; int y = 100; int size = 100; color buttoncolor = color(153, 153, 102); color highlight = color(102, 102, 51); ellipseMode(CENTER); circle1 = new CircleButton(x, y, size, buttoncolor, highlight); // Define and create rectangle button x = 130; y = 110; size = 24; buttoncolor = color(255, 153, 255); highlight = color(204, 153, 204); circle2 = new CircleButton(x, y, size, buttoncolor, highlight); // Define and create rectangle button x = 130; y = 140; size = 24; buttoncolor = color(204, 204, 0); highlight = color(153, 153, 0); circle3 = new CircleButton(x, y, size, buttoncolor, highlight); // Define and create rectangle button x = 150; y = 20; size = 100; buttoncolor = color(153, 102, 102); highlight = color(102, 51, 51); rect1 = new RectButton(x, y, size, buttoncolor, highlight); // Define and create rectangle button x = 90; y = 20; size = 50; buttoncolor = color(153, 153, 153); highlight = color(102, 102, 102); rect2 = new RectButton(x, y, size, buttoncolor, highlight);}
void draw()
{
background(currentcolor); stroke(255); update(mouseX, mouseY); circle1.display(); circle2.display(); circle3.display(); rect1.display(); rect2.display();}
void update(int x, int y)
{
if(!locked) { circle1.update(); circle2.update(); circle3.update(); rect1.update(); rect2.update(); } else { locked = false; } if(mousePressed) { if(circle1.pressed()) { currentcolor = circle1.basecolor; } else if(circle2.pressed()) { currentcolor = circle2.basecolor; } else if(circle3.pressed()) { currentcolor = circle3.basecolor; } else if(rect1.pressed()) { currentcolor = rect1.basecolor; } else if(rect2.pressed()) { currentcolor = rect2.basecolor; } }}
class Button
{
int x, y; int size; color basecolor, highlightcolor; color currentcolor; boolean over = false; boolean pressed = false; void update() { if(over()) { currentcolor = highlightcolor; } else { currentcolor = basecolor; } } boolean pressed() { if(over) { locked = true; return true; } else { locked = false; return false; } } boolean over() { return true; } void display() { }}
class CircleButton extends Button
{
CircleButton(int ix, int iy, int isize, color icolor, color ihighlight) { x = ix; y = iy; size = isize; basecolor = icolor; highlightcolor = ihighlight; currentcolor = basecolor; }
boolean over() { if( overCircle(x, y, size) ) { over = true; return true; } else { over = false; return false; } }
void display() { stroke(255); fill(currentcolor); ellipse(x, y, size, size); }}
class RectButton extends Button
{
RectButton(int ix, int iy, int isize, color icolor, color ihighlight) { x = ix; y = iy; size = isize; basecolor = icolor; highlightcolor = ihighlight; currentcolor = basecolor; } boolean over() { if( overRect(x, y, size, size) ) { over = true; return true; } else { over = false; return false; } } void display() { stroke(255); fill(currentcolor); rect(x, y, size, size); }}
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; }})
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); }}
Handle[] handles;
int num;
void setup()
{
size(200, 200); num = height/15; handles = new Handle[num]; int hsize = 10; for(int i=0; i<num; i++) { handles[i] = new Handle(width/2, 10+i*15, 50-hsize/2, 10, handles); }}
void draw()
{
background(153); for(int i=0; i<num; i++) { handles[i].update(); handles[i].display(); } fill(0); rect(0, 0, width/2, height);}
void mouseReleased()
{
for(int i=0; i<num; i++) { handles[i].release(); }}
class Handle
{
int x, y; int boxx, boxy; int length; int size; boolean over; boolean press; boolean locked = false; boolean otherslocked = false; Handle[] others; Handle(int ix, int iy, int il, int is, Handle[] o) { x = ix; y = iy; length = il; size = is; boxx = x+length - size/2; boxy = y - size/2; others = o; } void update() { boxx = x+length; boxy = y - size/2; for(int i=0; i<others.length; i++) { if(others[i].locked { otherslocked = true; break; } else { otherslocked = false; } } if(!otherslocked) { over(); press(); } if(press) { length = lock(mouseX-width/2-size/2, 0, width/2-size-1); } } void over() { if(overRect(boxx, boxy, size, size)) { over = true; } else { over = false; } } void press() { if(over && mousePressed || locked) { press = true; locked = true; } else { press = false; } } void release() { locked = false; } void display() { line(x, y, x+length, y); fill(255); stroke(0); rect(boxx, boxy, size, size); if(over || press) { line(boxx, boxy, boxx+size, boxy+size); line(boxx, boxy+size, boxx+size, boxy); }
}}
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; }}
int lock(int val, int minv, int maxv)
{
return min(max(val, minv), maxv);}
HScrollbar hs1, hs2;
PImage top, bottom; // Two image to load
int topWidth, bottomWidth; // The width of the top and bottom images
void setup()
{
size(200, 200); noStroke(); hs1 = new HScrollbar(0, 20, width, 10, 3*5+1); hs2 = new HScrollbar(0, height-20, width, 10, 3*5+1); setImage();}
void draw()
{
background(255); // Get the position of the top scrollbar // and convert to a value to display the top image float topPos = hs1.getPos()-width/2; fill(255); image(top, width/2-topWidth/2 + topPos*2, 0); //println(hs1.getPos()); // Get the position of the bottom scrollbar // and convert to a value to display the bottom image float bottomPos = hs2.getPos()-width/2; fill(255); image(bottom, width/2-bottomWidth/2 + bottomPos*2, height/2); hs1.update(); hs2.update(); hs1.draw(); hs2.draw(); //println(hs1.getPos());}
void setImage() {
top = loadImage("seedTop.jpg"); topWidth = top.width; bottom = loadImage("seedBottom.jpg"); bottomWidth = bottom.width;
}
class HScrollbar
{
int swidth, sheight; // width and height of bar int xpos, ypos; // x and y position of bar float spos, newspos; // x position of slider int sposMin, sposMax; // max and min values of slider int loose; // how loose/heavy boolean over; // is the mouse over the slider? boolean locked; float ratio;
HScrollbar (int xp, int yp, int sw, int sh, int l) { swidth = sw; sheight = sh; int widthtoheight = sw - sh; ratio = (float)sw / (float)widthtoheight; xpos = xp; ypos = yp-sheight/2; spos = xpos + swidth/2 - sheight/2; newspos = spos; sposMin = xpos; sposMax = xpos + swidth - sheight; loose = l; }
void update() { if(over()) { over = true; } else { over = false; } if(mousePressed && over) { locked = true; } if(!mousePressed) { locked = false; } if(locked) { newspos = constrain(mouseX-sheight/2, sposMin, sposMax); } if(abs(newspos - spos) > 1) { spos = spos + (newspos-spos)/loose; } }
int constrain(int val, int minv, int maxv) { return min(max(val, minv), maxv); }
boolean over() { if(mouseX > xpos && mouseX < xpos+swidth && mouseY > ypos && mouseY < ypos+sheight) { return true; } else { return false; } }
void draw() { fill(255); rect(xpos, ypos, swidth, sheight); if(over || locked) { fill(153, 102, 0); } else { fill(102, 102, 102); } rect(spos, ypos, sheight, sheight); }
float getPos() { // convert spos to be values between // 0 and the total width of the scrollbar return spos * ratio; }}
2010年02月05日(金) 13:51:34 Modified by ID:vTkKSMh+RQ