title: How to check if Button Color is same as square color in java Graphics and increase the points
body: <p>I am trying to create a game where there a 2 buttons <strong>red</strong> and <strong>blue</strong>. The square shape randomly picks a color from Color array. Thus if we press any of the 2 buttons and the color of that button is same as the random color in square we score a point. Here is my code-</p>
  
  <pre><code>import java.awt.Button;
  import java.awt.Color;
  import java.awt.Frame;
  import java.awt.Graphics;
  import java.awt.Graphics2D;
  import java.awt.TextArea;
  import java.awt.TextField;
  import java.awt.event.*;
  import java.util.Random;
  
  import javax.swing.JLabel;
  import javax.swing.JTextArea;  
  class apple1 extends Frame implements ActionListener{
      private Button b;
  
      public apple1(){  
  
  
          tf = new TextField("Points: ");
          tf.setBounds(10, 30, 140, 20);
  
          //create components   
          b=new Button("RED");
          b.setBackground(Color.red);
          b.setBounds(80,260,80,30); 
  
  
          Button b2=new Button("BLUE");
          b2.setBackground(Color.blue);
          b2.setBounds(180,260,80,30); 
  
          //register listener
          b.addActionListener(this);//passing current instance  
  
          //add components and set size, layout and visibility  
          add(tf);add(b);add(b2);  
          setSize(600,600);  
          setLayout(null);
          setVisible(true);
          }
  
  TextField tf;
  JTextArea lbl;
      public void paint(Graphics g) {
          Graphics2D g2 = (Graphics2D) g;
          Color[] clrs = {Color.red,Color.blue};
          Random rand = new Random();
          g2.setColor(clrs[rand.nextInt(clrs.length)]);
          g2.fillRect (60, 50, 200, 200);
          if(g2.getColor().equals(b.getBackground())){
              int count = 0;
              tf.setText(String.valueOf(count++));
          }
        }
  
  public void actionPerformed(ActionEvent e){
      repaint();
  }  
  public static void main(String args[]){  
  new apple1();  
  }  
  }  
  </code></pre>
  
  <p>Here is the error I am getting in eclipse:
  Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
      at apple1.paint(apple1.java:53)
      at sun.awt.RepaintArea.paintComponent(RepaintArea.java:264)
      at sun.lwawt.LWRepaintArea.paintComponent(LWRepaintArea.java:59)
      at sun.awt.RepaintArea.paint(RepaintArea.java:240)
      at sun.lwawt.LWComponentPeer.handleJavaPaintEvent(LWComponentPeer.java:1314)
      at sun.lwawt.LWComponentPeer.handleEvent(LWComponentPeer.java:1198)
      at java.awt.Component.dispatchEventImpl(Component.java:4965)
      at java.awt.Container.dispatchEventImpl(Container.java:2294)
      at java.awt.Window.dispatchEventImpl(Window.java:2746)
      at java.awt.Component.dispatchEvent(Component.java:4711)
      at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
      at java.awt.EventQueue.access$500(EventQueue.java:97)
      at java.awt.EventQueue$3.run(EventQueue.java:709)
      at java.awt.EventQueue$3.run(EventQueue.java:703)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
      at java.awt.EventQueue$4.run(EventQueue.java:731)
      at java.awt.EventQueue$4.run(EventQueue.java:729)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)</p>
  
  <p><a href="https://i.stack.imgur.com/R6DjU.png" rel="nofollow noreferrer">Random Color Color Compare</a></p>
  
  <p>I am in the initial stage of developing this so called games but I think i am not able to get the g2 Graphics variable called inside b button's actionPerformed() method. Please help.</p>