Solved

how to create a Graphics object?

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 64
Way to decrease size of apk file 9 112
servlet example 11 59
Setup GlassFish 4 37
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…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

730 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