changing country color
in
Programming Questions
•
1 year ago
Hi,
I'm trying to reshape the countries with difffrent colors but the coordinates are wrong.
Please, need help
import processing.core.*;
import java.util.*;
public class Co2_Emission extends PApplet {
/**
*
*/
private static final long serialVersionUID = 5138543055487809746L;
// create PShape objects
PShape world;
PFont font, legende, jahre;
int plus = 50;
int z = 240;
boolean started = false;
int mode = 0;
int year = 1;
int j1 = 1;
// CSV data
csv countryData, co2Emission;
ArrayList<PShape> country = new ArrayList<PShape>();
String years[] = new String[] { new String("1990"), new String("1991"),
new String("1992"), new String("1993"), new String("1994"),
new String("1995"), new String("1996"), new String("1997"),
new String("1998"), new String("1999"), new String("2000"),
new String("2001"), new String("2002"), new String("2003"),
new String("2004"), new String("2005"), new String("2006"),
new String("2007"), new String("2008"), new String("2009"),
new String("2010"), };
public void setup() {
size(1450, 680);
smooth();
noLoop();
// load vector graphic
world = loadShape("../Weltkarte.svg");
// load csv
countryData = new csv("../Country.csv");
co2Emission = new csv("../Co2.csv");
frameRate(1);
}
public void draw() {
background(255);
world.enableStyle();
resetMatrix();
shape(world,20,30);
world.disableStyle();
// draw on display
font = createFont("AVGmdBU-48.vlw", 20);
textFont(font);
fill(85, 0, 0);
text("Co2 Emission 1990-2010", 15, 20);
legende = createFont("AVGmdBU-48.vlw", 15);
textFont(legende);
fill(85, 0, 0);
text("Legende \nCo2 pro Person in Tonnen \n 0 - 5 \n 5 - 10 \n 10 - 15 \n 15 - 20 \n 20 - 25 ",
15, 400);
// legend
fill(255, 105, 105);
stroke(255, 0, 0, 99);
rect(15, 440, 15, 10);
fill(255, 65, 65);
stroke(255, 0, 0, 99);
rect(15, 460, 15, 10);
fill(255, 26, 26);
stroke(255, 0, 0, 99);
rect(15, 480, 15, 10);
fill(242, 0, 0);
stroke(255, 0, 0, 99);
rect(15, 500, 15, 10);
fill(203, 0, 0);
stroke(255, 0, 0, 99);
rect(15, 520, 15, 10);
// dunkelste
fill(163, 0, 0);
stroke(242, 0, 0);
rect(15, 540, 15, 10);
// draw lines on display
stroke(85, 0, 0);
line(240, 640, 240, 655);
int x = 240;
for (int i = 0; i < years.length - 1; i++) {
x += plus;
line(x, 640, x, 655);
}
jahre = createFont("AVGmdBU-48.vlw", 15);
textFont(jahre, 15);
int y = 240;
// draw years on display
for (int j = 0; j < years.length; j++) {
text(years[j], y, 640);
y += plus;
}
fill(255);
stroke(170, 0, 0);
ellipse(z, 650, 8, 8);
if (z < 1240) {
z += 50;
}
//
while(mode < 20)
//{
//for(int i = year; i< co2Emission.csv.length;i++) // i == Spalte ; j1 == Zeile
//{
//if(Float.parseFloat(co2Emission.csv[1][2])<= 7.0f)
//{
fill(255, 26, 26);
shape(world.getChild(co2Emission.csv[1][0]));
}
/*else if(Float.parseFloat(co2Emission.csv[i][2]) > 5.0f && Float.parseFloat(co2Emission.csv[i][1]) <= 10.0f)
{
fill(255, 65, 65);
shape(world.getChild(co2Emission.csv[i][0]));
}
else if(Float.parseFloat(co2Emission.csv[i][3])> 10.0f && Float.parseFloat(co2Emission.csv[i][1])<= 15.0)
{
fill(255, 26, 26);
shape(world.getChild(co2Emission.csv[i][0]));
}
else if(Float.parseFloat(co2Emission.csv[i][4])>15.0 && Float.parseFloat(co2Emission.csv[i][1])<= 20.0)
{
fill(242, 0, 0);
shape(world.getChild(co2Emission.csv[i][0]));
}
else if(Float.parseFloat(co2Emission.csv[i][5])>20.0 && Float.parseFloat(co2Emission.csv[i][1])<= 25.0)
{
fill(203, 0, 0);
shape(world.getChild(co2Emission.csv[i][0]));
}
else if(Float.parseFloat(co2Emission.csv[i][6])>25.0)
{
fill(163, 0, 0);
shape(world.getChild(co2Emission.csv[i][0]));*/
//}
//mode++;
//}
//}
//}
public void mousePressed() {
if (started == false) {
loop();
}
started = true;
}
void saveHiRes(int scaleFactor) {
// create a new PGraphics object
PGraphics hires = createGraphics(width * scaleFactor, height
* scaleFactor, JAVA2D);
// everything is drawn as PGraphic
beginRecord(hires);
// and scale
hires.scale(scaleFactor);
draw();
endRecord();
// save PGraphics object as .png
hires.save("hires.svg");
}
float[] getCoords(PShape thisShape)
{
float xMax = 20;
float yMax = 30;
float xMin = width;
float yMin = height;
float[] coords = new float[2];
for(int i = 0; i < thisShape.getVertexCodeCount();i++)
{
if (thisShape.getVertexY(i)>yMax) yMax=thisShape.getVertexY(i);
if (thisShape.getVertexX(i)<xMin) xMin=thisShape.getVertexX(i);
if (thisShape.getVertexY(i)<yMin) yMin=thisShape.getVertexY(i);
}
coords[0]=(xMin+xMax)/2;
coords[1]=(yMin+yMax)/2;
return coords;
}
}
import processing.core.PApplet;
public class csv extends PApplet
{
/**
*
*/
private static final long serialVersionUID = 1L;
public String[][] csv;
public int max = 0;
public csv(String filename)
{
String[] lines = loadStrings(filename);
int csvWidth = 0;
System.out.println(lines.length);
//calculate max width of csv file
for (int i=0; i < lines.length; i++)
{
String[] strings = split(lines[i],';');
if (strings.length > csvWidth)
{
csvWidth = strings.length;
}
}
// csvWidth = 4;
csv = new String[lines.length][csvWidth];//1.Zeile rauswerfen-> also von und 3 Spalte rauswerfen
for (int i=0; i < lines.length; i++)
{
String[] tmp = new String[csvWidth];
tmp = split(lines[i], ";");
for (int j=0; j < tmp.length; j++)
{
tmp[j] = tmp[j].replaceAll("\"", ""); //"" entfernen
tmp[j] = tmp[j].replaceAll(" ", ""); //Leerzeichen entfernen
tmp[j] = tmp[j].replaceAll(":","0"); // bei leeren Feldern das : entfernen
csv[i][j] = tmp[j];
}
}
}
}
1