Solved

how to create a Graphics object?

Posted on 1997-11-17
2
176 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
stackato and cloud 4 87
Spring Framework HTTPSession management 1 36
web application structure 18 77
printf performancy 11 45
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

825 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