array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • soty
    New Member
    • Mar 2008
    • 25

    array

    Hi

    i'm tryna make a 4 dimensional array that includes colors and changes color when i click any box.

    how do i creat the box first using swing and no applet?

    please note i dont wanna use button.

    thank you
  • Kid Programmer
    New Member
    • Mar 2008
    • 176

    #2
    just say:

    g.drawRect(poin t.x, point.y, rectWidth, rectHeight);

    Hope this helps. I'm not to good with Graphical User Interfaces though.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by soty
      Hi

      i'm tryna make a 4 dimensional array that includes colors and changes color when i click any box.
      How do you want to display a four dimensional box on a two dimensional screen?

      kind regards,

      Jos

      Comment

      • soty
        New Member
        • Mar 2008
        • 25

        #4
        Originally posted by JosAH
        How do you want to display a four dimensional box on a two dimensional screen?

        kind regards,

        Jos
        i wanna use swing not applet. and i'm using frame and mouseEventListe ner

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by soty
          i wanna use swing not applet. and i'm using frame and mouseEventListe ner
          Huh? erm, and I wanna have a beer not soda. and I'm using strong black tobacco
          and Dutch jenever but I fail to see what that has to with four dimensional boxes.

          kind regards,

          Jos

          Comment

          • sukatoa
            Contributor
            • Nov 2007
            • 539

            #6
            Originally posted by soty
            Hi

            i'm tryna make a 4 dimensional array that includes colors and changes color when i click any box.

            how do i creat the box first using swing and no applet?

            please note i dont wanna use button.

            thank you
            Are you attempting to store all the RGB color combinations in an array?

            Maybe you can just have it in a 2 dimensional array....
            eg: box[current color][RGB colors]

            regards,
            sukatoa

            Comment

            • soty
              New Member
              • Mar 2008
              • 25

              #7
              Originally posted by sukatoa
              Are you attempting to store all the RGB color combinations in an array?

              Maybe you can just have it in a 2 dimensional array....
              eg: box[current color][RGB colors]

              regards,
              sukatoa
              i'm tryna create a tictactoe game. but i dont wanna use gdraw . i'm tryna see if i could use jpanel. also i dont wanna use buttons either since i'm gonna use mouse eventlistener. finally the array for the boxes should be 2-dimensional array.

              thank you

              Comment

              • soty
                New Member
                • Mar 2008
                • 25

                #8
                finally i was able to come up wif this. but the thing is wrote method for checking if the board is full, method for win, and method t clear screen but i dont know how to use mouse listener to connect it.... also i dont know if my logic is right.


                please i need help its due tuesday.
                Code:
                import java.awt.*;
                import java.awt.event.*;
                import java.awt.event.MouseEvent;
                import javax.swing.*;
                import javax.swing.text.*;
                import java.text.DecimalFormat;
                import java.awt.datatransfer.*;
                
                public class TicTacToeGame extends JFrame implements ActionListener {
                
                    private JPanel ticTacPanel = new JPanel();
                    JLabel tictacpanelDisplay[][] = new JLabel[3][3];
                    ImageIcon xImage = createImageIcon("XImage.gif", "");
                    ImageIcon OImage = createImageIcon("OImage.gif", "");
                    ImageIcon plainImage = createImageIcon("plain.gif", "");
                    char player1 = 'x';
                    char player2 = 'o';
                    char empty = 'p';
                    char field[][] = new char[3][3];
                
                    public TicTacToeGame() {
                
                
                        this.setLayout(new BorderLayout());
                        ticTacPanel.setLayout(new GridLayout(3, 3));
                        ticTacPanel.addMouseListener(new MouseAdapter() {
                        });
                        //create an instance of the menu
                        MenuBar mnuBar = new MenuBar();
                        setMenuBar(mnuBar);
                
                        //construct and populate the File menu
                        Menu mnuFile = new Menu("File", true);
                        mnuBar.add(mnuFile);
                        MenuItem mnuFileNew = new MenuItem("New Game");
                        mnuFile.add(mnuFileNew);
                        MenuItem mnuFileScore = new MenuItem("See Score");
                        mnuFile.add(mnuFileScore);
                        MenuItem mnuFileExit = new MenuItem("Exit");
                        mnuFile.add(mnuFileExit);
                
                        mnuFileNew.addActionListener(this);
                        mnuFileScore.addActionListener(this);
                        mnuFileExit.addActionListener(this);
                
                        mnuFileNew.setActionCommand("New Game");
                        mnuFileScore.setActionCommand("See Score");
                        mnuFileExit.setActionCommand("Exit");
                
                        for (int i = 0; i < 3; i++) 
                        {
                            for (int j = 0; j < 3; j++) 
                            {
                                tictacpanelDisplay[i][j] = new JLabel(plainImage);
                                ticTacPanel.add(tictacpanelDisplay[i][j]);
                            }
                        }
                        add(ticTacPanel, BorderLayout.CENTER);
                
                        for (int row = 0; row < 3; row++) 
                        {
                            for (int column = 0; column < 3; column++) 
                            {
                                field[row][column] = empty;
                                System.out.print(field[row][column]);
                            }
                        }
                
                    }
                
                    
                    public boolean BoarfIsFull()
                    {
                        for (int i = 0; i < 3; i++)
                        {
                            for (int j = 0; j < 3; j++) 
                            {
                                if(field[i][j]== empty)
                                    return false;
                            }
                        }
                        return true;
                        
                    }
                    
                    
                     public void clearboard() 
                     {
                        for (int i = 0; i < 3; i++) 
                        {
                            for (int j = 0; j < 3; j++) 
                            {
                                field[i][j] = empty;
                            }
                        }
                        for (int i = 0; i < 3; i++) 
                        {
                            for (int j = 0; j < 3; j++) 
                            {
                                tictacpanelDisplay[i][j] = new JLabel(plainImage);
                                ticTacPanel.add(tictacpanelDisplay[i][j]);
                            }
                        }
                
                    }
                
                    public boolean BoardIsFull() {
                        for (int i = 0; i < 3; i++) {
                            for (int j = 0; j < 3; j++) {
                                if (field[i][j] == empty) {
                                    return false;
                                }
                            }
                        }
                        return true;
                
                    }
                
                    public void playgame(boolean click) 
                    {
                          int count=0;
                        for (int i = 0; i < 3; i++) 
                        {
                            for (int j = 0; j < 3; j++) 
                            {
                                if (click && (!BoardIsFull()) &&(count==0||count==2||count==4||count ==6||count ==8)) 
                                {
                                  
                                   field[i][j]=player1;
                                }
                                else if(click && (!BoardIsFull()) &&(count==1||count==3||count==5||count ==7))
                                    field[i][j] = player2;
                
                            }
                
                        }
                    }
                
                    public boolean isAwin(char token) {
                        for (int i = 0; i < 3; i++) {
                            if ((field[i][0] == token) && (field[i][1] == token) && (field[i][2] == token)) {
                                return true;
                            }
                        }
                
                        for (int j = 0; j < 3; j++) {
                            if ((field[0][j] == token) && (field[1][j] == token) && (field[2][j] == token)) {
                                return true;
                            }
                        }
                
                        if ((field[0][0] == token) && (field[1][1] == token) && (field[2][2] == token)) {
                            return true;
                        }
                
                        if ((field[0][2] == token) && (field[1][1] == token) && (field[2][0] == token)) {
                            return true;
                        }
                
                        return false;
                    }
                
                    // 	The code is executed when the mouse is pressed
                    public void mousePressed(MouseEvent me) {
                    }
                
                 
                    public void actionPerformed(ActionEvent e) {
                        //test for menu item clicks
                        String arg = e.getActionCommand();
                        if (arg == "Exit") {
                            System.exit(0);
                        }
                    }
                
                    /** Returns an ImageIcon, or null if the path was invalid. */
                    protected ImageIcon createImageIcon(String path,
                            String description) {
                        java.net.URL imgURL = getClass().getResource(path);
                        if (imgURL != null) {
                            return new ImageIcon(imgURL, description);
                        } else {
                            System.err.println("Couldn't find file: " + path);
                            return null;
                        }
                    }
                
                 
                    public static void main(String args[]) {
                        TicTacToeGame mine = new TicTacToeGame();
                        mine.setBounds(300, 300, 300, 400);
                        mine.setTitle("The Game of Tic Tac Toe");
                        mine.setVisible(true);
                    }
                }

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  1.) Don't mix AWT and Swing.
                  2) For the mouselistener, read Sun's tutorial here.
                  3.) For the correct logic, write down the correct steps in psuedo code and see if your program conforms to those steps.

                  Comment

                  • soty
                    New Member
                    • Mar 2008
                    • 25

                    #10
                    Originally posted by r035198x
                    1.) Don't mix AWT and Swing.
                    2) For the mouselistener, read Sun's tutorial here.
                    3.) For the correct logic, write down the correct steps in psuedo code and see if your program conforms to those steps.
                    thanks i finally solve is but i dont know was jus happen and i started getting this error

                    i'm finally done wif ma code but i'm getting this error. do u av any idea wat the prob is?



                    Code:
                    init:
                    deps-jar:
                    Compiling 1 source file to /Users/ask4soteria/NetBeansProjects/ticTacToeGame/build/classes
                    compile-single:
                    run-single:
                    Exception in thread "main" java.lang.NullPointerException
                            at ticTacToeGame.<init>(ticTacToeGame.java:38)
                            at ticTacToeGame.main(ticTacToeGame.java:208)
                    Java Result: 1
                    BUILD SUCCESSFUL (total time: 2 seconds)
                    Last edited by JosAH; Apr 30 '08, 07:56 AM. Reason: fixed the [code] ... [/code] tags

                    Comment

                    Working...