Using Pimage and get() to update and then replace the old image with the new
in
Programming Questions
•
1 year ago
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);
}
void imageSquares(){
sq1.display();
sq2.display();
graphBGround = get();
}
void draw()
{
image(graphBGround, 0, 0);
// stroke(0);
update(mouseX, mouseY);
rect1.display();
rect2.display();
rect3.display();
rect4.display();
}
void update(int x, int y)
{
if(locked == false) {
rect1.update();
rect2.update();
rect3.update();
rect4.update();
}
else {
locked = false;
}
if(mousePressed) {
if(rect1.pressed()) {
rect1.basecolor = onColor;
rect2.basecolor = offColor;
rect3.basecolor = offColor;
rect4.basecolor = offColor;
sensitivity = 1;
sq1.setName("1");
imageSquares();
} else if(rect2.pressed()) {
rect1.basecolor = offColor;
rect2.basecolor = onColor;
rect3.basecolor = offColor;
rect4.basecolor = offColor;
sensitivity = 2;
sq1.setName("2");
imageSquares();
} else if(rect3.pressed()) {
rect1.basecolor = offColor;
rect2.basecolor = offColor;
rect3.basecolor = onColor;
rect4.basecolor = offColor;
sensitivity = 4;
sq2.setName("4");
imageSquares();
} else if(rect4.pressed()) {
rect1.basecolor = offColor;
rect2.basecolor = offColor;
rect3.basecolor = offColor;
rect4.basecolor = onColor;
sensitivity = 8;
sq2.setName("8");
imageSquares();
}
}
}
class squares
{
String Name;
int x, y;
int sizeX;
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;
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 display()
{
stroke(0);
fill(currentcolor);
rect(x, y, sizeX, sizeY);
fill(0);
textAlign(CENTER);
text(Name,x+sizeX/2,y+sizeY/2+5);
}
}
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);
}
void imageSquares(){
sq1.display();
sq2.display();
graphBGround = get();
}
void draw()
{
image(graphBGround, 0, 0);
// stroke(0);
update(mouseX, mouseY);
rect1.display();
rect2.display();
rect3.display();
rect4.display();
}
void update(int x, int y)
{
if(locked == false) {
rect1.update();
rect2.update();
rect3.update();
rect4.update();
}
else {
locked = false;
}
if(mousePressed) {
if(rect1.pressed()) {
rect1.basecolor = onColor;
rect2.basecolor = offColor;
rect3.basecolor = offColor;
rect4.basecolor = offColor;
sensitivity = 1;
sq1.setName("1");
imageSquares();
} else if(rect2.pressed()) {
rect1.basecolor = offColor;
rect2.basecolor = onColor;
rect3.basecolor = offColor;
rect4.basecolor = offColor;
sensitivity = 2;
sq1.setName("2");
imageSquares();
} else if(rect3.pressed()) {
rect1.basecolor = offColor;
rect2.basecolor = offColor;
rect3.basecolor = onColor;
rect4.basecolor = offColor;
sensitivity = 4;
sq2.setName("4");
imageSquares();
} else if(rect4.pressed()) {
rect1.basecolor = offColor;
rect2.basecolor = offColor;
rect3.basecolor = offColor;
rect4.basecolor = onColor;
sensitivity = 8;
sq2.setName("8");
imageSquares();
}
}
}
class squares
{
String Name;
int x, y;
int sizeX;
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;
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 display()
{
stroke(0);
fill(currentcolor);
rect(x, y, sizeX, sizeY);
fill(0);
textAlign(CENTER);
text(Name,x+sizeX/2,y+sizeY/2+5);
}
}
1