Java 用类处理OOP问题

Java 用类处理OOP问题,java,android,eclipse,applet,processing,Java,Android,Eclipse,Applet,Processing,我正在使用Processing的Android模式创建草图。现在,我只想使用自定义点类显示椭圆。Eclipse没有检测到任何错误。我将发布整个代码以供参考。主要活动的代码如下所示: package com.example.yo; import processing.core.PApplet; public class MainActivity extends PApplet { public Dot dot = new Dot(50,50,155,200,20); public

我正在使用Processing的Android模式创建草图。现在,我只想使用自定义点类显示椭圆。Eclipse没有检测到任何错误。我将发布整个代码以供参考。主要活动的代码如下所示:

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);
        }

    }
}
Dot类别如下所示:

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类的结构与处理帮助页上给出的示例相同: 两个类都在同一个包中

我曾尝试在setup函数内、函数外实例化点,甚至在draw循环中连续实例化点,但都没有成功


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

问题已解决。供日后参考:

dot类在任何情况下都不应扩展PApplet,这仅适用于主要活动。然而,当dot类不扩展PApplet时,Eclipse会给出错误,这并不奇怪,因为它不理解任何处理命令。为了解决这个问题,PApplet类应该在开始时声明,然后在构造函数中,PApplet应该作为参数传递,并分配给我们在开始时声明的变量。现在,所有处理命令都应该被视为父类的方法。以下是dot的代码(我在MainActivity类中做的唯一更改是将此作为附加参数调用dot):

这里最好地描述了该问题的解决方案:


在Eclipse中使用多个类处理一节下。

日志将是开始的地方。