我正在尝试将图像应用于我创建的形状。每次单击砍伐按钮时,它都会创建一个图形,我会尝试将我在文件夹中的图片应用为纹理,这样形状就会充满树木,而不仅仅是黑色。我将为海平面和 c02 添加纹理,因此它会随着每个图形而变化我只想先找出森林,所以在森林按钮中我将 img 变量设为森林图片。
当我在控制台中运行时收到错误代码,提示 texture() 不适用于此渲染器。
Table table;
IntList yearList;
IntList graphedData;
String fileName;
String name;
PImage img;
void loadLabelsAndButtons() {
// Draw sea level button
fill(255); // added fill color
//c02 button
rect(50, 560, 150, 30, 18);
textSize(12);
textAlign(CENTER);
fill(0, 0, 0);
text("c02 Increase ", 120, 580);
//sealevel button
fill(255); // added fill color
rect(250, 560, 150, 30, 18);
fill(0, 0, 0);
text("Sea increase", 320, 580);
//deaforstation button
fill(255); // added fill color
rect(450, 560, 150, 30, 18);
fill(0, 0, 0);
text("Deforestation", 520, 580);
}
void setup() {
size(700, 600);
loadLabelsAndButtons();
}
void draw() {
}
void mousePressed() {
//sky map button
if (mouseX > 50 && mouseX < 200 && mouseY > 560 && mouseY < 590) {
//calls forest map
drawGraph("sky.csv");
textSize(20);
text("Global c02 increase scince 1960", 350, 100);
textSize(12);
text("(Million metric tons)", 350, 125);
fill(128,128,128);
text("(Source: Global Carbon Project:)", 350, 145);
}
//sealevel map button
if (mouseX > 250 && mouseX < 400 && mouseY > 560 && mouseY < 590) {
//calls seaslevel map
drawGraph("seaLevel.csv");
textSize(20);
text("United States sea-level increase scince 1960", 350, 100);
textSize(12);
text("(Millimeters)", 350, 125);
fill(128,128,128);
text("(Source:NOAA)", 350, 145);
}
//forest map button
if (mouseX > 450 && mouseX < 600 && mouseY > 560 && mouseY < 590) {
//calls forest map
** img = loadImage("tree.jpg");
** drawGraph("deforest.csv");
textSize(20);
text("Global deforestation scince 1960", 350, 100);
textSize(12);
text("(Million Hectares)", 350, 125);
fill(128,128,128);
text("(Source: Worl Data:)", 350, 145);
}
}
void drawGraph(String name) {
background(255);
//clear canvas
loadLabelsAndButtons();
//getsdata for new graph to draw
parseData(name);
//creates x axis
yearText();
int y = 600;
int x = 50;
int v = 0;
**
textureMode(NORMAL);
texture(img);**
beginShape();
for (v = 0; v < yearList.size(); v++) {
texture(img);
y = graphedData.get(v);
// align to the middle of the screen
vertex(x, (900 - y) / 2 + 50);
x = x + 50;
}
vertex(x-50, 900/2 + 50); // add vertex to complete the shape
endShape();
yearText();
}
//gets data for graphs from each spreadsheet
void parseData(String fileName) {
//water levels
yearList = new IntList();
graphedData = new IntList();
table = loadTable(fileName, "header");
for (TableRow row : table.rows()) {
int year = row.getInt("Year");
int yValue = row.getInt("Rise");
yearList.append(year);
graphedData.append(yValue);
}
}
//headers for x axis
void yearText() {
int y = 0;
int yText = 0;
int x = 50;
int v = 0;
int xText = 0;
for (v = 0; v < yearList.size(); v++) {
yText = graphedData.get(v);
xText = yearList.get(v);
y = (900 - yText) / 2 + 50;
fill(0);
textSize(12);
textAlign(CENTER);
text(yText, x, y - 10);
text(xText, x, 550);
x = x + 50;
}
}
您使用的方法 (
texture()
) 抛出此错误,因为此功能仅适用于处理中的 P3D 和 OPENGL 渲染器。要解决此问题,您应该在设置时将 P3D 添加到您的尺寸中。这是应该看的:
void setup() {
size(700, 600, P3D);
loadLabelsAndButtons();
}