new to java can anyone help?

Discussion in 'Java' started by spiketkr0, Apr 5, 2010.

  1. spiketkr0

    spiketkr0 New Member

    Joined:
    Apr 5, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Hello everyone,

    So I am suppose to make a source code that displays 9 labels(3x3) like tic tac toe but instead of playing the computer or even playing I am suppose to make it display either a x,o, or a blank spot in these labels randomly. I can't figure out how to associate the images to a math.random command. any suggestions?

    Spike
     
  2. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    one way to do this is....

    Code:
    
    import javax.swing.*;
    import java.awt.GridLayout;
    import java.util.Random;
    import java.awt.Color;
    
    public class Triliza extends JFrame{
        private JLabel labels[][];//array of JLabels
        private Random random;
        private String validChars[]={"x","o"," "};//valid characters in an array
        public Triliza(){
            random=new Random();
            labels=new JLabel[3][3];
            setLayout(new GridLayout(3,3));
            for (int i=0;i<3;i++)
                for(int j=0;j<3;j++){
                    labels[i][j]=new JLabel(validChars[random.nextInt(3)],JLabel.CENTER);//we choose randomly one element from the array of valid characters.
                    labels[i][j].setBorder(BorderFactory.createLineBorder(Color.black));//we add a border for better results
    
                    add(labels[i][j]);
                }
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            pack();
            setVisible(true);
        }
        public static void main(String args[]){
            new Triliza();
        }
    }
    
     
  3. spiketkr0

    spiketkr0 New Member

    Joined:
    Apr 5, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    well I have an image for the x and the o thats what was getting me confused
     
  4. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    is the above code ok , or you want it with images instead?
     
  5. spiketkr0

    spiketkr0 New Member

    Joined:
    Apr 5, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    well its suppose to use the images I personally think it shouldnt matter but thats what I am suppose to do
     
  6. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    then here you are.
    inside i have put 2 jpg files (x,o) and the appropriate code.
     

    Attached Files:

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice