I have OKNO1 and OKNO2, I want to show a button with an image strictly in OKNO1.
At the moment, the button with the image is shown globally.
I used to use cp5.addButton
and there I did it with .moveTo("okno1");
Now the image button uses OOP/Java.
How to solve a tricky problem? What ways can you do the task. I think to use again void draw()
.
Complete code example:
import controlP5.*;
ControlP5 cp5;
ImageButton button;
Textlabel I;
Textlabel tempIn;
void setup() {
size(700, 420, P3D);
surface.setResizable(false);
smooth();
frameRate(30);
cp5 = new ControlP5(this);
PFont p = createFont("Times New Roman", 18);
ControlFont font=new
ControlFont(p);
cp5.setFont(font);
cp5.addTab("okno2")
.setColorBackground(color(0, 160, 100))
.setColorLabel(color(255))
.setColorActive(color(255, 128, 0));
cp5.getTab("default")
.activateEvent(true)
.setLabel("okno1")
.setId(1);
cp5.getTab("okno2")
.activateEvent(true)
.setId(2);
tempIn = cp5.addTextlabel("Sostoyanie")
.setText("1")
.setFont(createFont("Times New Roman", 20))
.setColor(color(0xffffff00))
.setPosition(155, 35);
I = cp5.addTextlabel("Impuls")
.setText("2")
.setPosition(155, 35)
.setFont(createFont("Times New Roman", 20))
.setColor(color(0xffffff00))
.moveTo("okno2");
int w = 99;
int h = 25;
button = new ImageButton(112, 137, w, h,
new PImage[]{
loadImage("0.png"), // off
loadImage("1.png"), // 10
loadImage("2.png"), // 20
loadImage("3.png"), // 30
loadImage("4.png"), // 40
loadImage("5.png"), // 50
loadImage("6.png"), // 60
});
}
void draw() {
background(0);
button.draw();
}
void mousePressed() {
button.mousePressed(mouseX, mouseY);
println(button.min);
}
PImage getImage(int w, int h, int c) {
PImage img = createImage(w, h, RGB);
java.util.Arrays.fill(img.pixels, c);
img.updatePixels();
return img;
}
class ImageButton {
int min = 0;
PImage[] stateImages;
int stateIndex;
int x, y;
int w, h;
String label = "ВЫКЛ";
ImageButton(int x, int y, int w, int h, PImage[] stateImages) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.stateImages = stateImages;
}
void mousePressed(int mx, int my) {
boolean isOver = ((mx >= x && mx <= x + w) &&
(my >= y && my <= y + h) ); // check vertical
if (isOver) {
min += 10;
stateIndex++;
if (min>60) {
min = 0;
stateIndex = 0;
label = "ВЫКЛ";
} else {
label = min + " Мин";
}
}
}
void draw() {
if (stateImages != null && stateIndex < stateImages.length) {
image(stateImages[stateIndex], x, y, w, h);
} else {
println("error displaying button state image");
println("stateImages: ");
printArray(stateImages);
println("stateIndex: " + stateIndex);
}
}