1: Pix.java:8: class PixApplet is public, should be declared in a file named PixApp
你的类是public的,文件名就必须和类名相同,包括大小写2:
Public Rectgl(int width,int height,Color c)// 小写(public)吧
^
3:Rectgl r=new Rectgl(10,5,Color.red); //应该是Color.Red4:public Square(int side,Color c){
super(side,side,c); //参数的个数不一样
}
//(非构造)方法重载必须signature(就是包括名字,参数等)相同
//构造方法就不知道了5:return Math.Pl * (myRadius*myRadius); //Pl应改成PI吧6:同5不知道对不对,你改改试试看……
你的类是public的,文件名就必须和类名相同,包括大小写2:
Public Rectgl(int width,int height,Color c)// 小写(public)吧
^
3:Rectgl r=new Rectgl(10,5,Color.red); //应该是Color.Red4:public Square(int side,Color c){
super(side,side,c); //参数的个数不一样
}
//(非构造)方法重载必须signature(就是包括名字,参数等)相同
//构造方法就不知道了5:return Math.Pl * (myRadius*myRadius); //Pl应改成PI吧6:同5不知道对不对,你改改试试看……
import java.applet.Applet;
import java.awt.*;
/* Pix-an applet for exploring inheritance.
*[email protected]
*version 0.1,10 April 1996
*/public class PixApplet extends Applet{
public void init(){
Rectgl r=new Rectgl(10,5,Color.red);
Square s=new Square(10,Color.blue);
Circle c=new Circle(20,Color.yellow);
Square s2=new Square(40,Color.green);
add(r);
add(s);
add(c);
add(s2);
add(new PixLabel(r));
add(new PixLabel(s));
add(new PixLabel(c));
add(new PixLabel(r));
}
}/*public abstract*/
class Pix extends Canvas{
Dimension myDimension=new Dimension();
public void Pix(){
}
public Color getColor(){
return getForeground();
}
public void setColor(Color c){
setForeground(c);
}
public void paint(Graphics g){
}
public double getArea(){
return 0;
}
public double getPerimeter(){
return 0;
}
public String getKind(){
return "unknoen shape";
}
public Dimension preferredSize(){
return myDimension;
}
public Dimension minimumSize(){
return myDimension;
}
}class Rectgl extends Pix{
/*Constructor*/
public Rectgl(int width,int height,Color c){
myDimension.width=width;
myDimension.height=height;
setColor(c);
}
/*Draw the shape*/
public void paint(Graphics g){
g.fillRect(0,0,myDimension.width,myDimension.height);
}
/*Return this shape's area*/
public double getArea(){
return myDimension.width*myDimension.height;
}
/*Return this shape's perimeter*/
public double getPerimeter(){
return(myDimension.width+myDimension.height)*2;
}
/*Return a string describing the shape*/
public String getKind(){
return"Rectangle";
}
}class Square extends Rectgl{
/*constructor*/
public Square(int side,Color c){
super(side,side,c);
}
/*Return a string describing the shape*/
public String getKind(){
return "Square";
}
}class Circle extends Pix{
private int myRadius;
/*constructor*/
public Circle(int radius,Color c){
myRadius=radius;
myDimension.width=myDimension.height=2*radius;
setColor(c);
}
/*Display the shape*/
public void paint(Graphics g){
g.fillArc(0,0,(2*myRadius),(2*myRadius),0,360);
}
/*Return this shape's area*/
public double getArea(){
return Math.Pl * (myRadius*myRadius);
}
/*Return this shape's perimeter*/
public double getPerimeter(){
return 2 * Math.Pl * myRadius;
}
public String getKind(){
return "Circle";
}
}class PixLabel extends TextArea{
public PixLabel(Pix s){
super("I am a"+s.getKind()+"\nMy perimeter is"+Double.toString(s.getPerimeter())+"\nMY area is"+Double.toString(s.getArea()),3,15,SCROLLBARS_NONE);
}
}
D:\myjava>javac PixApplet.java
PixApplet.java:105: cannot resolve symbol
symbol : variable Pl
location: class java.lang.Math
return Math.Pl * (myRadius*myRadius);
^
PixApplet.java:109: cannot resolve symbol
symbol : variable Pl
location: class java.lang.Math
return 2 * Math.Pl * myRadius;
^
Note: PixApplet.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
2 errors
import java.awt.*;
/* Pix-an applet for exploring inheritance.
*[email protected]
*version 0.1,10 April 1996
*/public class PixApplet extends Applet{
public void init(){
Rectgl r=new Rectgl(10,5,Color.red);
Square s=new Square(10,Color.blue);
Circle c=new Circle(20,Color.yellow);
Square s2=new Square(40,Color.green);
add(r);
add(s);
add(c);
add(s2);
add(new PixLabel(r));
add(new PixLabel(s));
add(new PixLabel(c));
add(new PixLabel(r));
}
}//public
abstract class Pix extends Canvas{
Dimension myDimension=new Dimension();
public void Pix(){
}
public Color getColor(){
return getForeground();
}
public void setColor(Color c){
setForeground(c);
}
public void paint(Graphics g){
}
public double getArea(){
return 0;
}
public double getPerimeter(){
return 0;
}
public String getKind(){
return "unknoen shape";
}
public Dimension preferredSize(){
return myDimension;
}
public Dimension minimumSize(){
return myDimension;
}
}class Rectgl extends Pix{
/*Constructor*/
public Rectgl(int width,int height,Color c){
myDimension.width=width;
myDimension.height=height;
setColor(c);
}
/*Draw the shape*/
public void paint(Graphics g){
g.fillRect(0,0,myDimension.width,myDimension.height);
}
/*Return this shape's area*/
public double getArea(){
return myDimension.width*myDimension.height;
}
/*Return this shape's perimeter*/
public double getPerimeter(){
return(myDimension.width+myDimension.height)*2;
}
/*Return a string describing the shape*/
public String getKind(){
return"Rectangle";
}
}class Square extends Rectgl{
/*constructor*/
public Square(int side,Color c){
super(side,side,c);
}
/*Return a string describing the shape*/
public String getKind(){
return "Square";
}
}class Circle extends Pix{
private int myRadius;
/*constructor*/
public Circle(int radius,Color c){
myRadius=radius;
myDimension.width=myDimension.height=2*radius;
setColor(c);
}
/*Display the shape*/
public void paint(Graphics g){
g.fillArc(0,0,(2*myRadius),(2*myRadius),0,360);
}
/*Return this shape's area*/
public double getArea(){
return Math.PI * (myRadius*myRadius);
}
/*Return this shape's perimeter*/
public double getPerimeter(){
return 2 * Math.PI * myRadius;
}
public String getKind(){
return "Circle";
}
}class PixLabel extends TextArea{
public PixLabel(Pix s){
super("I am a"+s.getKind()+"\nMy perimeter is"+Double.toString(s.getPerimeter())+"\nMY area is"+Double.toString(s.getArea()),3,15,SCROLLBARS_NONE);
}
}
以上是正确的程序,注意,在同一个.java 文件中只能有一个public 类,书写时注意大小写!!
其次是Math.PI, 是派,3.14那个,
在Rectgl里的Public应该是public,
另外,是你第一个程序把?第一个就抄这么复杂,
学学这个把public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello World!");
}
}
D:\myjava>javac PixApplet.java
Note: PixApplet.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
preferredSize()
着两个函数改成别的名字就行了
<applet width="128" height="128" code="PixApplet.calss">
</applet>出现:PixApplet.class notinited我在想是否是将Pix列出来,编译成类。如果是那怎样编译成类。