Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在处理过程中进行循环_Java_Processing - Fatal编程技术网

Java 在处理过程中进行循环

Java 在处理过程中进行循环,java,processing,Java,Processing,我正在制作一个关于处理的动画。然后,我有一个关于循环的问题。通常,我的代码比较长。然而,我做了一个简单的代码,它也可以对初学者有用。 我的示例代码: void setup() { size(500, 500); coordinates = loadStrings("coordinates.txt"); beginShape(); // It combines the all of vertexes } void draw() { point(initi

我正在制作一个关于处理的动画。然后,我有一个关于循环的问题。通常,我的代码比较长。然而,我做了一个简单的代码,它也可以对初学者有用。 我的示例代码:

void setup() 
{  
  size(500, 500);

  coordinates = loadStrings("coordinates.txt");
  beginShape();         // It combines the all of vertexes
}

void draw() 
{
  point(initialX, initialY);
  println(initialX, initialY, p);

}

我怎么做

您很可能需要修正
设置
方法以从测线获取点数据,然后修改
绘制
方法以在循环中使用这些点:

int[][] points;
int curr = 0;

void setup() {

    size(500, 500);

    strokeWeight(4);
    frameRate(5);

    coordinates = loadStrings("coordinates.txt");
    beginShape();         // It combines the all of vertexes

    points = new int[coordinates.length][2];
    int row = 0;
    for (String line : coordinates) {
        String[] pair = line.split(" ");
        points[row] = new int[] { Integer.parseInt(pair[0]), Integer.parseInt(pair[1])};
        println(points[row][0]); // print x
        println(points[row][1]); // print y
        row++;
    }

    fixLineCoords();
    endShape(CLOSE);
}

void fixLineCoords() {
    int indexStart = curr % points.length;
    int indexEnd = (curr + 1) % points.length;
    initialX = points[indexStart][0];
    initialY = points[indexStart][1];
    finalX = points[indexEnd][0];
    finalY = points[indexEnd][1];

    deltaX = abs(finalX - initialX);
    deltaY = abs(finalY - initialY);
    p = 2 * deltaY - deltaX;

    println("Line between points " + curr + " and " + (curr+1));
    counter = 0; // reset counter;
}

void draw() {
    point(initialX, initialY);
    println(initialX, initialY, p);

    if (finalX > initialX )
        initialX++;
    else
        initialX--;

    if (p < 0) {
        p = p + 2 * deltaY;
    } else {
        if (initialY > finalY)
            initialY--;
        else
            initialY++;
        p = p + 2 * deltaY - 2 * deltaX;
    }

    counter++;
    if (counter > deltaX) {
        if (curr == points.length) {
            noLoop(); // all points processed
        } else {
            curr++;
            fixLineCoords();
        }
    }
}
int[][]点;
int curr=0;
无效设置(){
大小(500500);
冲程重量(4);
帧率(5);
坐标=加载字符串(“coordinates.txt”);
beginShape();//它组合了所有顶点
点=新整数[坐标长度][2];
int行=0;
用于(字符串行:坐标){
String[]pair=line.split(“”);
points[row]=newint[]{Integer.parseInt(对[0]),Integer.parseInt(对[1])};
println(点[行][0]);//打印x
println(点[行][1]);//打印y
行++;
}
fixLineCoords();
端形(闭合);
}
void fixLineCoords(){
int indexStart=curr%points.length;
int indexEnd=(当前值+1)%points.length;
initialX=点[indexStart][0];
初始值=点[indexStart][1];
finalX=点[指数][0];
最终=点数[指数][1];
deltaX=abs(finalX-initialX);
deltaY=绝对值(最终-初始值);
p=2*deltaY-deltaX;
println(“点“+curr+”和“+(curr+1)”之间的线);
计数器=0;//重置计数器;
}
作废提款(){
点(首字母X,首字母Y);
println(initialX,initialY,p);
如果(finalX>initialX)
initialX++;
其他的
首字母X-;
if(p<0){
p=p+2*三角洲;
}否则{
如果(初始>最终)
起首--;
其他的
initialY++;
p=p+2*deltaY-2*deltaX;
}
计数器++;
如果(计数器>deltaX){
if(curr==points.length){
noLoop();//已处理所有点
}否则{
curr++;
fixLineCoords();
}
}
}
结果:


我通常使用数组,将文本文件的所有行提取到数组中,然后从索引中访问它们。示例代码在这里。此外,若你们在代码绘制上有问题,你们可以发短信给我

/*Get Configration File*/
File fileSoucrce = new File (System.getenv("APPDATA")+"\\sapphire\\xmc.txt");
Scanner myReader;

this.console("Configration file exists.");

try {
        String[] fileText = new String[10];
        int i =0;
        myReader = new Scanner(fileSoucrce);
        while (myReader.hasNextLine()) {
        fileText[i++]=myReader.nextLine();
        }
myReader.close();

ConnectString =fileText[0];
ConnectUSER       =fileText[1];
ConnectPassword       =fileText[2];
} catch (FileNotFoundException ex) {
     JOptionPane.showMessageDialog(new JFrame(), "Configration file not found at "+fileSoucrce.getAbsolutePath());
     Logger.getLogger(OracleCon.class.getName()).log(Level.SEVERE, null, ex);
    return;
}
此InventoryMainPage.java中提供了代码

谢谢,但这说明了问题所在。我想像我的第一个例子那样画线。你现在知道怎么做了吗?看起来差不多完成了,但是我有一个错误
initialX=points[I][0]在这部分中。您在哪里定义了
i
?它应该是
curr
请检查更新-添加了一次迭代,并使用
%点固定计算点索引。长度
为了避免
ArrayOutOfBoundsException
有不同的方法,如果您只需要添加一个文件,可以使用另一个变量
String[]coords2=loadStrings(“coordinares2.txt”)
,然后合并两个数组或可能参数化现有功能以使用单个坐标数组。或者,也可以为文件添加另一个维度,使坐标数组变为二维,点变为三维。