0

I am currently working on a rock papper scissors game. Very early into the coding I have encountered an issue when I can't get the code to repaint something new when a button is pressed despite ofcourse the button is working and registered by the code. The buttons do work as it does printout what is being pressed and also the value of y also changes so the code works somewhat, but it is not doing it properly.

public class RockPaperScissors implements ActionListener {
 JButton rock =new JButton("Select rock");  
 JButton paper =new JButton("Select paper");  
 JButton scissors =new JButton("Select scissors");  


public void frame() {
     Gui gui = new Gui();  
     JFrame b = new JFrame("Rock paper scissors");
     rock.setBounds(150,100,120,30);  
     paper.setBounds(350,100,120,30);  
     scissors.setBounds(550,100,120,30);
     b.setSize(905,705);
     b.setLocation(300,60);
     b.setResizable(false);
     b.setVisible(true);
     b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
     b.setResizable(false);
     b.setVisible(true);
     b.add(rock);
     b.add(paper);
     b.add(scissors);
     rock.addActionListener(this);
     paper.addActionListener(this);
     scissors.addActionListener(this);
     b.add(gui);

}

public static  void main(String[] args) {
    RockPaperScissors start = new RockPaperScissors();
    start.frame();



}

@Override
  public void actionPerformed(ActionEvent e) {
     Gui selection = new Gui();  
     
    if (e.getSource() == rock){
        selection.selector("rock");
        
    } else if (e.getSource() == paper) {
        selection.selector("paper");
        
    } else if (e.getSource() == scissors) {
        selection.selector("scissors");
    }       
}

And this is the Gui class meant to of course handle the gui right now task is just to draw over different coloured squares when the correct button is pressed, but nothing happens, I suspect it might be an obvious solution I am missing.

public class Gui extends JPanel {

   private int y;
   public void paint(Graphics g) {
        g.setColor(Color.white);
        g.fillRect(50, 300, 300, 300);
        g.setColor(Color.white);
        g.fillRect(550, 300, 300, 300);
        


        
        if (y == 1) {
            g.setColor(Color.blue);
            g.fillRect(50, 300, 300, 300);
        }
        if (y == 2) {
            g.setColor(Color.black);
            g.fillRect(50, 300, 300, 300);
        }
        if (y == 3) {
            g.setColor(Color.yellow);
            g.fillRect(50, 300, 300, 300);

        }
        
       }
   

   public void selector(String x){
        if (x == "paper"){
            y = 1;
              repaint();

            System.out.println("paper" + y);

        } else if (x == "rock") {
            y = 2;
              repaint();

            System.out.println("rock" + y);

        } else if (x == "scissors") {
            y = 3;
              repaint();

            System.out.println("scissors" + y);

        }   
       
   }
}
jon doe
  • 37
  • 4
  • 2
    Related but not sure if duplicate: [How do I compare Strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – maloomeister Jun 30 '21 at 12:23
  • Also worth noting in addition to what is already covered in the answer: There is no need to call `setVisible(true)` on the `JFrame` multiple times. This should be done **once** as the last call when all components are added to the frame. – maloomeister Jun 30 '21 at 12:28
  • Note that answer is a community wiki because I'm just too lazy to search for the duplicates which I *know* are out there – Hovercraft Full Of Eels Jun 30 '21 at 12:33

1 Answers1

4

One problem is here:

public void actionPerformed(ActionEvent e) {
    Gui selection = new Gui();

    //....

    selection.selector("rock");

Yes, you are changing the state of a Gui instance, but it is the wrong instance. You already have a Gui instance that has been displayed, and that is the instance whose state should be changed. You should place that instance in its own field, not bury it within the frame method.

So change

public class RockPaperScissors implements ActionListener {
    JButton rock =new JButton("Select rock");  
    JButton paper =new JButton("Select paper");  
    JButton scissors =new JButton("Select scissors");  


    public void frame() {
         Gui gui = new Gui(); 

to

public class RockPaperScissors implements ActionListener {
    private JButton rock =new JButton("Select rock");  
    private JButton paper =new JButton("Select paper");  
    private JButton scissors =new JButton("Select scissors");  
    private Gui gui = new Gui();      

    public void frame() {
         // Gui gui = new Gui(); 

and change the state of the gui field

Another problem: Don't compare Strings using == or !=. Use the equals(...) or the equalsIgnoreCase(...) method instead. Understand that == checks if the two object references are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Ah yes of course I don't why I didn't just attempt to place the instance and it should have been something I would have seen instantly. But many thanks now back to the issue with buttons appearing in the corner when pressed but that is an issue for another time. – jon doe Jun 30 '21 at 13:06