Results 1 to 6 of 6
  1. #1
    thechariot is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Question Help me: i want to have an image on my game

    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;



    public class Game implements WindowListener{

    Random gen = new Random();

    private String user;
    private String comp;
    private int rand;

    private String r = "ROCK";
    private String p = "PAPER";
    private String s = "SCISSORS";

    private int userScore = 0;
    private int compScore = 0;
    private int tie = 0;

    JFrame f= new JFrame("Rock-Paper-Scissors");


    JTextField itemUsers = new JTextField(8);
    JTextField itemComps = new JTextField(8);
    JButton btnPlay = new JButton("Play!");
    JButton btnNew = new JButton("New Game");
    JButton btnRock = new JButton("Rock");
    JButton btnPaper = new JButton("Paper");
    JButton btnScissor = new JButton("Scissors");
    JButton btnClose = new JButton("Close");

    public Game(){

    Panel choice = new Panel();
    choice.setLayout(new FlowLayout());
    choice.add(new JLabel("Choose:"));
    choice.add(btnRock);
    choice.add(btnPaper);
    choice.add(btnScissor);


    Panel play = new Panel();
    play.setLayout(new FlowLayout());
    play.add(btnPlay);
    play.add(btnNew);
    play.add(btnClose);

    Panel vision = new Panel();

    vision.add(itemUsers);
    vision.add(new JLabel(" VS"));
    vision.add(itemComps);



    f.add(choice,BorderLayout.NORTH);
    f.add(play,BorderLayout.SOUTH);
    f.add(vision,BorderLayout.CENTER);

    f.addWindowListener(this);
    f.setSize(370,150);
    f.setResizable(false);
    f.setVisible(true);
    addFunctions();

    }

    public void addFunctions(){


    btnRock.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    itemUsers.setText("");
    itemUsers.setText(r);
    user = r;
    }
    }
    );

    btnPaper.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    itemUsers.setText("");
    itemUsers.setText(p);
    user = p;

    }
    }
    );


    btnScissor.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    itemUsers.setText("");
    itemUsers.setText(s);
    user = s;


    }
    }
    );

    btnClose.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    JOptionPane.showMessageDialog(null,"Thank You"+"\nFor"+"\nPlaying! Come Again :D");
    System.exit(0);

    }
    }
    );

    btnPlay.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    rand= gen.nextInt(3);
    if(user == null){
    JOptionPane.showMessageDialog(null,"Choose an item first!");
    rand=4;
    }

    if(rand == 0)
    comp=r;
    if(rand == 1)
    comp=p;
    if (rand == 2)
    comp=s;
    itemComps.setText("");
    itemComps.setText(comp);

    if(user == r && comp == r){
    JOptionPane.showMessageDialog(null,"ohhh! its a tie. choose again!");
    itemUsers.setText(null);
    itemComps.setText(null);
    user = null;
    comp = null;
    tie++;
    }
    if(user == r && comp == p){
    JOptionPane.showMessageDialog(null,"PAPER BEATS ROCK"+"\nYOU LOSE :P");
    itemUsers.setText(null);
    itemComps.setText(null);
    user = null;
    comp = null;
    compScore++;
    }
    if(user == r && comp == s){
    JOptionPane.showMessageDialog(null,"ROCK KILLS SCISSOR"+"\nCongratulation!"+"\nYOU WIN! :D");
    itemUsers.setText(null);
    itemComps.setText(null);
    user = null;
    comp = null;
    userScore++;
    }


    if(user == p && comp == p){
    JOptionPane.showMessageDialog(null,"ohhh! its a tie. choose again!");
    itemUsers.setText(null);
    itemComps.setText(null);
    user = null;
    comp = null;
    tie++;
    }
    if(user == p && comp == s){
    JOptionPane.showMessageDialog(null,"SCISSOR CUTS PAPER"+"\nYOU LOSE :P");
    itemUsers.setText(null);
    itemComps.setText(null);
    user = null;
    comp = null;
    compScore++;
    }
    if(user == p && comp == r){
    JOptionPane.showMessageDialog(null,"PAPER BEATS ROCK"+"\nCongratulation!"+"\nYOU WIN! :D");
    itemUsers.setText(null);
    itemComps.setText(null);
    user = null;
    comp = null;
    userScore++;
    }

    if(user == s && comp == s){
    JOptionPane.showMessageDialog(null,"ohhh! its a tie. choose again!");
    itemUsers.setText(null);
    itemComps.setText(null);
    user = null;
    comp = null;
    tie++;
    }
    if(user == s && comp == r){
    JOptionPane.showMessageDialog(null,"ROCK KILLS SCISSOR"+"\nYOU LOSE :P");
    itemUsers.setText(null);
    itemComps.setText(null);
    user = null;
    comp = null;
    compScore++;
    }
    if(user == s && comp == p){
    JOptionPane.showMessageDialog(null,"SCISSORS CUTS PAPER"+"\nCongratulation!"+"\nYOU WIN! :D");
    itemUsers.setText(null);
    itemComps.setText(null);
    user = null;
    comp = null;
    userScore++;
    }
    JOptionPane.showMessageDialog(null, "Scores: " + "\nTies= "+tie+"\nUser= " + userScore + "\nComputer= " + compScore);

    }

    }
    );

    btnNew.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    itemUsers.setText(null);
    itemComps.setText(null);
    user = null;
    comp = null;
    userScore = 0;
    compScore = 0;
    tie = 0;

    }
    }
    );

    }



    public void windowActivated(WindowEvent e){
    }

    public void windowClosed(WindowEvent e){

    }

    public void windowClosing(WindowEvent e){
    JOptionPane.showMessageDialog(null,"Thank You"+"\nFor"+"\nPlaying! Come Again :D");

    System.exit(0);
    }

    public void windowDeactivated(WindowEvent e){
    }

    public void windowDeiconified(WindowEvent e){
    }

    public void windowIconified(WindowEvent e){

    }

    public void windowOpened(WindowEvent e){

    }

    public static void main(String args[]){
    Game g= new Game();



    }


    }

    can some one help me to put an image on my game when i press rock the rock image will display?
    thanks
    Last edited by thechariot; 03-17-2009 at 12:54 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I recommend that you look at using an ImageIcon and place that icon into a JLabel. Then you can use a CardLayout to swap JLabels when necessary. You can find out more about this plus some great sample code here:

    How to Use Labels (The Java™ Tutorials > Creating a GUI with JFC/Swing > Using Swing Components)
    How to Use Icons (The Java™ Tutorials > Creating a GUI with JFC/Swing > Using Swing Components)
    How to Use CardLayout (The Java™ Tutorials > Creating a GUI with JFC/Swing > Laying Out Components Within a Container)

    Also, one more thing: when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]
    Good luck!

  3. #3
    thechariot is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    can you help me how to use this code.. i know how to use it but when i press the rock button the image did not display..

    Java Code:
      
    		btnRock.addActionListener(new ActionListener(){
    					public void actionPerformed(ActionEvent e){
    						itemUsers.setText(""); //how can i set here so when i press the button image will display in label
    						itemUsers.setText(r);
    						user = r;
    								}
    							}
    				);

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I would have 3 ImageIcons, one for a rock (let's call it rockIcon), one for a scissors, one for a paper. I would have a JLabel, let's call it imageLabel that is a class variable and thus will be visible from within the action listener. Then within the actionperformed, I'd call imageLabel.setIcon(rockIcon);

    So actually you don't even need a CardLayout to solve this problem.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Here's a small program that demonstrates this. For this to work on your system, you'll have to change the image file names and image path to match some on your system:
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JComponent;
    
    public class SwapImages
    {
      // these are images present in my classpath.  You'll have to change
      // these constants to test this program on your system
      private static final String IMAGE_PATH = "src/yr2009/m02/c/images/";
      private static final String IMAGE1 = "Bullfight.jpg";
      private static final String IMAGE2 = "Thinking.jpg";
      
      private static final String SHOW_IMAGE_1 = "Show Image 1";
      private static final String SHOW_IMAGE_2 = "Show Image 2";
      private JPanel mainPanel = new JPanel();
      private ImageIcon icon1;
      private ImageIcon icon2;
      private JLabel imageLabel = new JLabel();
      
      public SwapImages()
      {
        try
        {
          // create images
          BufferedImage image1 = ImageIO.read(new File(IMAGE_PATH + IMAGE1));
          BufferedImage image2 = ImageIO.read(new File(IMAGE_PATH + IMAGE2));
          
          // turn them into image icons
          icon1 = new ImageIcon(image1);
          icon2 = new ImageIcon(image2);
          
          // get the largest dimension and set image labe's preferred size
          // to this dimension
          int width = Math.max(image1.getWidth(), image2.getWidth());
          int height = Math.max(image1.getHeight(), image2.getHeight());
          imageLabel.setPreferredSize(new Dimension(width, height));
          
          // create buttons that show images, use constant Strings as button's
          // Strings.  These will be the same as the button's actionCommand
          // Strings that will be tested in the action listener
          JButton showImage1Btn = new JButton(SHOW_IMAGE_1);
          JButton showImage2Btn = new JButton(SHOW_IMAGE_2);
          ButtonListener btnListener = new ButtonListener();
          showImage1Btn.addActionListener(btnListener);
          showImage2Btn.addActionListener(btnListener);
          
          JPanel buttonPanel = new JPanel();
          buttonPanel.add(showImage1Btn);
          buttonPanel.add(showImage2Btn);
          
          mainPanel.setLayout(new BorderLayout());
          mainPanel.add(imageLabel, BorderLayout.CENTER);
          mainPanel.add(buttonPanel, BorderLayout.SOUTH);
        }
        catch (IOException e)
        {
          e.printStackTrace();
        }
      }
    
      public JComponent getComponent()
      {
        return mainPanel;
      }
      
      private class ButtonListener implements ActionListener
      {
        public void actionPerformed(ActionEvent e)
        {
          String command = e.getActionCommand();
          if (command.equals(SHOW_IMAGE_1))
          {
            imageLabel.setIcon(icon1);
          }
          else if (command.equals(SHOW_IMAGE_2))
          {
            imageLabel.setIcon(icon2);
          }
        }
      }
      private static void createAndShowUI()
      {
        JFrame frame = new JFrame("SwapImages");
        frame.getContentPane().add(new SwapImages().getComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowUI();
          }
        });
      }
    }

  6. #6
    thechariot is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  2. Replies: 14
    Last Post: 04-10-2009, 08:46 AM
  3. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 06:00 AM
  4. drawing an image to an offscreen image
    By hunterbdb in forum Java 2D
    Replies: 9
    Last Post: 10-30-2008, 06:17 PM
  5. Replies: 0
    Last Post: 01-08-2008, 05:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •