在处理过程中进行循环

问题描述 投票:1回答:1

我正在做一个关于处理的动画。然后,我有一个关于循环的问题。通常情况下,我的代码比较长,但是,我做了一个简单的代码,对于初学者来说也是很有用的。但是,我做了一个简单的代码,对初学者来说也很有用。

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);

}

我的示例代码: 如何制作?

java processing
1个回答
1
投票

很有可能你需要修复你的 setup 方法从线上获取点数据,然后修改 draw 方法在循环中使用这些点。

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();
        }
    }
}

结果:

Closed Polyline


1
投票

大多数情况下,我使用数组并将文本文件的所有行获取到数组中,然后从索引中访问它们。示例代码在这里。

/*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中找到。https:/github.comMuhammadFaisal1521Java-Application-Inventory-Management-System-with-Oracle-DatabasetreemastersrcInventory。

© www.soinside.com 2019 - 2024. All rights reserved.