处理,替换文字

时间:2015-09-17 11:24:03

标签: java text processing

我目前得到的代码是我正在尝试为研究制作的一张海报(要说明一切,因为它可能是相关的):

package interactiveposter;
import processing.core.PApplet;
import processing.core.PImage;


public class InteractivePoster extends PApplet {
// Declare variables:

    PImage[] imgs = new PImage[12]; 
    int i = 0;
    boolean introduction = true;
    boolean storyboardtext = true;
    boolean features = true;
    int picWidth = 300;
    int picHeight = 200;
    PImage storyboard;
    PImage phone;

// Set size of window and load images:

    public void setup() {
        size(750,900);
        smooth();
        storyboard = loadImage("C:/Users/Frederik/Desktop/Medialogy AAU/Images/storyboardfixed.png");
        storyboard.resize(270, 757);
        phone = loadImage("C:/Users/Frederik/Desktop/Medialogy AAU/Images/phone.PNG");
        phone.resize(300, 500);
    }

// All that should run continuously goes in draw:

    public void draw() {
        background(255,255,255);
        textAlign(CENTER);
        textSize(24);
        fill(0);
        text("Creative Play - Applied Technology",width/2,50); 
        textSize(16);
        fill(120);
        text("B-341",width/2,900);
        image(storyboard, 50, 100);
        image(phone, 385, 140);

        int tboxPos = 50;
        tboxPos=tboxPos+335;
        if(introduction == false) {
            features = true;
            text("Text 1...Introduction", 490, 230);
        }

        if(storyboardtext == false) {
            text("Text 2...Storyboard", 480, 230);
        }
        if(features == false) {
            text("Text 3...Features", 480, 230);
            introduction = true;
        }



        fill(0,0,0);
        rect(tboxPos,700, 300, 100, 7); //FrameRect

        fill(102,204,255);
        rect(tboxPos, 700, 300, 50, 7); //IntroductionRect
        fill(255,255,255);
        textSize(20);
        text("Introduction", tboxPos+150, 730);

        fill(102,204,255);
        rect(tboxPos, 750, 150, 50, 7); // StoryboardRect
        fill(255,255,255);
        textSize(20);
        text("Storyboard", tboxPos+75, 780);

        fill(102,204,255);
        rect(tboxPos+150, 750, 150, 50, 7); //FeaturesRect
        fill(255,255,255);
        textSize(20);
        text("Features", tboxPos+225, 780);
    }

// Check if mouse is clicked on one of the images, then change that variable from true to false or opposite

    public void mouseClicked() {
        if(mouseX > 385 && mouseX < 685 && mouseY > 700 && mouseY < 750)
        {
            if(introduction == true) introduction = false;
            else introduction = true;
        }
        if(mouseX > 385 && mouseX < 535 && mouseY > 750 && mouseY < 800)
        {
            if(storyboardtext == true) storyboardtext = false;
            else storyboardtext = true;
        }
        if(mouseX > 535 && mouseX < 685 && mouseY > 750 && mouseY < 800)
        {
            if(features == true) features = false;
            else features = true;
        }
    }

}

海报:enter image description here

因此,当您按下智能手机下方的按钮时,应显示相关文字。现在它单独工作,我点击介绍,但要看到其他人之一,我必须再次点击介绍,使其首先消失。 我需要做的是在单击另一个按钮时使文本替换另一个。

我尝试在if语句中将其他文本设置为true,但它只适用于其中一些,其他文件被阻止了。

另一个想法是在void mouseClicked()中做了一些事情,但我不确定是什么。

非常感谢帮助,谢谢=)

2 个答案:

答案 0 :(得分:0)

现在,您只为每个按钮设置一个变量。相反,您要做的是设置所有变量。

以下是一个例子:

if(mouseX > 385 && mouseX < 685 && mouseY > 700 && mouseY < 750){
   if(introduction == true){
      introduction = false;
   }
   else{
      features = false
      storyboardtext = false;
      introduction = true;
   }
}

顺便说一下,你可以缩短以上所有内容:

if(mouseX > 385 && mouseX < 685 && mouseY > 700 && mouseY < 750){
   features = false
   storyboardtext = false;
   introduction = !introduction;
}

您可能还会考虑使用enum而不是3个单独的boolean值。

答案 1 :(得分:0)

我建议使用整数来跟踪状态,更多状态布尔值变得更难以管理,并且更容易出错。

这是一个基本的例子:

final int INTRODUCTION = 0;
final int STORYBOARD = 1;
final int FEATURES = 2;
int state = INTRODUCTION;

void draw(){
  switch(state){
    case INTRODUCTION:
      drawIntroduction();
    break;
    case STORYBOARD:
      drawStoryboard();
    break;
    case FEATURES:
      drawFeatures();
    break;
  }
}

void drawIntroduction(){
  background(0);
  fill(255);
  text("Introduction",15,15);
}
void drawStoryboard(){
  background(255);
  fill(0);
  text("Storyboard",15,55);
}
void drawFeatures(){
  background(192);
  fill(64);
  text("Features",15,95);
}

void keyReleased(){
  state = (state + 1) % 3;//cycle through states to test
}

我建议使用单独的函数绘制每个状态以保持代码更整洁。按任意键可循环显示状态。

以上大致适合您的代码,看起来有点像这样:

package interactiveposter;
import processing.core.PApplet;
import processing.core.PImage;


public class InteractivePoster extends PApplet {
// Declare variables:

    PImage[] imgs = new PImage[12]; 
    int i = 0;
    int picWidth = 300;
    int picHeight = 200;
    PImage storyboard;
    PImage phone;

    final int INTRODUCTION = 0;
    final int STORYBOARD = 1;
    final int FEATURES = 2;
    int state = INTRODUCTION;


// Set size of window and load images:

    public void setup() {
        size(750,900);
        smooth();
        storyboard = loadImage("C:/Users/Frederik/Desktop/Medialogy AAU/Images/storyboardfixed.png");
        storyboard.resize(270, 757);
        phone = loadImage("C:/Users/Frederik/Desktop/Medialogy AAU/Images/phone.PNG");
        phone.resize(300, 500);
    }

// All that should run continuously goes in draw:

    public void draw() {
        background(255,255,255);
        textAlign(CENTER);
        textSize(24);
        fill(0);
        text("Creative Play - Applied Technology",width/2,50); 
        textSize(16);
        fill(120);
        text("B-341",width/2,900);
        image(storyboard, 50, 100);
        image(phone, 385, 140);

        int tboxPos = 50;
        tboxPos=tboxPos+335;
        if(state == INTRODUCTION) {
            text("Text 1...Introduction", 490, 230);
        }

        if(state == STORYBOARD) {
            text("Text 2...Storyboard", 480, 230);
        }
        if(state == FEATURES) {
            text("Text 3...Features", 480, 230);
        }



        fill(0,0,0);
        rect(tboxPos,700, 300, 100, 7); //FrameRect

        fill(102,204,255);
        rect(tboxPos, 700, 300, 50, 7); //IntroductionRect
        fill(255,255,255);
        textSize(20);
        text("Introduction", tboxPos+150, 730);

        fill(102,204,255);
        rect(tboxPos, 750, 150, 50, 7); // StoryboardRect
        fill(255,255,255);
        textSize(20);
        text("Storyboard", tboxPos+75, 780);

        fill(102,204,255);
        rect(tboxPos+150, 750, 150, 50, 7); //FeaturesRect
        fill(255,255,255);
        textSize(20);
        text("Features", tboxPos+225, 780);
    }

// Check if mouse is clicked on one of the images, then change that variable from true to false or opposite

    public void mouseClicked() {
        if(mouseX > 385 && mouseX < 685 && mouseY > 700 && mouseY < 750)
        {
          state = INTRODUCTION;
        }
        if(mouseX > 385 && mouseX < 535 && mouseY > 750 && mouseY < 800)
        {
            state = STORYBOARD;
        }
        if(mouseX > 535 && mouseX < 685 && mouseY > 750 && mouseY < 800)
        {
            state = FEATURES;
        }
    }

}

请注意,我无法测试此代码(因此可能存在语法错误),但解释的概念应该是明确的。

另外,请查看类似问题的this answer以及按钮示例(在文件&gt;示例&gt;主题&gt; GUI&gt;按钮下)在处理)