Solved

how to create a Graphics object?

Posted on 1997-11-17
2
167 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:kunala
2 Comments
 
LVL 4

Accepted Solution

by:
rembo earned 0 total points
ID: 1230834

You never created the Canvas you are trying to
do a getGraphics() on.  Canvas myfan = new Canvas();

should fix your NullPointer problem.

0
 

Author Comment

by:kunala
ID: 1230835
Thank you
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now