Mouse position after translation
in
Programming Questions
•
1 year ago
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);
}
void draw(){
background(127);
translate(25,25);
xGraph.display();
translate(0,graphHeight+25);
yGraph.display();
translate(0,graphHeight+25);
zGraph.display();
}
void serialEvent(Serial myPort) {
//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;
Graph(String gName,color cValue,int graphX,int graphY,int gWidth,int gHeight ,int[] graphYValues,int ySens){
gN = gName;
gX = graphX;
gY = graphY;
gW = gWidth;
gH = gHeight;
gV = graphYValues;
cV = cValue;
yS = ySens;
rect1 = new RectButton("/1",gX,gY+0, 25, 25, c1, c2);
rect2 = new RectButton("/2",gX,gY+25, 25, 25, c1, c2);
rect3 = new RectButton("/4",gX,gY+50, 25, 25, c1, c2);
rect4 = new RectButton("/8",gX,gY+75, 25, 25, c1, c2);
}
int gHashes = 30;//Grid Spacing
void createGrid(){
int b = gW/gHashes;//Used for center of X width
fill(0);
int gCenterH = gH/2;//Used to find Y height Center
for(int i=gCenterH/gHashes;i>-gCenterH/gHashes-1;i--){
stroke(225);
line(gX+1,gCenterH-gHashes*i,gW-1,gCenterH-gHashes*i);//Print Y line
textAlign(LEFT);
fill(255);
text(i*gHashes,gW+2,gCenterH-gHashes*i+5);//Print text for Y lines
//X hashes Lines
for(int j=0;j<b-1;j++){
if(j!=0){
line(gX+gW-j*gHashes,gCenterH-gHashes*i+5,gX+gW-j*gHashes,gCenterH-gHashes*i-5);
}
}
}
for(int k=0;k<b-1;k++){//Print Values of seconds on bottom of graph
textAlign(CENTER);
if(k%2==0){
text(k*gHashes/10,gX+gW-k*gHashes,gH+15);
}
}
rotate(radians(-90));
textAlign(CENTER);
fill(cV);
text(gN,-gH+gH/2,gY-5);
rotate(radians(90));
textAlign(LEFT);
fill(255);
text("Sec's",gX,gH+15);
}
void displayData(){
strokeWeight(1);
beginShape();//show line graph
stroke(cV);
noFill();
for(int i=0;i<gV.length;i++){
vertex(i+gX,gV[i]/yS+gH/2);
}
endShape();
}
void update(int x, int y){
if(locked == false) {
rect1.update();
rect2.update();
rect3.update();
rect4.update();
}
else {
locked = false;
}
}
void display(){
stroke(0);
fill(255);
rect(gX,gY,gW,gH);
createGrid();
displayData();
update(mouseX, mouseY);
rect1.displayButton();
rect2.displayButton();
rect3.displayButton();
rect4.displayButton();
}
}
class Button
{
String Name;
int x, y;
int sizeX;
int sizeY;
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;
currentcolor = highlightcolor;
return true;
}
else {
locked = false;
currentcolor = basecolor;
return false;
}
}
boolean over()
{
return 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;
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);
}
}
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);
}
void draw(){
background(127);
translate(25,25);
xGraph.display();
translate(0,graphHeight+25);
yGraph.display();
translate(0,graphHeight+25);
zGraph.display();
}
void serialEvent(Serial myPort) {
//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;
Graph(String gName,color cValue,int graphX,int graphY,int gWidth,int gHeight ,int[] graphYValues,int ySens){
gN = gName;
gX = graphX;
gY = graphY;
gW = gWidth;
gH = gHeight;
gV = graphYValues;
cV = cValue;
yS = ySens;
rect1 = new RectButton("/1",gX,gY+0, 25, 25, c1, c2);
rect2 = new RectButton("/2",gX,gY+25, 25, 25, c1, c2);
rect3 = new RectButton("/4",gX,gY+50, 25, 25, c1, c2);
rect4 = new RectButton("/8",gX,gY+75, 25, 25, c1, c2);
}
int gHashes = 30;//Grid Spacing
void createGrid(){
int b = gW/gHashes;//Used for center of X width
fill(0);
int gCenterH = gH/2;//Used to find Y height Center
for(int i=gCenterH/gHashes;i>-gCenterH/gHashes-1;i--){
stroke(225);
line(gX+1,gCenterH-gHashes*i,gW-1,gCenterH-gHashes*i);//Print Y line
textAlign(LEFT);
fill(255);
text(i*gHashes,gW+2,gCenterH-gHashes*i+5);//Print text for Y lines
//X hashes Lines
for(int j=0;j<b-1;j++){
if(j!=0){
line(gX+gW-j*gHashes,gCenterH-gHashes*i+5,gX+gW-j*gHashes,gCenterH-gHashes*i-5);
}
}
}
for(int k=0;k<b-1;k++){//Print Values of seconds on bottom of graph
textAlign(CENTER);
if(k%2==0){
text(k*gHashes/10,gX+gW-k*gHashes,gH+15);
}
}
rotate(radians(-90));
textAlign(CENTER);
fill(cV);
text(gN,-gH+gH/2,gY-5);
rotate(radians(90));
textAlign(LEFT);
fill(255);
text("Sec's",gX,gH+15);
}
void displayData(){
strokeWeight(1);
beginShape();//show line graph
stroke(cV);
noFill();
for(int i=0;i<gV.length;i++){
vertex(i+gX,gV[i]/yS+gH/2);
}
endShape();
}
void update(int x, int y){
if(locked == false) {
rect1.update();
rect2.update();
rect3.update();
rect4.update();
}
else {
locked = false;
}
}
void display(){
stroke(0);
fill(255);
rect(gX,gY,gW,gH);
createGrid();
displayData();
update(mouseX, mouseY);
rect1.displayButton();
rect2.displayButton();
rect3.displayButton();
rect4.displayButton();
}
}
class Button
{
String Name;
int x, y;
int sizeX;
int sizeY;
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;
currentcolor = highlightcolor;
return true;
}
else {
locked = false;
currentcolor = basecolor;
return false;
}
}
boolean over()
{
return 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;
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);
}
}
1