Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Directions Buttons needs picture

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Directions Buttons needs picture

    Hi everyone i just recently finished creating a rock paper scissors program but i can figure out how to get my directions image to pop up when i click my directions button. Before i just had tect but now i want to add an image and remove the text.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    class RPSpanel extends JPanel implements ActionListener {
     
        private ImageIcon rockImg = new ImageIcon("Rock.jpg");
        private ImageIcon paperImg = new ImageIcon("Paper.jpg");
        private ImageIcon scissorsImg = new ImageIcon("Scissors.jpg");
        private ImageIcon directionsImg = new ImageIcon ("Rock_paper_scissors.jpg");
        private ImageIcon exitImg = new ImageIcon("Exit1.jpg");
        private JTextField userTxt, compTxt, winsTxt, lossesTxt, tiesTxt, outComeTxt;
        private JLabel userLbl, compLbl, winsLbl, lossesLbl, tiesLbl, outComeLbl;
        private JButton rockBtn, paperBtn, scissorsBtn, exitBtn, dirBtn;
        private JTextArea outputArea;
     
        final static int ROCK = 1;
        final static int PAPER = 2;
        final static int SCISSORS = 3;// class variables- will exist for duration of
        // program
     
        static int wins = 0;// also class variables- starts count as 0
        static int losses = 0;
        static int ties = 0;
        static int userChoice;
        static int computerGuess;
     
        public RPSpanel() {
            // display panel
            JPanel displayPanel = new JPanel();
            displayPanel.setLayout(new GridLayout(6, 2));
     
            new Die(3);
            Font ssBold16 = new Font("SansSerif", Font.BOLD, 16);
     
            userLbl = new JLabel(" User Chose: ");
            userLbl.setFont(ssBold16);
            displayPanel.add(userLbl);
            userTxt = new JTextField(11);
            userTxt.setFont(ssBold16);
            displayPanel.add(userTxt);
     
            compLbl = new JLabel(" Computer Chose: ");
            compLbl.setFont(ssBold16);
            displayPanel.add(compLbl);
            compTxt = new JTextField(11);
            compTxt.setFont(ssBold16);
            displayPanel.add(compTxt);
     
            outComeLbl = new JLabel(" Results: ");
            outComeLbl.setFont(ssBold16);
            displayPanel.add(outComeLbl);
            outComeTxt = new JTextField(11);
            outComeTxt.setFont(ssBold16);
            displayPanel.add(outComeTxt);
     
            winsLbl = new JLabel(" Wins: ");
            winsLbl.setFont(ssBold16);
            displayPanel.add(winsLbl);
            winsTxt = new JTextField(11);
            winsTxt.setFont(ssBold16);
            displayPanel.add(winsTxt);
     
            lossesLbl = new JLabel(" Losses: ");
            lossesLbl.setFont(ssBold16);
            displayPanel.add(lossesLbl);
            lossesTxt = new JTextField(11);
            lossesTxt.setFont(ssBold16);
            displayPanel.add(lossesTxt);
     
            tiesLbl = new JLabel(" Ties: ");
            tiesLbl.setFont(ssBold16);
            displayPanel.add(tiesLbl);
            tiesTxt = new JTextField(11);
            tiesTxt.setFont(ssBold16);
            displayPanel.add(tiesTxt);
     
            // buttons
            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
     
            // Directions button
            dirBtn = new JButton("Directions");
            dirBtn.addActionListener(this);
            buttonPanel.add(dirBtn);
     
            // rock button
            rockBtn = new JButton("Rock", rockImg);
            rockBtn.addActionListener(this);
            paperBtn = new JButton("Paper", paperImg);
            paperBtn.addActionListener(this);
            scissorsBtn = new JButton("Scissors", scissorsImg);
            scissorsBtn.addActionListener(this);
            // exit button
            exitBtn = new JButton(exitImg);
            exitBtn.addActionListener(this);
            buttonPanel.add(rockBtn);
            buttonPanel.add(paperBtn);
            buttonPanel.add(scissorsBtn);
            buttonPanel.add(exitBtn);
     
            // add panels to main panel
            this.setLayout(new BorderLayout());
            this.add(displayPanel, BorderLayout.CENTER);
            this.add(buttonPanel, BorderLayout.SOUTH);
        }// constructor
     
        public void actionPerformed(ActionEvent e) {
            Die hal = new Die(3);
            Object source = e.getSource();
            if (source == exitBtn)
                System.exit(0);
            else if (source == rockBtn) {
                userTxt.setText(" Rock");
                userChoice = ROCK;
                hal.setRoll();
                computerGuess = hal.getFaceValue();
                switch (computerGuess) {
                case ROCK:
                    compTxt.setText(" Rock");
                    break;
                case PAPER:
                    compTxt.setText(" Paper");
                    break;
                case SCISSORS:
                    compTxt.setText(" Scissors");
                    break;
                default:
                    compTxt.setText(" error");
                } // switch
                compare(userChoice, computerGuess);
     
            } // if Rock
            else if (source == paperBtn) {
                userTxt.setText(" Paper");
                userChoice = PAPER;
                hal.setRoll();
                computerGuess = hal.getFaceValue();
                switch (computerGuess) {
                case ROCK:
                    compTxt.setText(" Rock");
                    break;
                case PAPER:
                    compTxt.setText(" Paper");
                    break;
                case SCISSORS:
                    compTxt.setText(" Scissors");
                    break;
                default:
                    compTxt.setText(" error");
                } // switch
                compare(userChoice, computerGuess);
     
            } // if Paper
            else if (source == scissorsBtn) {
                userTxt.setText(" Scissors");
                userChoice = SCISSORS;
                hal.setRoll();
                computerGuess = hal.getFaceValue();
                switch (computerGuess) {
                case ROCK:
                    compTxt.setText(" Rock");
                    break;
                case PAPER:
                    compTxt.setText(" Paper");
                    break;
                case SCISSORS:
                    compTxt.setText(" Scissors");
                    break;
                default:
                    compTxt.setText(" error");
                } // switch
     
                compare(userChoice, computerGuess);
            } // if Scissors
            else {
                userTxt.setText("Directions"); //
                showInstructions();
            }
        } // actionPerformed
     
        public void compare(int user, int comp) {
            Font ssBold16 = new Font("SansSerif", Font.BOLD, 16); // change to your
            // fonts and colors, etc.
            if (comp == user) {
                outComeTxt.setFont(ssBold16);
                outComeTxt.setText(" TIE. ");
                ties++;
            } else if ((computerGuess == ROCK && userChoice == SCISSORS)
                    || (computerGuess == PAPER && userChoice == ROCK)
                    || (computerGuess == SCISSORS && userChoice == PAPER)) {
                outComeTxt.setFont(ssBold16);
                outComeTxt.setText(" YOU LOSE."); // feel free to find a better
                // algorithm for the above
                losses++;
                lossesTxt.setFont(ssBold16);
            } else {
                outComeTxt.setFont(ssBold16);
                outComeTxt.setText(" YOU WIN.");
                wins++;
            }
            tiesTxt.setFont(ssBold16);
            tiesTxt.setText(" " + ties);
            lossesTxt.setFont(ssBold16);
            lossesTxt.setText(" " + losses);
            winsTxt.setFont(ssBold16);
            winsTxt.setText(" " + wins);
        } // compare
     
        public void showInstructions() {
     
            outputArea = new JTextArea();
            String output = "";
            Font ssBold14 = new Font("SansSerif", Font.BOLD, 14);
            output = "\n\n Welcome to my Rock, Paper, Scissors Game. Have fun and good luck!\n\n"; // add
            // instructions
            outputArea.setBorder(BorderFactory.createMatteBorder(15, 15, 15, 15,
                    Color.yellow)); // feel free to change colors, attributes, etc.
            outputArea.setBackground(Color.blue);
            outputArea.setForeground(Color.white);
            outputArea.setFont(ssBold14);
            outputArea.setText(output);
            JOptionPane.showMessageDialog(null, outputArea,
                    "INSTRUCTIONS FOR ROCK, PAPER, SCISSORS GAME",
                    JOptionPane.PLAIN_MESSAGE);
        } // showInstructions
     
    } // class


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Directions Buttons needs picture

    That is waaay too much code for us to wade through. Make it easy for people to help you by providing an SSCCE that demonstrates only the problem you're having, without any extra stuff. Narrow it down to a simple program that only attempts to do the thing you're trying to do in your main program.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Directions Buttons needs picture

    Im sorry about that..

    Im trying to get
    private ImageIcon directionsImg = new ImageIcon ("Rock_paper_scissors.jpg");

    to appear when i click my directions button in my program
    public void showInstructions() {
     
            outputArea = new JTextArea();
            String output = "";
            Font ssBold14 = new Font("SansSerif", Font.BOLD, 14);
            output = "\n\n Welcome to my Rock, Paper, Scissors Game. Have fun and good luck!\n\n"; // add
            // instructions
            outputArea.setBorder(BorderFactory.createMatteBorder(15, 15, 15, 15,
                    Color.yellow)); // feel free to change colors, attributes, etc.
            outputArea.setBackground(Color.blue);
            outputArea.setForeground(Color.white);
            outputArea.setFont(ssBold14);
            outputArea.setText(output);
            JOptionPane.showMessageDialog(null, outputArea,
                    "INSTRUCTIONS FOR ROCK, PAPER, SCISSORS GAME",
                    JOptionPane.PLAIN_MESSAGE);
        } // showInstructions

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Directions Buttons needs picture

    So you want the image to display instead of the outputArea JTextArea?

    Just add the icon to a JLabel, then put the JLabel in a JDialog. Show the JDialog instead of calling JOptionPane.showMessageDialog().
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Darkcore123 (December 14th, 2011)

  6. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Directions Buttons needs picture

    oh ok i see, thanks alot

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Directions Buttons needs picture

    You might even be able to pass the JLabel into the JOptionPane.showMessageDialog() method as the second parameter instead of outputArea. I'm not sure though, as I haven't played with it at all.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Converting a picture made up of 0s and 1s into a colored picture??
    By Kranti1992 in forum Java Theory & Questions
    Replies: 10
    Last Post: November 21st, 2011, 06:25 PM
  2. Showing a picture in a GUI
    By joachim89 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 15th, 2010, 02:42 PM
  3. Help with a program to make a landscape picture
    By noseeds in forum Java Theory & Questions
    Replies: 1
    Last Post: December 15th, 2009, 10:25 PM
  4. Modify Colors in a Picture
    By theuniverse in forum Java Theory & Questions
    Replies: 0
    Last Post: October 17th, 2009, 04:49 PM
  5. Need help with a Picture!
    By Scout in forum Java Theory & Questions
    Replies: 1
    Last Post: October 12th, 2009, 05:33 PM

Tags for this Thread