?
Solved

how to create a Graphics object?

Posted on 1997-11-17
2
Medium Priority
?
196 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month15 days, 15 hours left to enroll

743 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