//TestMyPackage.javaimport java.awt.*;
import java.applet.*;import src.drawShape.drawShape;public class TestMyPackage extends Applet {

private Label lOfCalNum,lresultOfNum,lOfCalCircle;
private Label lx0,ly0,lx1,ly1;
private TextField tOfCalNum,tresultOfNum,tOfCircle;
private TextField x0,y0,x1,y1;
private Panel p1,p2;
private Panel p21,p22,p23;
private Button btn;

private drawShape draw;

public void init() {
draw=new drawShape();
    draw.setBackground(Color.green);
    draw.resize(700,250);

p1=new Panel();
p1.setBackground(Color.yellow);
tOfCalNum=new TextField(10);
lOfCalNum=new Label("    请输入需要计算Fibonacci的参数(正整数)");
p1.add(lOfCalNum);
p1.add(tOfCalNum);

p2=new Panel();
p2.setBackground(Color.lightGray);
lOfCalCircle=new Label(" 画圆的个数):");
lx0=new Label(" x0:");
ly0=new Label(" y0:");
lx1=new Label(" x1:");
ly1=new Label(" y1:");
tOfCircle=new TextField(15);
x0=new TextField(5);
y0=new TextField(5);
x1=new TextField(5);
y1=new TextField(5);
btn=new Button("显示图形");
p2.add(lOfCalCircle);
p2.add(tOfCircle);
p2.add(lx0);
p2.add(x0);
p2.add(ly0);
p2.add(y0);
p2.add(lx1);
p2.add(x1);
p2.add(ly1);
p2.add(y1);
p2.add(btn);

setLayout(new BorderLayout());
add("North",p1);
add("South",p2);
add("Center",draw);

   
    resize(700,300);
} public void paint(Graphics g) {
}

public boolean action(Event e,Object o)
{
if(e.target==tOfCalNum)
{
int i;
i=draw.getCalResult(Integer.parseInt(o.toString()));
if(i==0)
       tOfCircle.setText("please input the legal number");
    else 
       tOfCircle.setText(Integer.toString(i));
}
if(e.target==btn)
{
if(draw.setNumber(Integer.parseInt(tOfCircle.toString())))
{
draw.setRect(Integer.parseInt(x0.toString()),
             Integer.parseInt(y0.toString()),
             Integer.parseInt(x1.toString()),
             Integer.parseInt(y1.toString()));
draw.callRepaint();

}
}
return true;
}
}
//drawShape.javapackage src.drawShape;//这是我做的一个包,上面调用了import java.awt.*;
import java.applet.*;class calFibonacci
{

public void calFibonacci()
{
} public static int calculate(int i)
{
if(i<=0)
   return 0;//wrong result
else if(i==1 || i==2)
   return 1;
else 
   return calculate(i-1)+calculate(i-2);
}
}public class drawShape extends Canvas
{
private int n;//the num of the circle
private int x0,y0,x1,y1;
private int width,height;
private calFibonacci theCal;

public void drawShape()
{
x0=120;
y0=60;
x1=30;
y1=30;
                  n=0;
}

public int getCalResult(int i)
{
return theCal.calculate(i);
}

public boolean setNumber(int i)
{
if(i<n)
   return false;
else
{
n=i;
return true;
}
         
}

public int getNumber()
{
return n;
}

public void setRect(int ax,int ay,int bx,int by)
{
n=1;
x0=ax;
y0=by;
x1=ax;
y1=ay;
}

private void calRectData()
{
width=Math.abs(x1-x0);
height=Math.abs(y1-y0);

//determin the upper left point of
//the bounding rectangle
x0=Math.min(x0,x1);
y0=Math.min(y0,y1);
}

public void callRepaint()
{
repaint();
}

public void paint(Graphics g)
{
int i;
int xpot,ypot;
int radius;

g.setColor(Color.black);

calRectData();
g.drawRect(x0,y0,width,height);

//set the radius of the circles
radius=(width/n>=height/2) ? width/n:height/2; 
for(i=0;i<n;i++)
{
xpot=x0+i*width/n;
ypot=y0+height/2;
g.drawOval(xpot,ypot-radius/2,radius,radius);
}
}
}我写了一个Applet小程序,调用包中的类来实现画图.包中的类是继承Canvas的,可程序运行却怎么都不能画出图形.我估计程序根本没有调用drawShape类的paint函数,因为我有初始化的数据,至少是应该显示一个矩形框的.高手帮帮忙啊,很急的.先谢谢了.

解决方案 »

  1.   

    radius=(width/n>=height/2) ? width/n:height/2; 
    这里报错,n不能为0别的我就不知道了
      

  2.   

    哦,多谢.可我初始化的时候是1,而且我后面有方法保证n>0的
    前面有点笔误
    public boolean setNumber(int i)
    {
    if(i<n)   //这里应该是if(i<=0)
       return false;
    else
    {
    n=i;
    return true;
    }
             
    }
    前面的界面和后面的包我都分别测试过了,没问题啊,和怎么就画不出来啊?
    大家帮帮忙啊
      

  3.   

    int i;//为成员变量
    public boolean action(Event e,Object o)
    {
    if(e.target==tOfCalNum)
    {
        i=draw.getCalResult(Integer.parseInt(o.toString()));
        if(i==0)
           tOfCircle.setText("please input the legal number");
        else 
           tOfCircle.setText(Integer.toString(i));
    }
    if(e.target==btn)
    {
    if(draw.setNumber(i)
    {
    draw.setRect(Integer.parseInt(x0.toString()),
                 Integer.parseInt(y0.toString()),
                 Integer.parseInt(x1.toString()),
                 Integer.parseInt(y1.toString()));
    draw.repaint();

    }
    }
    return true;
    }
    我试着在按钮(btn)里发送消息,调试之后,我发现是Integer.parseInt(x0.toString())这句话根本没有执行.我敢断定是这里的错,因为我试着给我的自定义函数draw.setRect直接赋值,图形也画出来了.
    高手帮帮我啊,告诉我这里到底有什么错?应该怎么解决?
      

  4.   

    问题找出来了,是应该把toString()改为getText.
    因为java.awt.Component.toString()的定义是Returns a string representation of this component and its values. 而非仅仅为TextField中的值.