我正在使用Processing 的Android 模式来创建草图。目前,我只想使用自定义 Dot 类显示椭圆。 Eclipse 未检测到任何错误。我将发布完整的代码以供引用。主要 Activity 的代码如下:
package com.example.yo;
import processing.core.PApplet;
public class MainActivity extends PApplet {
public Dot dot = new Dot(50,50,155,200,20);
public void setup() {
background(0,0,0);
stroke(255,0,0);
strokeWeight(10);
}
public void draw() {
dot.display();
}
}
Dot 类如下:
package com.example.yo;
import processing.core.PApplet;
public class Dot extends PApplet{
//declaration of dot's fields
public int x;
public int y;
public int redd;
public int greenn;
public int bluee;
public Boolean through = false;
//constructor
Dot(int xPos, int yPos, int redness, int greenness, int blueness){
x = xPos;
y = yPos;
redd = redness;
greenn = greenness;
bluee = blueness;
}
public void display(){
noStroke();
fill(redd,greenn,bluee);
if (through){
ellipse(x, y,40,40);
}else{
ellipse(x, y, 30, 30);
}
}
}
当我尝试运行该应用程序时,该应用程序立即崩溃,并显示消息框“不幸的是,Yo 已停止”。我必须承认,我无法更准确地向您指出我的问题,因为我不知道代码中出了什么问题。 Dot 类的结构与处理帮助页面上给出的示例等效:https://www.processing.org/reference/class.html 这两个类都在同一个包中。
我尝试在设置函数内、外部甚至在绘图循环中连续实例化点,但没有成功。
如果您需要更多信息,请告诉我。预先感谢您。
最佳答案
问题已解决。供将来引用:
点类在任何情况下都不应该扩展 PApplet,这只应该是主要 Activity 的情况。然而,当点类没有扩展 PApplet 时,Eclipse 会给出错误,这并不奇怪,因为它不理解任何处理命令。为了解决这个问题,应该在开头声明 PApplet 类,然后在构造函数中将 PApplet 作为参数传递并分配给我们在开头声明的变量。现在,所有处理命令都应该被视为父类的方法。这是 dot 的以下代码(我在 MainActivity 类中所做的唯一更改是使用 this 作为附加参数来调用 Dot):
package com.example.yo;
import processing.core.*;
class Dot{
//declaration of dot's fields
public float xpos;
public float ypos;
public int redd;
public int greenn;
public int bluee;
public Boolean through = false;
PApplet parent;
//constructor
Dot(PApplet p, float xPosition, float yPosition, int redness, int greenness, int blueness){
parent = p;
xpos = xPosition;
ypos = yPosition;
redd = redness;
greenn = greenness;
bluee = blueness;
}
public void display(){
parent.noStroke();
parent.fill(redd,greenn,bluee);
if (through){
parent.ellipse(xpos, ypos, 4.0f, 4.0f);
}else{
parent.ellipse(xpos, ypos, 30.0f, 30.0f);
}
}
}
这里最好地描述了该问题的解决方案:
https://processing.org/tutorials/eclipse/
在使用多个类在 Eclipse 中进行处理部分下。
关于java - 处理类的 OOP 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27574515/