kunala
asked on
how to create a Graphics object?
I am trying to create a small animation applet which requires to generate a Graphics object dynamically. I am using getGraphics() method on a Canvas object and then painting on that but when I run my program it says NullPointerException.
I don't know why the method getGraphics() is not returning any Graphics object. A part of my code is below.
class Fan extends Thread
{
private int r,x,y,deg;
private Canvas myfan;
public boolean direction;
Fan(int new_r, int new_x, int new_y, int new_deg, Canvas C)
{
this.r=new_r;
this.x=new_x;
this.y=new_y;
this.deg=new_deg;
this.myfan=C;
this.direction = true;
}// constructor
public void draw()
{
int rx, ry;
Graphics g = myfan.getGraphics();
if (myfan.getGraphics()==null )
System.out.println("error" );
rx=(int) (r*Math.cos(deg));
ry=(int) (r*Math.sin(deg));
g.drawLine(x-rx,y-ry,x+rx, y+ry);
g.fillOval(x-rx-3,y-ry-3,6 ,6);
g.fillOval(x-10,y-10,20,20 );
}
I appreciate your help.
Thank you,
Kunala
I don't know why the method getGraphics() is not returning any Graphics object. A part of my code is below.
class Fan extends Thread
{
private int r,x,y,deg;
private Canvas myfan;
public boolean direction;
Fan(int new_r, int new_x, int new_y, int new_deg, Canvas C)
{
this.r=new_r;
this.x=new_x;
this.y=new_y;
this.deg=new_deg;
this.myfan=C;
this.direction = true;
}// constructor
public void draw()
{
int rx, ry;
Graphics g = myfan.getGraphics();
if (myfan.getGraphics()==null
System.out.println("error"
rx=(int) (r*Math.cos(deg));
ry=(int) (r*Math.sin(deg));
g.drawLine(x-rx,y-ry,x+rx,
g.fillOval(x-rx-3,y-ry-3,6
g.fillOval(x-10,y-10,20,20
}
I appreciate your help.
Thank you,
Kunala
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER