Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Detecting collisions

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Detecting collisions

    I am trying to detect the collision between a moving Jbutton and stationary Jbuttons using Rectangle Intersection. When I run the program I am getting a NullPointerException on line 'msg1.setBounds(new Rectangle(320,50,400,50));'. Any suggestions on how to fix the problem? Thanks!


    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
    public class gamePanel01 extends JPanel implements KeyListener
    {
     
        character ch1 = new character("Hero");
     
        //Global Variables
        JButton blocker01;
        JButton blocker02;
        JButton blocker03;
        JButton blocker04;
        JButton blocker05;
        JLabel msg1;
        JLabel msg2;
     
        //Character movement Variables
        int x=100;
        int y=100;
     
        //Image for background of gamePanel01
        ClassLoader cl = this.getClass().getClassLoader();    
        Image gamePanelBG = new ImageIcon(cl.getResource("images/gamePanel01.jpg")).getImage();
        ImageIcon PSUBlocker = new ImageIcon(cl.getResource("images/PSU_Helmet_Blocker.gif"));
        ImageIcon PSULionRight = new ImageIcon(cl.getResource("images/NittanyLion_Mini_Right.png"));
        ImageIcon PSULionLeft = new ImageIcon(cl.getResource("images/NittanyLion_Mini_Left.png"));
     
        public gamePanel01()
        {
        super();
        setLayout(null);
     
        add(ch1);    
        ch1.setIcon(PSULionRight);
        ch1.setBounds(new Rectangle(x,y,45,45));
        ch1.addKeyListener(this);
     
        ch1.setFocusPainted(false);
        ch1.setContentAreaFilled(false);
        ch1.setBorderPainted(false);
     
     
        //Blocker Buttons created/added
        blocker01 = new JButton();
        blocker02 = new JButton();
        blocker03 = new JButton();
        blocker04 = new JButton();
        blocker05 = new JButton();
     
        add(blocker01);
        add(blocker02);
        add(blocker03);
        add(blocker04);
        add(blocker05);
        //Blocker Button Placement-------------------------------------------------
        //**********Rectangle Parameters (x,y,length,height)
        blocker01.setBounds(new Rectangle(250,150,45,45));
        blocker02.setBounds(new Rectangle(250,195,45,45));
        blocker03.setBounds(new Rectangle(400,240,45,45));
        blocker04.setBounds(new Rectangle(400,285,45,45));
        blocker05.setBounds(new Rectangle(400,330,45,45));
     
     
        //Set Images to Buttons and Button Parameters
        blocker01.setIcon(PSUBlocker);
        blocker01.setFocusPainted(false);
        blocker01.setContentAreaFilled(false);
        blocker01.setBorderPainted(false);
     
        blocker02.setIcon(PSUBlocker);
        blocker02.setFocusPainted(false);
        blocker02.setContentAreaFilled(false);
        blocker02.setBorderPainted(false);
     
        blocker03.setIcon(PSUBlocker);
        blocker03.setFocusPainted(false);
        blocker03.setContentAreaFilled(false);
        blocker03.setBorderPainted(false);
     
        blocker04.setIcon(PSUBlocker);
        blocker04.setFocusPainted(false);
        blocker04.setContentAreaFilled(false);
        blocker04.setBorderPainted(false);
     
        blocker05.setIcon(PSUBlocker);
        blocker05.setFocusPainted(false);
        blocker05.setContentAreaFilled(false);
        blocker05.setBorderPainted(false);
     
        msg1.setBounds(new Rectangle(320,50,400,50));
        msg2.setBounds(new Rectangle(320,280,400,50));
        add(msg1);
        add(msg2);
     
         if (ch1.intersects(blocker01))
        {msg1.setText("ch1 and blocker01 intersect");}
        else
        {msg1.setText("ch1 and blocker01 do not intersect");}
        if (ch1.intersects(blocker02))
        {msg2.setText("ch1 and blocker02 intersect");}
        else
        {msg2.setText("ch1 and blocker02 do not intersect");}
     
        }
        @Override
       public void paintComponent(Graphics g)
       {
       super.paintComponent(g);
       g.drawImage(gamePanelBG, 0, 0, this);
        }
     
       public void keyPressed(KeyEvent ev)
      {
          int k= ev.getKeyCode();      
          if (k==KeyEvent.VK_LEFT){
              x=x-5;
              ch1.setIcon(PSULionLeft);
          }
          if(k==KeyEvent.VK_RIGHT) {
              x=x+5;
              ch1.setIcon(PSULionRight);
          }
          if(k==KeyEvent.VK_UP) {y=y-5;}
          if(k==KeyEvent.VK_DOWN) {y=y+5;}
          ch1.setBounds(new Rectangle(x,y,45,45));
          //Rectangle r1=new Rectangle(x,y,45,45);
          //setBounds(r1);
     
      }
       public void keyReleased(KeyEvent evt) {  }
     
       public void keyTyped(KeyEvent evt) {  }
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Detecting collisions

    getting a NullPointerException on line 'msg1.setBounds(new Rectangle(320,50,400,50)
    What is the value of the variable: msg1? That looks like the only source of a NPE on that line.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] need help with basc Collisions
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 22nd, 2012, 03:40 PM
  2. [SOLVED] detecting pc type
    By usama8800 in forum Java Theory & Questions
    Replies: 1
    Last Post: June 26th, 2012, 02:17 PM
  3. hashing / collisions
    By Herah in forum Java Theory & Questions
    Replies: 1
    Last Post: November 29th, 2011, 11:16 PM
  4. Collisions... working but not.
    By Skyhigh32 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 24th, 2011, 08:05 PM
  5. Particle collisions...
    By Macattack1459 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 3rd, 2011, 09:44 PM