[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 811
  • Last Modified:

Java 2D Mouse Animation

Hello, I am doing a small mickey animation project where mickeys head is going to bounce around the panel and change color when it hits a side. I am getting the following errors.

Exception in thread "main" java.lang.NullPointerException
      at bouncingMickeyPanel.<init>(MickeyMouseScreeenSaver.java:77)
      at MickeyMouseScreeenSaver.init(MickeyMouseScreeenSaver.java:30)
      at MickeyMouseScreeenSaver.main(MickeyMouseScreeenSaver.java:23)
If anyone can see what is causing the problem I would appreciate it.


import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.awt.geom.*;
import java.util.Random;

public class MickeyMouseScreeenSaver extends JApplet {
  public static void main(String s[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Programming Assignment 9 Bouncing Mickey");
    JApplet applet = new MickeyMouseScreeenSaver();
    applet.init();
    frame.getContentPane().add(applet);
    frame.pack();
    frame.setVisible(true);
  }
 
  public void init() {
    JPanel panel = new bouncingMickeyPanel();
    getContentPane().add(panel);
  }
}

class bouncingMickeyPanel extends JPanel implements ActionListener {  

        public static final int PANEL_WIDTH = 800;
        public static final int PANEL_HEIGHT = 800;
        
      
             
        MickeyMouse mickeyMouse = new MickeyMouse(0,0,200,200);
        MickeyMouse mickeyMouse2 = new MickeyMouse(100,100,200,200);;
        
        //Variables to hold/change mickeyMouse direction
        double changeX = Math.abs(10*Math.random());
        double changeY = Math.abs(10*Math.random());
        double changeX2 = Math.abs(10*Math.random());
        double changeY2 = Math.abs(10*Math.random());
        
        private float s;
        
        //Store Colors for each mickey mouse object
        Color mColor = Color.getHSBColor( s=(float) (250*Math.random()), s=(float) (250*Math.random()), s=(float) (250*Math.random()));  
        Color mColor2 = Color.getHSBColor( s=(float) (250*Math.random()), s=(float) (250*Math.random()), s=(float) (250*Math.random()));
        
        //Create new Mickey Area Objects
        Area area1 = new Area(mickeyMouse);
        Area area2 = new Area(mickeyMouse2);
        
        //Create Affine Transform
        AffineTransform trans;
        
        
        //Check to see if paint was called
        boolean paintNow = false;
        boolean intersected = false;
        
        //Create rectangles to check if the Mickey Objects have hit the sides
        Rectangle2D top = new Rectangle2D.Double(0,0,getWidth(),1);
        Rectangle2D bottom = new Rectangle2D.Double(0,-getHeight(),getWidth(),1);
        Rectangle2D left = new Rectangle2D.Double(0,0,1,getHeight());
        Rectangle2D right = new Rectangle2D.Double(0,getWidth(),1,getHeight());
        
        public bouncingMickeyPanel(){  
          
              trans.translate(changeX, changeY);
              area1.transform(trans);
              trans.translate(changeX2, changeY2);
              area2.transform(trans);
      
        //Create The timer and start it
        Timer timer = new Timer(10,this);
        timer.start();

}
        
        public void actionPerformed(ActionEvent arg0) {
            
              if(paintNow){
                    intersected = false;
              }
             
              if(top.contains(area1.getBounds2D()) || bottom.contains(area1.getBounds2D())){
                    changeY = -changeY;
              }
              if(right.contains(area1.getBounds2D()) || left.contains(area1.getBounds2D())){
                    changeX = -changeX;
              }
              if(area1.contains(area2.getBounds2D()) || area2.contains(area1.getBounds2D())){
                    //The two Mickeys have collided
                    changeX = -changeX;
                    changeX2 = -changeX2;
                    trans.scale(1,2);
                    area1.transform(trans);
                    trans.scale(-1, 1);
              }
             
              if(top.contains(area2.getBounds2D()) || bottom.contains(area2.getBounds2D())){
                    changeY2 = -changeY2;
              }
              if(right.contains(area2.getBounds2D()) || left.contains(area2.getBounds2D())){
                    changeX2 = -changeX2;
              }
             
             
              //Randomize Colors again
              mColor = Color.getHSBColor( s=(float) (250*Math.random()), s=(float) (250*Math.random()), s=(float) (250*Math.random()));  
              mColor2 = Color.getHSBColor( s=(float) (250*Math.random()), s=(float) (250*Math.random()), s=(float) (250*Math.random()));
             
              repaint();
             
             
        }
        
        public void paintComponent(Graphics g) {
             
                  paintNow = true;
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D)g;
                g2.translate(getWidth()/2, getHeight()/2);
               
                //Only render Mickey Mouse in here, do all translations in actionPerformed
                g2.setColor(mColor);
                g2.fill(area1);
               
                g2.setColor(mColor2);
                g2.fill(area2);
               
              }
        
Here is my mickeymouse class.

import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Ellipse2D.Double;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;


public class MickeyMouse implements Shape {

      private Area mickeyMouse;
      
      //Mickey Mouse Area Object
      public MickeyMouse(int x, int y, int widthHead, int heightHead){
             mickeyMouse = new Area(new Ellipse2D.Double(x, y, widthHead, heightHead));
      //Set up the main facial parts and ears to Mickey       
             Ellipse2D face = new Ellipse2D.Double(x+widthHead/2.0,y+heightHead/2.0,widthHead,heightHead);
             Ellipse2D earR = new Ellipse2D.Double(widthHead/4.0, heightHead/4.0, widthHead/2.0, heightHead/2.0);
             Ellipse2D earL = new Ellipse2D.Double((5.0/4.0)*widthHead, y-widthHead/4.0, widthHead/2.0, heightHead/2.0);
             
             Ellipse2D mouth = new Ellipse2D.Double((x + face.getWidth()/4 + earL.getWidth()/2 - face.getWidth()/4.0), y + face.getHeight()/2 + earL.getHeight()/2.0, face.getWidth()/2, face.getWidth()/8);
             Ellipse2D nose = new Ellipse2D.Double(x + face.getWidth()/4 + earL.getWidth()/2 - face.getWidth()/16, y + face.getHeight()/2 + earL.getHeight()/5, face.getWidth()/8, face.getWidth()/8);
             Ellipse2D eyeR = new Ellipse2D.Double(widthHead, heightHead/2.0+20, widthHead/4.0, heightHead/4.0);
             Ellipse2D eyeL = new Ellipse2D.Double(widthHead*(2.0/3.0)+10, heightHead/2.0+20, widthHead/4.0, heightHead/4.0);
             Ellipse2D pupil1 = new Ellipse2D.Double(widthHead*(2.0/3.0)+20, heightHead/2.0+40, widthHead/8.0, heightHead/8.0);
             Ellipse2D pupil2 = new Ellipse2D.Double(widthHead+10, heightHead/2.0+30, widthHead/8.0, heightHead/8.0);
             
             //Combine these ellipses together to make a Mickey Mouse Head
             
             mickeyMouse.add(new Area(earL));
             mickeyMouse.add(new Area(earR));
             mickeyMouse.subtract(new Area(mouth));
             mickeyMouse.subtract(new Area(nose));
             mickeyMouse.subtract(new Area(eyeR));
             mickeyMouse.subtract(new Area(eyeL));
             mickeyMouse.add(new Area(pupil1));
             mickeyMouse.add(new Area(pupil2));
      }
      
      @Override
      public Rectangle getBounds() {
            // TODO Auto-generated method stub
            return mickeyMouse.getBounds();
      }

      @Override
      public Rectangle2D getBounds2D() {
            // TODO Auto-generated method stub
            return mickeyMouse.getBounds2D();
      }

      @Override
      public boolean contains(double x, double y) {
            // TODO Auto-generated method stub
            return mickeyMouse.contains(x, y);
      }

      @Override
      public boolean contains(Point2D p) {
            // TODO Auto-generated method stub
            return mickeyMouse.contains(p);
      }

      @Override
      public boolean intersects(double x, double y, double w, double h) {
            // TODO Auto-generated method stub
            return mickeyMouse.intersects(x, y, w, h);
      }

      @Override
      public boolean intersects(Rectangle2D r) {
            // TODO Auto-generated method stub
            return mickeyMouse.intersects(r);
      }

      @Override
      public boolean contains(double x, double y, double w, double h) {
            // TODO Auto-generated method stub
            return mickeyMouse.contains(x, y, w, h);
      }

      @Override
      public boolean contains(Rectangle2D r) {
            // TODO Auto-generated method stub
            return mickeyMouse.contains(r);
      }

      @Override
      public PathIterator getPathIterator(AffineTransform at) {
            // TODO Auto-generated method stub
            return mickeyMouse.getPathIterator(at);
      }

      @Override
      public PathIterator getPathIterator(AffineTransform at, double flatness) {
            // TODO Auto-generated method stub
            return mickeyMouse.getPathIterator(at, flatness);
      }

      
}
        


0
amhinkel
Asked:
amhinkel
  • 2
  • 2
1 Solution
 
for_yanCommented:
In bouncingMickeyPanel You have declaration AfineTransform trans;
But you never create any object for trans, but then in constructor you apply method to trans.
If trans is still null at that time, then trans.method() will cause nullpointer exception
Check if that is line 77
0
 
amhinkelAuthor Commented:
That is Line 77.
0
 
for_yanCommented:

I see no line 77, but it looks it is about in this region - so check mt comments after // bellow

  //Create Affine Transform
        AffineTransform trans; // trans is only declared
        
        
        //Check to see if paint was called
        boolean paintNow = false;
        boolean intersected = false;
        
        //Create rectangles to check if the Mickey Objects have hit the sides
        Rectangle2D top = new Rectangle2D.Double(0,0,getWidth(),1);
        Rectangle2D bottom = new Rectangle2D.Double(0,-getHeight(),getWidth(),1);
        Rectangle2D left = new Rectangle2D.Double(0,0,1,getHeight());
        Rectangle2D right = new Rectangle2D.Double(0,getWidth(),1,getHeight());
        
        public bouncingMickeyPanel(){  
          
              trans.translate(changeX, changeY);  // is trans created at this point ? id it is null, then invokation of the method will cause exception
              area1.transform(trans);

Open in new window

0
 
Pramod KumarCommented:
All works for me once done below change. I have just added one line to initiate the AffineTransform

public bouncingMickeyPanel()
    {
        trans = new AffineTransform();
        trans.translate(changeX, changeY);
        area1.transform(trans);
        trans.translate(changeX2, changeY2);
        area2.transform(trans);

        //Create The timer and start it
        Timer timer = new Timer(10, this);
        timer.start();

    }

Open in new window



And set the frame size and remove pack()
 
frame.setSize(500,400);
//frame.pack();

Open in new window

0
 
amhinkelAuthor Commented:
Thanks Much!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now