Ok I rewrote my button program. It shows 4 buttons each having a starting color red, then I change one in the setup to start it as the chosen button green, when you hover over the others they highlight to green and then when pressed change all to red except for the one pressed. The problem is that when I click out side the buttons they all go red. I know where the problem is in the mousepressed loop, but cant figure out another way to do the same thing, any Ideas
Thanks Ken
int bNumber = 4;
RectButton [] buttons = new RectButton[bNumber];
color[] colors = {#FF0000,#00FF00};
boolean locked = false;
class Button
{
String Name;
int posX, posY;
int sizeX;
int sizeY;
color buttonColor,regColor,highlightColor;
boolean over = false;
boolean pressed = true;
boolean toggle = false;
I have created 4 buttons, when you mouse over 1 button it changes color to highlight and when its not over it returns to a background color. If mouse is pressed it change and stays highlighted. If you chose another it highlights and returns the previous button to the background color. The problem Im having is I would like to have 1 button start as highlighted but the first time it hits update in my code it changes back to the background. Im having trouble on the logic part that would keep the button highlighted untill another is pressed
The code is below Thanks for the help Ken
int wWidth = 600; int wHeight = 800; int graphWidth = wWidth-90; int graphHeight = wHeight/3-35; color c1 = color(255,0,0); color c2 = color(0,255,0); color c3 = color(0,0,255); RectButton rect1,rect2,rect3,rect4;
//Button stuff boolean locked = false;
void setup(){ size(wWidth,wHeight); rect1 = new RectButton("/.5",10,10+0, 25, 25, c1, c2); rect2 = new RectButton("/1",10,10+25, 25, 25, c1, c2); rect3 = new RectButton("/2",10,10+50, 25, 25, c1, c2); rect4 = new RectButton("/4",10,10+75, 25, 25, c1, c2); }
class Button { String Name; int x, y; int sizeX; int sizeY; color basecolor, highlightcolor; color currentcolor; boolean over = false; boolean pressed = false; boolean start = true;
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 RectButton extends Button { RectButton(String A,int ix, int iy, int xsize,int ysize, color icolor, color ihighlight) { Name = A; x = ix; y = iy; sizeX = xsize; sizeY = ysize; basecolor = icolor; highlightcolor = ihighlight; if(A == "/1"){ //locked = false; currentcolor = highlightcolor; }else{ currentcolor = basecolor; } println(currentcolor); }
boolean over() { if( overRect(x, y, sizeX, sizeY) ) { over = true; return true; } else { over = false; return false; } } void displayButton() { stroke(0); fill(currentcolor); rect(x, y, sizeX, sizeY); fill(0); textAlign(CENTER); text(Name,x+sizeX/2,y+sizeY/2+5); } }
I created a graphing program for serial data and have 3 different graphs. I shifted them with translate and have buttons with each graph, but when I try to find the mouse position over each of the graphs. it only picks up the whole window mouse positions. Is there a way to have the mouse know where it is when the translation occurs.
code is below;
int wWidth = 600;
int wHeight = 800;
int graphWidth = wWidth-60;
int graphHeight = wHeight/3-35;
color c1 = color(255,0,0);
color c2 = color(0,255,0);
color c3 = color(0,0,255);
Graph xGraph,yGraph,zGraph;
//Serial parameters
import processing.serial.*;
//Serial and line graph data
Serial myPort;
//X,Y,Z accelerometer values
int valueX=0;
int valueY=0;
int valueZ=0;
//String data from serial
int lf = 10;
String stringToChange="";
//Save data points to graph each redraw
int[] valueXs = new int[graphWidth];
int[] valueYs = new int[graphWidth];
int[] valueZs = new int[graphWidth];
//Button stuff
boolean locked = false;
void setup(){
//String portName = Serial.list()[0];
//myPort = new Serial(this, portName, 9600);
//myPort.bufferUntil('\n');
size(wWidth,wHeight);
xGraph = new Graph("X-Graph",c1,0,0,graphWidth,graphHeight,valueXs,1);
yGraph = new Graph("Y-Graph",c2,0,0,graphWidth,graphHeight,valueYs,1);
zGraph = new Graph("Z-Graph",c3,0,0,graphWidth,graphHeight,valueZs,4);
//Serial data until new line
stringToChange = myPort.readStringUntil('\n');
if (stringToChange != null) {//split X,Y,Z data that has a tab betweein
String [] data = split(stringToChange, '\t');
//trim string data and convert to ints
valueX = int(data[0].trim());
valueY = int(data[1].trim());
valueZ = int(data[2].trim());
// shift all values in array by 1 to free up the new data
for (int i = 1; i<valueXs.length;i++) {
valueXs[i-1] = valueXs[i];
valueYs[i-1] = valueYs[i];
valueZs[i-1] = valueZs[i];
}
//replace new data at last array point and scale if needed
valueXs[valueXs.length-1]=valueX;
valueYs[valueYs.length-1]=valueY;
valueZs[valueZs.length-1]=valueZ;
}
}
class Graph{
String gN;
int gX,gY,gW,gH,yS;
int [] gV;
color cV;
RectButton rect1,rect2,rect3,rect4;
class Button
{
String Name;
int x, y;
int sizeX;
int sizeY;
color basecolor, highlightcolor;
color currentcolor;
boolean over = false;
boolean 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 RectButton extends Button
{
RectButton(String A,int ix, int iy, int xsize,int ysize, color icolor, color ihighlight)
{
Name = A;
x = ix;
y = iy;
sizeX = xsize;
sizeY = ysize;
basecolor = icolor;
highlightcolor = ihighlight;
currentcolor = basecolor;
}
boolean over()
{
if( overRect(x, y, sizeX, sizeY) ) {
over = true;
return true;
}
else {
over = false;
return false;
}
}
void displayButton()
{
stroke(0);
fill(currentcolor);
rect(x, y, sizeX, sizeY);
fill(0);
textAlign(CENTER);
text(Name,x+sizeX/2,y+sizeY/2+5);
}
}
The program creates 4 buttons, and 2 squares with titles above them. Clicking on the buttons change the titles of the squares, the problem is that when the title change it just writes above the original title.
1 Im trying to use the image as a background that sometimes needs to be changed. So i redraw it at the beginning of draw().
2 in imageSquares I re display then get the graphbackground, but I think its taking the image that is still displayed.
3 is there a way that will just update the image and the text without it over writing it.
code used is just a sample Im using for a graphing program and sometimes I just want the background to change when it is graphing but this overwriting is messing it up;
Thanks ken
RectButton rect1, rect2, rect3, rect4;
squares sq1,sq2;
boolean locked = false;
color offColor = color(255,0,0);
color onColor = color(0,255,0);
int sensitivity=1;
PImage graphBGround;
void setup()
{
size(400, 200);
smooth();
// Define and create circle button\
// Define and create rectangle button
sq1 = new squares("",50,100,25);
sq2 = new squares("",100,100,25);
imageSquares();
rect1 = new RectButton("Sen X1",20, 20, 50, 30, offColor, onColor);
rect2 = new RectButton("Sen X2",70, 20, 50, 30, offColor, onColor);
rect3 = new RectButton("Sen X4",120, 20, 50, 30, offColor, onColor);
rect4 = new RectButton("Sen X8",170, 20, 50, 30, offColor, onColor);
}
squares(String name1,int x1, int y1, int size1)
{
Name=name1;
x=x1;
y=y1;
sizeX=size1;
}
void setName(String nameChange){this.Name=nameChange;}
void display(){
fill(0);
rect(x, y, sizeX, sizeX);
textAlign(LEFT);
text(Name,x,y-5);
}
}
class Button
{
String Name;
int x, y;
int sizeX;
int sizeY;
color basecolor, highlightcolor;
color currentcolor;
boolean over = false;
boolean pressed = false;
int edgeIndent = 20; int dBetweenGraph = 20; int gHeight = int((wHeight-4*dBetweenGraph-2*edgeIndent)/3); int gWidth = int((wWidth-2*dBetweenGraph-2*edgeIndent));
void drawRect(float x, float y, int xWidth, int yHeight){ print(xWidth); int wHashTag = 10; int yHashTag = 30; stroke(0); fill(255); rect (x-xWidth/2,y-yHeight/2,xWidth,yHeight);
for (int i=xWidth+dBetweenGraph+edgeIndent-30; i>31; i=i-30){
stroke(225); line (i,+1+y-yHeight/2,i,y-yHeight/2+wHashTag); line (i,y-wHashTag,i,y+wHashTag); line (i,-1+y+yHeight/2,i,y+yHeight/2-wHashTag); textAlign(CENTER); int toPrint = (i*-1)+xWidth+wHashTag; if((toPrint%60)==0){ text(toPrint,i+30,+15+y+yHeight/2); //println(i); } } } Which looks good.
Then I have this code
import processing.serial.*;
Serial myPort; int graphLength=300; int valueX=0; int valueY=0; int valueZ=0; int lf = 10; String stringToChange=""; int[] valueXs = new int[graphLength]; int[] valueYs = new int[graphLength]; int[] valueZs = new int[graphLength];
My question really is every time I add the graphing code to the first it doesnt show any graphing. I tried a few different ways and it seems that possibly that it blanks out the graph that I create and then tries to graph.
I know when I created the graph I had to worry about the order of when the rects and the line were drawn. I thought if I create the graph then add the code and the correct positioning I could just draw it on top.
Any Ideas appreciated. I will post later if I get any better information
also if this is not in the correct forum area please move.
Thanks Ken
Im trying to send with Arduino the code below integers between 0-300 and creates a line graph in processing. It works fine when I use the serial terminal and I see 0-300, but when it is being received in processing it only sees up to 255 and then starts over 0-44. and then restarts.
//Arduino code
int a=0;
void setup() {
Serial.begin(9600);
}
void loop() {
toSerial();
}
void toSerial() {
Serial.write(a);
if (a>300){
a=0;
}
a++;
}
//processing code
import processing.serial.*;
Serial myPort;
int value;
int[] values = new int[400];