Why this canvas program can't work?

import java.applet.*;

import java.awt.*;

import java.awt.event.*;



public class useCanvas extends Applet {

private Ball b;

public void init()

{

setLayout(new BorderLayout());

b=new Ball();



Panel bPanel = new Panel();

bPanel.setLayout( new GridLayout(1,1) );

bPanel.add(b);

add(b, BorderLayout.WEST);

}







class Ball extends Canvas{

private Color BallColor;

public void Ball()

{

setSize(30,30);

BallColor = Color.red;

}

public void setBallColor(Color c)

{

BallColor = c;repaint();

}

public Color getBallColor()

{

return BallColor;

}

public void update(Graphics g) { paint(g); }



public void paint(Graphics g){

setBackground(Color.red);

g.setColor(BallColor);

g.fillOval(2,2,33,33);

}



}





}






Comments

  • While quickly browsing through your source, I believe that your main mistake is the constructor of the Ball-class isn't there.



    public void Ball() --> public Ball()



    If you alter this, then your code should work.


    URL:http://efgee.dwaalspoor.com

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories