Results 1 to 5 of 5

Thread: Java Question     submit to reddit submit to twitter

  1. #1
    Banned.

    Join Date
    Jul 2005
    Posts
    5,821
    BG Level
    8
    FFXI Server
    Sylph
    WoW Realm
    Arthas

    Java Question

    The assignment:

    Create a GUI Java application that simulates the playing of the game "Rock, Paper, Scissors" between two players - the computer and a person. The application needs to use at least three buttons and one textfield. Create an inner class to respond to the pressing/clicking of a button and an anonymous inner class to respond to the closing of the GUI window.

    Before starting to design your solution to this assignment, you may want to do a little background research by do a search on the Internet with key words of “rock paper scissors or "rps championship" or "rock paper scissors rules”. Don’t get carried away with creating a "world class" program before getting the basics working correctly.
    The problem I'm having is the inner class. Its being called as static?

    Here's the source:

    Code:
    /**
    @version 04/12/2010
    @author Kyle 
    
    */
       import javax.swing.*;
       import java.awt.event.*;
        import java.awt.*;
        import java.util.*;
    
        public class BuildGUI implements ActionListener {
          JPanel score, game, text;
            JFrame frame;
          JButton btnRock, btnPaper, btnScissors;
            JTextField status;
            JButton rock, paper, scissors, reset; 
          JLabel lblPlayer, lblPlayerCount, lblComputer, lblComputerCount, lblTies, lblTiesCount;
     
           public static void main(String[] args){
             new BuildGUI();
          }
       
           public void actionPerformed(ActionEvent e){
                     
                class Rps{
                
                    //0 = rock
                    //1 = paper
                    //2 = scissors
                    
                    public int pcRoll(){
                        Random r = new Random();
                        int roll = r.nextInt(2);
                        return roll;
                    }
                    
                    // 0 = lose
                    // 1 = win
                    // 2 = tie
                    // 3 = error
                    public int compare(int player, int pc){
                        int value = 3;
                        
                        if (player == pc){
                            return 2;
                        }
                        
                        switch (player){
                            case 0: 
                                if (pc == 1) {
                                    value = 0;
                                }
                                
                                if (pc == 2) {
                                    value = 1;
                                }
                                
                                break;
                            
                            case 1:
                                if (pc == 0) {
                                    value = 1;
                                }
                                if (pc == 2){
                                    value = 0;
                                }
                                
                                break;
                            case 2:
                                if (pc == 0) { 
                                    value = 0;
                                }
                                if (pc == 1) {
                                    value = 1;
                                }
                                break;
                        }                    
                        return value;                    
                    }
                }
            
                Rps game = new Rps();
                int pc, player, returned;
              String message = "IDK BRO";
              
               if ( e.getSource() == rock ){
                    player = 0;
                }
            
                if ( e.getSource() == paper){
                    player = 1;
                } 
                
                if ( e.getSource() == rock){{
                    player = 2;
                }
                
                pc = Rps.pcRoll();
                returned = Rps.compare(player, pc);
                
               JOptionPane.showMessageDialog(null, "Player: " + player + " PC: " + pc);
          }
        }
       
           public BuildGUI(){
                 score                    =    new JPanel(new FlowLayout());
                game                    =     new JPanel(new FlowLayout());    
                text                    =     new JPanel(new FlowLayout());        
                frame                    =     new JFrame();
                
                frame.setLayout(new FlowLayout());
                
             lblPlayer             =    new JLabel("Player: ");
                lblPlayerCount        =    new JLabel("0");
                lblComputer            =    new JLabel("Computer: ");
                lblComputerCount    =     new JLabel("0");
                  lblTies                 =     new JLabel("Ties: ");
                lblTiesCount        =    new JLabel("0");   
                
             score.add(lblPlayer);
             score.add(lblPlayerCount);
                score.add(lblComputer);
                score.add(lblComputerCount);
                score.add(lblTies);
                score.add(lblTiesCount);
                
                status = new JTextField("Make a Move!", 15);
                status.setEditable(false);
                text.add(status);
                
                rock                    =    new JButton("Rock");
                paper                    =    new JButton("Paper");
                scissors                =    new JButton("Scissors"); 
                
                game.add(rock);
                game.add(paper);
                game.add(scissors);
                
                frame.add(score);
                frame.add(status);
                frame.add(game);            
          
          
             rock.addActionListener( this );
             paper.addActionListener( this );
                scissors.addActionListener( this );
          
             frame.setSize(250,155);
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setLocationRelativeTo(null);
             frame.setTitle("Rock Paper Scissors - Kyle");
             frame.setVisible(true);
          }
            
       }
    The errors I'm getting:

    BuildGUI.java:99: non-static method pcRoll() cannot be referenced from a static context
    pc = Rps.pcRoll();
    ^
    BuildGUI.java:100: non-static method compare(int,int) cannot be referenced from a static context
    returned = Rps.compare(player, pc);
    Any Ideas?

  2. #2
    Sea Torques
    Join Date
    Jul 2009
    Posts
    608
    BG Level
    5

    I opened the thread thinking it was about this kind of Java

  3. #3
    Old Merits
    Join Date
    Mar 2005
    Posts
    1,156
    BG Level
    6

    change Rps. to game. , the class isn't static

  4. #4
    Banned.

    Join Date
    Jul 2005
    Posts
    5,821
    BG Level
    8
    FFXI Server
    Sylph
    WoW Realm
    Arthas

    Ah, that was a retarded error on my part. The other problem I'm having is the inner class for the System.exit(0); shit. Where do I go about that? Any clue :<?

  5. #5
    netz
    Guest

    Quote Originally Posted by Senoska View Post
    Ah, that was a retarded error on my part. The other problem I'm having is the inner class for the System.exit(0); shit. Where do I go about that? Any clue :<?
    You kind of ignored the directions given to you for doing an inner class by making your whole class an ActionListener and dumping your inner class in the actionPerformed() method. I mean, it works, but it's not a terribly good solution.

    Maybe do this instead:

    Code:
    public class Rps implements ActionListener {
       // stuff
    
       public void actionPerformed(ActionEvent e) {
       // more stuff.
       }
    }
    and then doing later on:

    Code:
     Rps rpsgame = new Rps();
     rock.addActionListener(rpsgame);
     paper.addActionListener(rpsgame);
     scissors.addActionListener(rpsgame);
    For your anonymous inner class, well... look here. Basic idea is that you pass this as your argument when adding a new listener to the JFrame that you made.

    Look here as well to figure out what listeners correspond to what.

    Other tip: Swing also makes available these things called Adapters that do not require you to implement all of the abstract methods that a regular Listener would, so they're perfect for short anonymous inner classes. So, instead of using WindowListener and having to implement all of the methods that you see on that API list, you use WindowAdapter and implement just the windowClosing() method.

    Sample for doing anonymous inner class:

    Code:
    MyThing stuff = new MyThing();
    
    stuff.addEventListener(new MyAdapter() {
        @Override
        public void somethingHappened(Event e) {
           // do stuff
        }
    });

Similar Threads

  1. Java Question
    By bori in forum General Discussion
    Replies: 4
    Last Post: 2010-01-28, 08:23