Search code examples
javarandomgraphics2d

How to check if Button Color is same as square color in java Graphics and increase the points


I am trying to create a game where there a 2 buttons red and blue. 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-

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();  
}  
}  

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)

Random Color Color Compare

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.


Solution

  • You should remove declarations from the constructor:

    class apple1 extends Frame implements ActionListener {
    
    private Button b;
    
    TextField tf;
    JTextArea lbl;
    int count = 0;
    
    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);
    }
    
    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())) {
            tf.setText(String.valueOf(count++));
        }
    }
    
    public void actionPerformed(ActionEvent e) {
        repaint();
    }
    
    public static void main(String args[]) {
        new apple1();
    }
    }