We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › constant transparency
Page Index Toggle Pages: 1
constant transparency (Read 1720 times)
constant transparency
May 6th, 2010, 5:17am
 
hey! i'm making an graphic user interface and i would like that my buttons were transparent and the only way im being able to do this is by adding them in the setup(){
and since i want my buttons to fly in and fly out i need to do this on the draw(){
but if i do this the image keeps adding on top of the previous and have no more a transparent button. at the same time I cant be adding a background because the button triggers a thing that needs previous frames to be visible!

how do i do this?
Re: constant transparency
Reply #1 - May 6th, 2010, 5:55am
 
brt wrote on May 6th, 2010, 5:17am:
but if i do this the image keeps adding on top of the previous and have no more a transparent button. at the same time I cant be adding a background because the button triggers a thing that needs previous frames to be visible!

how do i do this


Rethink how your 'previous frames' are being displayed.  The usual approach in this situation is - each time draw() is called - determine what should and shouldn't be displayed (usually using conditions and boolean variables).  So whatever code was used to draw the elements in the 'previous frames' should exist as separate entities, either as functions or class methods, that can be called each frame if an appropriate condition is true...

This should give you greater control and also avoid the issue you're having with transparency...
Re: constant transparency
Reply #2 - May 6th, 2010, 6:08am
 
Another alternative is to draw your cumulated graphics on an offline buffer.
On draw(), you just image() this buffer on sketch's surface.
Thus, you can draw your button above this one without perturbing your main graphics.
Re: constant transparency
Reply #3 - May 6th, 2010, 6:15am
 
ok! this is a simplyfied version of my code ( i removed a gradient on the background and a grid)
Code:
import tuioZones.*;
import oscP5.*;
import netP5.*;
TUIOzoneCollection zones;
Bolas b1 = new Bolas();
PImage butao;

void setup(){
size(screen.width,screen.height);
smooth();
butao = loadImage("butao.png");
zones=new TUIOzoneCollection(this);
zones.setZone("zone1", 50,height-150,100,100);
zones.setZone("zone2", width-150,height-150,100,100);
image(butao,zones.getZoneX("zone2"),zones.getZoneY("zone2"),zones.getZoneWidth("zone2"),zones.getZoneHeight("zone2"));
stroke(0);
rect(zones.getZoneX("zone1"),zones.getZoneY("zone1"),zones.getZoneWidth("zone1"),zones.getZoneHeight("zone1"));

}

void draw(){


if (zones.isZoneToggleOn("zone1")){
fill(255);
stroke(0);
//loop();
b1.desenhar();

}

if (zones.isZoneToggleOn("zone2")){
exit();

}
}

class Bolas{

float xplus = random(0.09);
float yplus = random(0.09);
float xinc = random(1);
float yinc = random(1);

float xx = random(width);
float yy = random(height);




void desenhar(){
stroke(0);
//noStroke();

float x3 = random(xx);
//float y3 = noise(yy);
float x = noise(xx)*width;
float y = noise(yy)*height;
xx += xplus;
yy += yplus;
int x2 = (int)x;
int y2 = (int)y;

//color col = img.get(x2,y2);
//fill(col);
//float prop = random(90,100);
float prop = random(9,20);
ellipse(x, y, prop, prop);



}
}


as you see my bolas class is some balls moving with some perlin noise and leaving a trail and that is my the main thing about this app ( off course other things will be added later
Re: constant transparency
Reply #4 - May 6th, 2010, 6:23am
 
PhiLho  wrote on May 6th, 2010, 6:08am:
Another alternative is to draw your cumulated graphics on an offline buffer.
On draw(), you just image() this buffer on sketch's surface.
Thus, you can draw your button above this one without perturbing your main graphics.


like in a PGraphics I'm trying it right now but not succeding tho :s
Re: constant transparency
Reply #5 - May 6th, 2010, 6:27am
 
brt wrote on May 6th, 2010, 6:23am:
like in a PGraphics I'm trying it right now but not succeding tho :s

Show us your attempt. Smiley
Re: constant transparency
Reply #6 - May 6th, 2010, 7:07am
 
well ive done it but its running super slow :s Code:
import tuioZones.*;
import oscP5.*;
import netP5.*;
TUIOzoneCollection zones;
Bolas b1 = new Bolas();
PImage butao;


void setup(){
size(screen.width,screen.height);
smooth();
butao = loadImage("butao.png");
zones=new TUIOzoneCollection(this);
zones.setZone("zone1", 50,height-150,100,100);
zones.setZone("zone2", width-150,height-150,100,100);
image(butao,zones.getZoneX("zone2"),zones.getZoneY("zone2"),zones.getZoneWidth("zone2"),zones.getZoneHeight("zone2"));
stroke(0);
rect(zones.getZoneX("zone1"),zones.getZoneY("zone1"),zones.getZoneWidth("zone1"),zones.getZoneHeight("zone1"));



}




void draw(){


//if (zones.isZoneToggleOn("zone1")){
if (key=='s'){
fill(255);
stroke(0);
b1.desenhar();


}

if (zones.isZoneToggleOn("zone2")){
exit();

}
}


class Bolas{

float xplus = random(0.09);
float yplus = random(0.09);
float xinc = random(1);
float yinc = random(1);

float xx = random(width);
float yy = random(height);
PGraphics cena;


void desenhar(){
cena = createGraphics(width,height,JAVA2D);


cena.beginDraw();
cena.smooth();
cena.stroke(0);
//noStroke();

float x3 = random(xx);
//float y3 = noise(yy);
float x = noise(xx)*width;
float y = noise(yy)*height;
xx += xplus;
yy += yplus;
int x2 = (int)x;
int y2 = (int)y;

//color col = img.get(x2,y2);
//fill(col);
//float prop = random(90,100);
float prop = random(9,20);
cena.ellipse(x, y, prop, prop);
cena.endDraw();
image(cena,0,0);



}
}



so even if it was fast what how would i draw the image button on the draw(){ to make it transparent? would i make a noLoop()? would that affect the PGraphic too?
Re: constant transparency
Reply #7 - May 6th, 2010, 7:28am
 
Quote:
its running super slow

No wonder, as you create the graphics on each draw. You are supposed to create it once, since you must draw multiple instances of your object on it.
Move the createGraphics() call to the constructor of your object.

No need to noLoop(), I don't see how it would help.

On event to draw the button, animate it (update its position) and just draw it in draw() after the call drawing your bolas.
Re: constant transparency
Reply #8 - May 6th, 2010, 7:34am
 
PhiLho  wrote on May 6th, 2010, 7:28am:
Quote:
its running super slow

No wonder, as you create the graphics on each draw. You are supposed to create it once, since you must draw multiple instances of your object on it.
Move the createGraphics() call to the constructor of your object.



I know that but it wasn't working  :s i don't know why. It says java.lang.RuntimeExeption: Processing.core.PGraphics.JAVA2D needs to be updated for the current release of processing.


PhiLho  wrote on May 6th, 2010, 7:28am:
[quote]

On event to draw the button, animate it (update its position) and just draw it in draw() after the call drawing your bolas.

this part I didn't understand :s



OFFtopic : by the way PhiLHo did you know that the way you screenname is read in Portuguese it means "son" Phi reads Fi  and the word for son is filho! Tongue
Re: constant transparency
Reply #9 - May 6th, 2010, 8:16am
 
OK, here is what I meant. The problem was that you used width & height values while creating Bolas before calling setup() which defines the values of these variables.
Code:
Bolas b1;
PImage butao;
boolean bMoveButton, bDrawButton;
float bX, bY;
float dbX = 20, dbY = 10;

void setup() {
size(screen.width, screen.height);
smooth();
// Better here as width & height variables are now defined
b1 = new Bolas();
butao = loadImage("D:/Dev/PhiLhoSoft/images/logo_sun_small_sdn.gif");
}

void draw() {
background(120);
b1.desenhar();
if (bMoveButton) {
dbX -= bX / width;
dbY -= bY / height;
bX += dbX;
bY += dbY;
if (bX > width / 2 - butao.width / 2) {
bMoveButton = false;
}
}
if (bDrawButton) {
image(butao, bX, bY);
}
}

void keyPressed() {
if (key == 's') {
bMoveButton = bDrawButton = true;
}
}

void mousePressed() {
if (mouseX > bX && mouseX < bX + butao.width &&
mouseY > bY && mouseY < bY + butao.height) {
bDrawButton = false;
// Do action
}
}

class Bolas {

float xplus = random(0.09);
float yplus = random(0.09);
float xinc = random(1);
float yinc = random(1);

float xx = random(width);
float yy = random(height);
PGraphics cena = createGraphics(width, height, JAVA2D);

void desenhar() {
cena.beginDraw();
cena.smooth();
cena.stroke(0);
//noStroke();

float x3 = random(xx);
//float y3 = noise(yy);
float x = noise(xx)*width;
float y = noise(yy)*height;
xx += xplus;
yy += yplus;
int x2 = (int)x;
int y2 = (int)y;

//color col = img.get(x2,y2);
//fill(col);
//float prop = random(90,100);
float prop = random(9,20);
cena.ellipse(x, y, prop, prop);
cena.endDraw();
image(cena,0,0);
}
}

(removed library code as I don't have them)
There is a poor attempt at easing the move of the button...

[OT] Amusing. It is just a shortcut for Philippe Lhoste, my real name, and I found that abbreviation amusing as it sounds like philo[sophy] (in English and in French) even if I have no ambition in this field!
Re: constant transparency
Reply #10 - May 6th, 2010, 8:59am
 
thanks! you got that problem solved! but now i gained a new one! wich is putting all the background stuff i had on the background like the gradient and grid in a PGraphic so i can keep redrawing the background in every draw().

not sure why I'm not being able to do this :s

Edit: the grid is already in a pgraphic...the gradient i can't do it :s its the void setGradient on the Learning section :s
Re: constant transparency
Reply #11 - May 6th, 2010, 12:03pm
 
Code:

int Y_AXIS = 1;
int X_AXIS = 2;
b11 = color(255);
b2 = color(#369DC9);
setGradient(0, 0, width, height, b11, b2, Y_AXIS);



Code:
 void setGradient(int x, int y, float w, float h, color c1, color c2, int axis ){
// calculate differences between color components
float deltaR = red(c2)-red(c1);
float deltaG = green(c2)-green(c1);
float deltaB = blue(c2)-blue(c1);

// choose axis
if(axis == Y_AXIS){
/*nested for loops set pixels
in a basic table structure */
// column
for (int i=x; i<=(x+w); i++){
// row
for (int j = y; j<=(y+h); j++){
color c = color(
(red(c1)+(j-y)*(deltaR/h)),
(green(c1)+(j-y)*(deltaG/h)),
(blue(c1)+(j-y)*(deltaB/h))
);
set(i, j, c);
}
}
}
else if(axis == X_AXIS){
// column
for (int i=y; i<=(y+h); i++){
// row
for (int j = x; j<=(x+w); j++){
color c = color(
(red(c1)+(j-x)*(deltaR/h)),
(green(c1)+(j-x)*(deltaG/h)),
(blue(c1)+(j-x)*(deltaB/h))
);
set(j, i, c);
}
}
}

}


how do i put this into a PGraphic?

Re: constant transparency
Reply #12 - May 6th, 2010, 12:50pm
 
The recipe is simple: whenever you see a Processing function acting on the graphics, here set(), just prefix it with a PGraphics object, eg. gradientGraphics.set(i, j, c);
Page Index Toggle Pages: 1