d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Playing Around > Cant Figure Out Why This Doesnt Work
Add Reply New Topic New Poll
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Jan 9 2016 04:10pm
so, i'm trying to learn JFrames and actionlistener cuz i wanted to make a dumb little game

but it doesnt work, i think it's something fundamental like with how i'm declaring static classes and variables and stuff idk
when i test it the "rock" button seems to work, paper will break it and make a blank frame, scissors, lizard, and spock, do nothing at all.

idk been staring at it for a while so maybe you know what i need to work on

Code
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.*;

public class SwitchPractice2 {

static JPanel panel= new JPanel();
static JFrame frame = new JFrame ("Test");
static JPanel gamePanel = new JPanel();
static JPanel postPanel = new JPanel();
static int f=0, a=0, b=0, d=0;
static String tagLine, winnerLine;
public static void main (String[] args){
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

panel = new JPanel();
JButton button = new JButton("play");
panel.add(button);
button.addActionListener(new PlayAction());
JLabel label=new JLabel("your current record is"+a+"\\"+b+"\\"+d);
panel.add(label);

gamePanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//frame.getContentPane().add(panel, BorderLayout.NORTH);

c.gridx=1;
c.gridy=2;
JButton button1=new JButton("Paper");
button1.addActionListener(new PaperAction());
gamePanel.add(button1,c);

c.gridx=1;
c.gridy=3;
JButton button2=new JButton("Rock");
button2.addActionListener(new RockAction());
gamePanel.add(button2,c);

c.gridx=1;
c.gridy=4;
JButton button3=new JButton("Scissors");
button1.addActionListener(new ScissorsAction());
gamePanel.add(button3,c);

c.gridx=1;
c.gridy=5;
JButton button4=new JButton("Lizard");
button1.addActionListener(new LizardAction());
gamePanel.add(button4,c);

c.gridx=1;
c.gridy=6;
JButton button5=new JButton("Spock");
button1.addActionListener(new SpockAction());
gamePanel.add(button5,c);

c.gridx=1;
c.gridy=1;
JLabel label0 = new JLabel ("Try to defeat the computer");
gamePanel.add(label0, c);

c.gridx=1;
c.gridy=0;
JButton button0=new JButton("Back to Main Menu");
button0.addActionListener(new QuitAction());
gamePanel.add(button0,c);
frame.add(panel);
frame.setVisible(true);
}


private static void postScreen(){
frame.setVisible(false);
postPanel.removeAll();
frame.remove(gamePanel);
postPanel.removeAll();
postPanel = new JPanel(new GridBagLayout());
GridBagConstraints g = new GridBagConstraints();
g.gridx=0;
g.gridy=0;
JButton playAgain = new JButton("play again");
playAgain.addActionListener(new PlayAction());
postPanel.add(playAgain);
g.gridy=1;
JButton quitButton = new JButton("quit");
quitButton.addActionListener(new QuitAction());
postPanel.add(quitButton, g);

JLabel label1 = new JLabel(tagLine);
JLabel label2 = new JLabel(winnerLine);
JLabel label3 = new JLabel("win\\loss\\tie is: "+ a + "\\" + b + "\\" + d);
g.gridy=2;
postPanel.add(label1,g);
g.gridy=3;
postPanel.add(label2,g);
g.gridy=4;
postPanel.add(label3,g);

frame.add(postPanel);
frame.setVisible(true);
}

public static class PlayAction implements ActionListener{
public void actionPerformed (ActionEvent y){
frame.setVisible(false);
frame.remove(panel);
frame.remove(postPanel);
frame.add(gamePanel);
frame.setVisible(true);
}
}

public static class QuitAction implements ActionListener{
public void actionPerformed (ActionEvent j){
frame.setVisible(false);
frame.remove(postPanel);
frame.remove(gamePanel);
frame.add(panel);
frame.setVisible(true);
}
}


public static class RockAction implements ActionListener{
public void actionPerformed (ActionEvent k){
playGame(0);
postScreen();
frame.setVisible(true);

}

}

public static class PaperAction implements ActionListener{
public void actionPerformed (ActionEvent l){
playGame(1);
postScreen();
frame.setVisible(true);

}

}

public static class ScissorsAction implements ActionListener{
public void actionPerformed (ActionEvent m){
playGame(2);
postScreen();
frame.setVisible(true);

}

}
public static class LizardAction implements ActionListener{
public void actionPerformed (ActionEvent n){
playGame(3);
postScreen();
frame.setVisible(true);

}

}
public static class SpockAction implements ActionListener{
public void actionPerformed (ActionEvent o){
playGame(4);
postScreen();
frame.setVisible(true);

}

}



public static void playGame(int choice){
//will compare user choice to computer and give an integer back that will indicate win/loss/tie and situation

int computerGuess=(int)(Math.random()*5);
//what happened
switch (choice){
case 0: switch (computerGuess){
case 0: f=1;break;
case 1: f=2;break;
case 2: f=3;break;
case 3: f=4;break;
case 4: f=5;break;
};break;
case 1:switch (computerGuess){
case 0: f=6;break;
case 1: f=7;break;
case 2: f=8;break;
case 3: f=9;break;
case 4: f=10;break;
};break;

case 2:switch (computerGuess){
case 0: f=11;break;
case 1: f=12;break;
case 2: f=13;break;
case 3: f=14;break;
case 4: f=15;break;
};break;

case 3:switch (computerGuess){
case 0: f=16;break;
case 1: f=17;break;
case 2: f=18;break;
case 3: f=19;break;
case 4: f=20;break;
};break;

case 4:switch (computerGuess){
case 0: f=21;break;
case 1: f=22;break;
case 2: f=23;break;
case 3: f=24;break;
case 4: f=25;break;
};break;
}

switch(f){
case 1:d++;winnerLine="tie";tagLine="The unmoved Movers";break;
case 2:b++;winnerLine="computer wins :(";tagLine="Paper covers Rock";break;
case 3:a++;winnerLine="user wins!";tagLine="Rock crushes Scissors";break;
case 4:a++;winnerLine="user wins!";tagLine="Rock crushes Lizard";break;
case 5:b++;winnerLine="computer wins :(";tagLine="Spock vaporizes Rock";break;
case 6:a++;winnerLine="user wins!";tagLine="Paper covers Rock";break;
case 7:d++;winnerLine="tie";tagLine="Paper floating about";break;
case 8:b++;winnerLine="computer wins :(";tagLine="Scissors cuts Paper";break;
case 9:b++;winnerLine="computer wins :(";tagLine="Lizard eats Paper";break;
case 10:a++;winnerLine="user wins!";tagLine="Paper disproves Spock";break;
case 11:b++;winnerLine="computer wins :(";tagLine="Rock crushes Scissors";break;
case 12:a++;winnerLine="user wins!";tagLine="Scissors cuts Paper";break;
case 13:d++;winnerLine="tie";tagLine="Edward";break;
case 14:a++;winnerLine="user wins!";tagLine="Scissors decapitate Lizard";break;
case 15:b++;winnerLine="computer wins :(";tagLine="Spock smashes Scissors";break;
case 16:b++;winnerLine="computer wins :(";tagLine="Rock crushes Lizard";break;
case 17:a++;winnerLine="user wins!";tagLine="Lizard eats Paper";break;
case 18:b++;winnerLine="computer wins :(";tagLine="Scissors decapitate Lizard";break;
case 19:d++;winnerLine="tie";tagLine="Get a room";break;
case 20:a++;winnerLine="user wins!"; tagLine="Lizard poisons Spock";break;
case 21:a++;winnerLine="user wins!";tagLine="Spock vaporizes Rock";break;
case 22:b++;winnerLine="computer wins :("; tagLine="Paper disproves Spock";break;
case 23:a++;winnerLine="user wins!";tagLine="Spock smashes Scissors";break;
case 24:b++;winnerLine="computer wins :(";tagLine="lizard poisons spock";break;
case 25: d++;winnerLine="tie"; tagLine="2 Spocks 1 Nimoy";break;

}

}
}
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Jan 12 2016 01:12pm
Huh, thought someone would respond, I broke it all up into separate classes, but the scissors lizard and Spock buttons still do nothing
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jan 12 2016 03:41pm
If I had to guess, this is your problem:
button4=new JButton("Lizard");
button1.addActionListener(new LizardAction());

I'm not interested in running/debugging logic errors, especially the longer it gets. If I don't see the problem at a glance, I'll ask for your logging to trace it. If the problem I highlighted is the issue, then it would have been immediately obvious if you ran it through a debugger or used logging. I suggest you look into both for the future

This post was edited by carteblanche on Jan 12 2016 03:45pm
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Jan 12 2016 07:40pm
Quote (carteblanche @ Jan 12 2016 03:41pm)
If I had to guess, this is your problem:
button4=new JButton("Lizard");
button1.addActionListener(new LizardAction());

I'm not interested in running/debugging logic errors, especially the longer it gets. If I don't see the problem at a glance, I'll ask for your logging to trace it. If the problem I highlighted is the issue, then it would have been immediately obvious if you ran it through a debugger or used logging. I suggest you look into both for the future


ya, that would make sense, i started over from scratch and it worked fine i'll have to figure out how debugger works in eclipse

e. i have tried the debugger a couple times but i dont understand it, i saw it used once and it seemed pretty awesome, step step stepping but i havent been able to do it myself yet, maybe i'll practice on that now thanks

This post was edited by Ideophobe on Jan 12 2016 07:43pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jan 12 2016 07:51pm
Quote (Ideophobe @ Jan 12 2016 08:40pm)
ya, that would make sense, i started over from scratch and it worked fine i'll have to figure out how debugger works in eclipse

e. i have tried the debugger a couple times but i dont understand it, i saw it used once and it seemed pretty awesome, step step stepping but i havent been able to do it myself yet, maybe i'll practice on that now thanks


i haven't used the debugger in years since i prefer logging now. but the gist of it is you can add a break point to a particular section in code. when that line gets executed, your program will pause while you can see every variable's value. then you can use the "step" functions to move line by line. if you had a breakpoint on your lizard's action performed, you would have noticed it didn't execute.
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Jan 13 2016 11:27am
Quote (carteblanche @ Jan 12 2016 07:51pm)
i haven't used the debugger in years since i prefer logging now. but the gist of it is you can add a break point to a particular section in code. when that line gets executed, your program will pause while you can see every variable's value. then you can use the "step" functions to move line by line. if you had a breakpoint on your lizard's action performed, you would have noticed it didn't execute.


ya, i'm just not sure where all the info is located, havent been doing this very long as i'm sure u can tell. i'm not sure what logging is at all tbh, maybe next break from school, i go back monday and i havent prepared at all
Member
Posts: 9,834
Joined: Nov 13 2007
Gold: 771.00
Jan 18 2016 08:13am
Quote (Ideophobe @ Jan 13 2016 05:27pm)
ya, i'm just not sure where all the info is located, havent been doing this very long as i'm sure u can tell. i'm not sure what logging is at all tbh, maybe next break from school, i go back monday and i havent prepared at all


console logging :). A great way to make sure your individual code snippets, methods / functions will do what you intend for them to do. Since you made a GUI you can just System.out.println(); put your method calls or whatever you want to execute within the params of the println statement to make sure it returns what you want. In the future when handling exceptions the Logger class and printStackTrace() func will be very useful tools!

edit -- grammar fail :|

This post was edited by Petrusk on Jan 18 2016 08:13am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll