Java 按下按钮时更改图片

Java 按下按钮时更改图片,java,processing,control-p5,Java,Processing,Control P5,告诉我当你点击一张图片时,如何将其切换到下一张。我使用一个库“ControlP5,cp5.addbutton”和一个常规按钮,在这个库中我可以不用图片。 我有两个例子,一个普通的按钮和一张图片,我用一种不同的方式来改变图片,通过鼠标悬停和点击来清晰地显示。 “分钟数s1”-一个带有公共无效分钟数s1()的常规按钮,该按钮使用.setCaptionLabel(“БцЛ”)。 “分钟数2”-我认为该如何用于图片。setCaptionLabel(“БцЛ”)/不需要 完整的示例代码: import

告诉我当你点击一张图片时,如何将其切换到下一张。我使用一个库“ControlP5,cp5.addbutton”和一个常规按钮,在这个库中我可以不用图片。 我有两个例子,一个普通的按钮和一张图片,我用一种不同的方式来改变图片,通过鼠标悬停和点击来清晰地显示。

“分钟数s1”-一个带有公共无效分钟数s1()的常规按钮,该按钮使用.setCaptionLabel(“БцЛ”)。 “分钟数2”-我认为该如何用于图片。setCaptionLabel(“БцЛ”)/不需要

完整的示例代码:

import controlP5.*;
ControlP5 cp5;
int min=0;
Button minutess1;
Button minutess2;

void setup() {
  size(700,400);
  
  PFont fontn = createFont("Times New Roman",18);
  cp5 = new ControlP5(this);
  PFont p = createFont("Times New Roman",18);
  ControlFont font=new
  ControlFont(p);
  cp5.setFont(font);
  
    minutess1 = cp5.addButton("minutesss1")
    .setCaptionLabel("ВЫКЛ")
    .setPosition(200,200)
    .setSize(99,25);
    
    PImage[] imgs1 = {loadImage("0.png"),loadImage("1.png"),loadImage("2.png")}; // ,loadImage("3.png"),loadImage("4.png"),loadImage("5.png"),loadImage("6.png")
    minutess2 = cp5.addButton("minutesss2")
    //.setCaptionLabel("ВЫКЛ")
    .setPosition(400,200)
    .setImages(imgs1);
    minutess2.setSize(99,25);
  
textFont(fontn);}
  public void minutesss1() {
  min+=10;
 if (min>60) {min=0; minutess1.setCaptionLabel("ВЫКЛ"); }
  else  minutess1.setCaptionLabel(str(min)+" Мин");}
void draw(){
  background(0);
  fill(255);}

很高兴您发布了这样格式的代码。 如果您之前在Processing(Ctrl+T)中格式化它,以使其更易于阅读,那就更好了

很难完全理解你的要求。 如果你在使用翻译工具,你可以试着把长短语分解成更小、更简单的句子。希望翻译工具能做得更好

据我所知,这里有两个问题:

  • 如何从第二个按钮调用与第一个按钮相同的分钟更新功能
  • 如何使用自定义图像对第二个按钮进行蒙皮
  • 第一个问题可以通过多种方式解决。 以下是几个选项:

    选项1:使用
    controlEvent
    ,更新任何controlP5组件时会自动调用它。您可以检查按下了哪个按钮,并相应地调用函数:

    import controlP5.*;
    ControlP5 cp5;
    int min=0;
    Button minutess1;
    Button minutess2;
    
    void setup() {
      size(700, 400);
    
      PFont fontn = createFont("Times New Roman", 18);
      cp5 = new ControlP5(this);
      PFont p = createFont("Times New Roman", 18);
      ControlFont font=new
        ControlFont(p);
      cp5.setFont(font);
    
      minutess1 = cp5.addButton("minutesss1")
        .setCaptionLabel("ВЫКЛ")
        .setPosition(200, 200)
        .setSize(99, 25);
    
      //PImage[] imgs1 = {loadImage("0.png"), loadImage("1.png"), loadImage("2.png")}; // ,loadImage("3.png"),loadImage("4.png"),loadImage("5.png"),loadImage("6.png")
      PImage[] imgs1 = {getImage(99,25,color(0,0,192)),
                        getImage(99,25,color(0,0,240)),
                        getImage(99,25,color(0,0,120))};
      minutess2 = cp5.addButton("minutesss2")
        //.setCaptionLabel("ВЫКЛ")
        .setPosition(400, 200)
        .setImages(imgs1);
      minutess2.setSize(99, 25);
    
      textFont(fontn);
    }
    
    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;
    }
    
    public void minutesss1() {
      min+=10;
      if (min>60) {
        min=0; 
        minutess1.setCaptionLabel("ВЫКЛ");
      } else  minutess1.setCaptionLabel(str(min)+" Мин");
      println(min,minutess1.getCaptionLabel().getText());
    }
    
    void draw() {
      background(0);
      fill(255);
    }
    
    public void controlEvent(ControlEvent event) {
      if(event.controller() == minutess2){
        minutesss1();
      }
    }
    
    选项2:将第一个按钮按下功能的指令提取到一个单独的函数中,这两个函数都可以调用。这可能更简单、更直观:

    import controlP5.*;
    ControlP5 cp5;
    int min=0;
    Button minutess1;
    Button minutess2;
    
    void setup() {
      size(700, 400);
    
      PFont fontn = createFont("Times New Roman", 18);
      cp5 = new ControlP5(this);
      PFont p = createFont("Times New Roman", 18);
      ControlFont font=new
        ControlFont(p);
      cp5.setFont(font);
    
      minutess1 = cp5.addButton("minutesss1")
        .setCaptionLabel("ВЫКЛ")
        .setPosition(200, 200)
        .setSize(99, 25);
    
      //PImage[] imgs1 = {loadImage("0.png"), loadImage("1.png"), loadImage("2.png")}; // ,loadImage("3.png"),loadImage("4.png"),loadImage("5.png"),loadImage("6.png")
      // don't have images to reproduce: making new ones
      PImage[] imgs1 = {getImage(99,25,color(0,0,192)),
                        getImage(99,25,color(0,0,240)),
                        getImage(99,25,color(0,0,120))};
      minutess2 = cp5.addButton("minutesss2")
        //.setCaptionLabel("ВЫКЛ")
        .setPosition(400, 200)
        .setImages(imgs1);
      minutess2.setSize(99, 25);
    
      textFont(fontn);
    }
    
    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;
    }
    
    void updateMinutes(){
      min+=10;
      if (min>60) {
        min=0; 
        minutess1.setCaptionLabel("ВЫКЛ");
      } else  minutess1.setCaptionLabel(str(min)+" Мин");
      println(min,minutess1.getCaptionLabel().getText());
    }
    
    public void minutesss1() {
      updateMinutes();  
    }
    
    public void minutesss2() {
      updateMinutes();  
    }
    
    void draw() {
      background(0);
      fill(255);
    }
    
    关于问题的第二部分,不清楚您是否希望仅使用默认控件5状态(默认、结束、活动、突出显示)的图像。如果您传递的图像超过4个或少于3个,它们将被忽略,如中所示

    如果您想为每次分钟更新显示不同的图像(例如关闭、10、20、30、40、50、60),则需要制作自己的自定义按钮。 逻辑没有那么复杂,您可以使用作为更简单的起点

    封装更复杂的自定义功能将是非常好的,您需要一些面向对象编程(OOP)的基础知识。您可以查看和

    为了便于说明,这里有一个单独的草图,该草图将为每个按钮按下状态显示不同的图像(忽略覆盖/突出显示状态):

    ImageButton;
    无效设置(){
    大小(300300);
    //按钮尺寸
    int w=75;
    int h=25;
    //使用生成的图像进行测试
    按钮=新图像按钮(112、137、w、h、,
    新PImage[]{
    //将loadImage与您自己的图像一起使用,而不是getImage:)
    getImage(宽、高、颜色(192,0,32*2)),//关闭
    getImage(w,h,颜色(0,0,32*3)),//10
    getImage(w,h,颜色(0,0,32*4)),//20
    getImage(w,h,颜色(0,0,32*5)),//30
    getImage(w,h,颜色(0,0,32*6)),//40
    getImage(w,h,颜色(0,0,32*7)),//50
    getImage(w,h,颜色(0,0,32*8)),//60
    });
    //加载图像类似于:
    //按钮=新图像按钮(112、137、w、h、,
    //新PImage[]{
    //loadImage(“0.png”),//关闭
    //loadImage(“1.png”),//10
    //loadImage(“2.png”),//20
    //loadImage(“3.png”),//30
    //loadImage(“4.png”),//40
    //loadImage(“5.png”),//50
    //loadImage(“6.png”),//60
    //             });
    }
    作废提款(){
    背景(0);
    button.draw();
    }
    void mousePressed(){
    按钮。鼠标按下(mouseX,mouseY);
    println(按钮最小值);
    }
    //测试图像以表示加载的状态图像
    PImage getImage(整数w、整数h、整数c){
    PImage img=创建图像(w、h、RGB);
    java.util.array.fill(img.pixels,c);
    img.updatePixels();
    返回img;
    }
    //创建自定义图像按钮类
    类图像按钮{
    //分钟是它存储的数据
    int min=0;
    //每个州的图像
    PImage[]状态图像;
    //显示哪个图像
    int状态索引;
    //位置
    int x,y;
    //尺寸:宽度、高度
    int w,h;
    //要显示的文本
    String label=“БцЛ”;
    图像按钮(整数x、整数y、整数w、整数h、PImage[]状态图像){
    这个.x=x;
    这个。y=y;
    这个.w=w;
    这个,h=h;
    this.stateImages=stateImages;
    }
    鼠标按下无效(int mx,int my){
    //检查光标是否在按钮边界内
    布尔值isOver=((mx>=x&&mx=y&&my 60){
    最小值=0;
    stateIndex=0;
    label=“БцЛ”;
    }否则{
    标签=最小值+;
    }
    }
    }
    作废提款(){
    //如果图像和索引有效
    if(stateImages!=null&&stateIndex
    谢谢。正如我从您的示例中看到的。我需要将第一个示例与最后一个示例连接起来。任务是从按钮(关闭,10-60)传输数据对于控制器,按钮本身会在按所需的分钟数后更改文本。我想通过按顺序绘制图片来实现同样的效果。当然,我不需要将图片从鼠标悬停更改为单击。这取决于您对OOP/Java的舒适度。如果您足够舒适,可以扩展ControlP5Button类,重写
    setImages(…)
    以处理自定义图像并使用它。否则
    ImageButton button;
    
    void setup(){
      size(300, 300);
      // button dimensions
      int w = 75;
      int h = 25;
      // test with generated images
      button = new ImageButton(112, 137, w, h, 
                   new PImage[]{
                     // use loadImage with your own images instead of getImage :)
                     getImage(w, h, color(192, 0, 32 * 2)), // off
                     getImage(w, h, color(0, 0, 32 * 3)), // 10
                     getImage(w, h, color(0, 0, 32 * 4)), // 20
                     getImage(w, h, color(0, 0, 32 * 5)), // 30
                     getImage(w, h, color(0, 0, 32 * 6)), // 40
                     getImage(w, h, color(0, 0, 32 * 7)), // 50
                     getImage(w, h, color(0, 0, 32 * 8)), // 60
                   });
                   
      // loading images will be something similar to:
      //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);
    }
    
    // test images to represent loaded state images
    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;
    }
    
    // make a custom image button class
    class ImageButton{
      // minutes is the data it stores
      int min = 0;
      // images for each state
      PImage[] stateImages;
      // which image to display
      int stateIndex;
      // position
      int x, y;
      // dimensions: width , height
      int w, h;
      // text to display
      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){
        // check the cursor is within the button bounds
        boolean isOver = ((mx >= x && mx <= x + w) &&  // check horizontal
                          (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 the images and index are valid
        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);
        }
        // display text
        text(label, x + 5, y + h - 8);
      }
      
    }