gpt4 book ai didi

java - 使用类处理 OOP 问题

转载 作者:搜寻专家 更新时间:2023-11-01 08:46:42 26 4
gpt4 key购买 nike

我正在使用 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();
}
}

点类如下:

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这两个类都在同一个包中。

我尝试在设置函数内、它之外甚至在绘制循环中连续实例化该点,但都没有成功。

如果您需要更多信息,请告诉我。提前谢谢你。

最佳答案

问题已解决。供将来引用:

dot 类在任何情况下都不应该扩展 PApplet,这只应该是主要 Activity 的情况。但是,当 dot 类不扩展 PApplet 时,Eclipse 会报错,这并不奇怪,因为它不理解任何处理命令。为了解决这个问题,应该在开头声明 PApplet 类,然后在构造函数中将 PApplet 作为参数传递并分配给我们在开头声明的变量。现在,所有处理命令都应该被视为父类的方法。这是点的以下代码(我在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com