We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexDiscussionExhibition › fileReader for 10 marks
Page Index Toggle Pages: 1
fileReader for 10 marks (Read 1156 times)
fileReader for 10 marks
Dec 13th, 2006, 9:34pm
 
We just had an in lab test today :p for 10% of our overall subject marks.

we had 2hrs to make a sceneload system.

**Text file***(file starts from 800, 800)***

800, 800
layer 1
rect
f 0, 255, 0, 128
s 0, 0, 0
400, 400, 600, 600
rect
f 255, 0, 0, 128
s 0, 0, 0
400, 400, 200, 200
layer 2
rect
f 255, 0, 0, 128
s 0, 0, 0
400, 400, 400, 400
ellipse
f 0, 255, 255, 255
s 0, 0, 0
400, 400, 100, 200

*********************************

Code:
SceneGraph sg;
void setup()
{
sg = new SceneGraph();
}
void draw()
{
background(0);
sg.draw();
}
//===================================== my code
class SceneGraph
{
private int width, height;

ArrayList layers;
String order;
int layers2draw;

SceneGraph()
{
order = "";
layers = new ArrayList();
layers2draw = 0;
load("Sample1.txt");

size(width,height);
}

public void draw()
{
int arrayPlace = 0;

if (keyPressed)
{
if (keyCode == UP)
{
if(2>layers2draw)
layers2draw++;
}
else if (keyCode == DOWN)
{
if(0<layers2draw)
layers2draw--;
}
keyPressed = false;
}

for(int i = 0; i<order.length(); i++)
{
if(((int)order.charAt(i) - (int)'0')<=layers2draw)
{
i++;
if(order.charAt(i) == 'r')
{
rectMode(CENTER);
Arect temp = (Arect)layers.get(arrayPlace);
temp.draw();
}
else if(order.charAt(i) == 'e')
{
Aellipse temp = (Aellipse)layers.get(arrayPlace);
temp.draw();
}
arrayPlace++;
}
}
}
part2
Reply #1 - Dec 13th, 2006, 9:34pm
 
Code:


private void load(String fileName)
{
println("***********************");
println(fileName);
println("***********************\n");

String[] lines = loadStrings(fileName);
String[] textSplit;
int layerNum = 0;

for(int counter = 0; lines.length>counter; counter++)
{
if(0 == counter)
{
textSplit = lines[counter].split(", ");
width = Integer.parseInt(textSplit[0]);
height = Integer.parseInt(textSplit[1]);
}
else
{
textSplit = lines[counter].split(" ");

if(textSplit[0].equalsIgnoreCase("layer"))
{
println(lines[counter]);
layerNum++;
counter++;
}
order += layerNum;

//===================================================================================================== rect

if(lines[counter].equalsIgnoreCase("rect"))
{
println("rect");
order += "r";
Arect tempRect = new Arect();
for(int i = 0; i<3; i++)
{
counter++;
if(lines[counter].charAt(0) == 'f')
{
lines[counter] = lines[counter].substring(2);//remove the "f "
textSplit = lines[counter].split(", ");
tempRect.setColour(
Integer.parseInt(textSplit[0]),
Integer.parseInt(textSplit[1]),
Integer.parseInt(textSplit[2]),
Integer.parseInt(textSplit[3]));

}
else if(lines[counter].charAt(0) == 's')
{
lines[counter] = lines[counter].substring(2);//remove the "s "
textSplit = lines[counter].split(", ");
tempRect.setStroke(
Integer.parseInt(textSplit[0]),
Integer.parseInt(textSplit[1]),
Integer.parseInt(textSplit[2]));
}
else
{
textSplit = lines[counter].split(", ");
tempRect.place(
Integer.parseInt(textSplit[0]),
Integer.parseInt(textSplit[1]),
Integer.parseInt(textSplit[2]),
Integer.parseInt(textSplit[3]));
}
}
layers.add(tempRect);
}

//===================================================================================================== ellipse

else if(lines[counter].equalsIgnoreCase("ellipse"))
{
println("ellipse");
order += "e";
Aellipse tempEllipse = new Aellipse();
for(int i = 0; i<3; i++)
{
counter++;
if(lines[counter].charAt(0) == 'f')
{
lines[counter] = lines[counter].substring(2);//remove the "f "
textSplit = lines[counter].split(", ");
tempEllipse.setColour(
Integer.parseInt(textSplit[0]),
Integer.parseInt(textSplit[1]),
Integer.parseInt(textSplit[2]),
Integer.parseInt(textSplit[3]));

}
else if(lines[counter].charAt(0) == 's')
{
lines[counter] = lines[counter].substring(2);//remove the "s "
textSplit = lines[counter].split(", ");
tempEllipse.setStroke(
Integer.parseInt(textSplit[0]),
Integer.parseInt(textSplit[1]),
Integer.parseInt(textSplit[2]));
}
else
{
textSplit = lines[counter].split(", ");
tempEllipse.place(
Integer.parseInt(textSplit[0]),
Integer.parseInt(textSplit[1]),
Integer.parseInt(textSplit[2]),
Integer.parseInt(textSplit[3]));
}
}
layers.add(tempEllipse);
}
}//end else
}// end loop
println("file all done");
println("***********************\n");
}
}

//============================================================= rect
class Ashape
{

color colourFill;
color colourStroke;

int x, y, w, h;

public void setColour(int r, int g, int b, int a)
{
colourFill = color(r,g,b,a);
}
public void setStroke(int r, int g, int b)
{
colourStroke = color(r,g,b);
}
public void place(int x, int y, int w, int h)
{
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
}

class Arect extends Ashape
{
public void draw()
{
fill(colourFill);
stroke(colourStroke);
rect(x,y,w,h);
}
}
class Aellipse extends Ashape
{
public void draw()
{
fill(colourFill);
stroke(colourStroke);
ellipse(x,y,w,h);
}
}

Page Index Toggle Pages: 1