2015-11-12 79 views
1

我想写一个程序,其中文本被翻译成十六进制,然后到十进制,然后到(R,G,B)格式。但是,在尝试合并ClickableRectangle时,我得到一个ArrayIndexOutOfBounds异常,该异常动态地以rects的符号进行更改。为什么我会得到这个动态的IndexOutOfBounds异常?

对我的问题/优化有什么想法?

char[] colors; //Array of characters to be translated into hexadecimal 
String r = ""; //Red value of color 
String g = ""; //Green value of color 
String b = ""; //Blue value of color 
int x = 0;  //X-coordinate of rectangle 
int y = 0;  //Y-coordinate of rectangle 
int q;   //Character count 
ClickableRectangle[] rects = new ClickableRectangle[400*400]; //Rectangles 
void settings() { 
    size(displayWidth, displayHeight); 
} 

void setup() { 
    background(0); 
    colors = new char[3]; 
    String s = ([INSERT TRANSCRIPT HERE]); //Too long to be in post// 
    for (int i = 0; i < s.length(); i+=3) {  
    for (int j = i; j < i+3; j++) {  
     colors[j-i] = s.charAt(j); 
    } 
    r = hex(colors[0], 2); 
    g = hex(colors[1], 2); 
    b = hex(colors[2], 2); 
    drawAPoint(r, g, b, i); 
    println(i); 
    q++; 
    } 
    save("SlachtochtFeuf.png"); //Ignore this, using for testing purposes 
    println("q = " + q); 
    println("x = " + x); 
    println("y = " + y); 
} 

void draw() { 
    for (int i = 0; i < rects.length; i++) { 
    if (rects[i].isClicked()) { 
     println(rects[i].getValue()); //Prints char representation of color 
    } 
    } 
} 

void drawAPoint(String r2, String g2, String b2, int i) { 
    noStroke(); 
    fill(unhex(r2), unhex(g2), unhex(b2)); 
    rects[i] = new ClickableRectangle(x, y, r2, g2, b2); 
    rects[i].display(); 
    if (x >= width) { 
    x = 0; 
    y += 6; 
    } else { 
    x+=6; 
    } 
} 

class ClickableRectangle { 
    int x = 0; 
    int y = 0; 
    String r = ""; 
    String g = ""; 
    String b = ""; 

    public ClickableRectangle(int x, int y, String r, String g, String b) { 
    this.x = x; 
    this.y = y; 
    this.r = r; 
    this.g = g; 
    this.b = b; 
    } 

    public void display() { 
    fill(unhex(r), unhex(g), unhex(b)); 
    rect(x, y, 6, 6); 
    } 

    public void setRGB(String r, String g, String b) { 
    this.r = r; 
    this.g = g; 
    this.b = b; 
    } 

    public String getValue() { 
    return ""+char(unhex(r))+char(unhex(g))+char(unhex(b)); 
    } 

    public boolean isClicked() { 
    return mouseX > x && mouseY > y && mouseX < x+6 && mouseY < y+6; 
    } 
} 

回答

1

为了将来的参考,您应该告诉我们究竟哪一行引发异常,并且应该包含重复该问题所需的所有代码。如果文字太长而无法发布,请将其缩小至较小的MCVE

但是,从我可以看到情况来看,问题似乎是在这里:

String s = "test"; 
for (int i = 0; i < s.length(); i+=3) {  
    for (int j = i; j < i+3; j++) {  
    colors[j-i] = s.charAt(j); 
    } 
    //other stuff 
} 

只有通过此在你的脑袋上运行:

  • i = 0时和j = 0 ,您访问charAt 0.
  • 当i = 0且j = 1时,您访问charAt 1.
  • 当i = 0且j = 2时,您访问charAt 2。
  • 在i = 3和j = 3,您访问的charAt 3.
  • 在i = 3和j = 4,您访问的charAt 4.

你也可以使用println()语句,以便更好地看看会发生什么于:

for (int i = 0; i < s.length(); i+=3) { 
    println("i: " + i); 
    for (int j = i; j < i+3; j++) { 
    println("j: " + j); 
    colors[j-i] = s.charAt(j); 
    } 
    //other stuff 
} 

打印出:

i: 0 
j: 0 
j: 1 
j: 2 
0 
i: 3 
j: 3 
j: 4 

这最后一部分是problem-我使用的字符串为“测试”,所以它只有4个字符:charAt(0),charAt(1),charAt(2)charAt(3)。所以当你尝试访问charAt(4)时,它会抛出一个StringIndexOutOfBoundsException!

我不确切知道你在这里做什么,但是你必须重做你的代码,以便它不会尝试访问字符串范围之外的字符。

作为一个方面说明:它看起来像你试图编写你的整个项目一次。不要这样做。相反,在小的增量中开发这一点 - 在将它们合并到整个项目之前,尝试让小块自行工作。你能写一个单独的草图,只是简单地循环一个字符串,并打印出你想要提取的字符?那么如果你被卡住了,你可以使用那个较小的草图作为你的MCVE,这对我们来说更容易帮助你。

相关问题