Solved

how to create a Graphics object?

Posted on 1997-11-17
2
158 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
Comment Utility

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
Comment Utility
Thank you
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
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 tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

762 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

11 Experts available now in Live!

Get 1:1 Help Now