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 16 of 16

Thread: CALLING ANOTHER PROGRAM

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default CALLING ANOTHER PROGRAM

    Need some help
    I have 2 java programs
    TicTacToe.java(2 player mode)
    TicTacToeAI.java(AI mode)

    I plan to create a java menu bar for the 2 programs

    I have this:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class FinalProject extends JFrame implements ActionListener
    {
     
     
     public FinalProject()
     {
    MenuBar menuBar = new MenuBar();
    setMenuBar(menuBar);
     
    Menu menuFile = new Menu("Game", true);
    menuBar.add(menuFile);
    MenuItem menuTTT = new MenuItem("Versus Mode");
    menuFile.add(menuTTT);
    MenuItem menuTTTAI = new MenuItem("Versus AI");
    menuFile.add(menuTTTAI);
     
    Menu menuAbout = new Menu("About", true);
    menuBar.add(menuAbout);
    MenuItem menuAboutTicTacToe = new MenuItem("About TicTacToe");
    menuAbout.add(menuAboutTicTacToe);
     
    menuTTT.addActionListener(this);
    menuTTTAI.addActionListener(this);
    menuAboutTicTacToe.addActionListener(this);
     
    menuTTT.setActionCommand("Versus Mode");
    menuTTTAI.setActionCommand("Versus AI");
    menuAboutTicTacToe.setActionCommand("About");
     }
     
    public void createAndShow()
     {
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setPreferredSize(new Dimension(100, 80));
      setLocation((1000/2), (400/2));
      pack();
      setVisible(true);
     }
     
     
     
     
    public void actionPerformed(ActionEvent e)
    {
     
    String arg = e.getActionCommand();
        if (arg == "Versus Mode")
    {
     
    }
     
        if (arg == "Versus AI")
    {
     
    }
     
     
        if (arg == "About")
    {
        String message = "Elson's TicTacToe\nComputer Programming II\nCopyright 2011\nAll rights reserved";
        JOptionPane.showMessageDialog(null,message,
        "The Programmer", JOptionPane.INFORMATION_MESSAGE);
    }
    }
     
     
    public static void main(String[] args)
     {
      FinalProject frame = new FinalProject();
      frame.createAndShow();
     }
     
    }

    how can I call the 2 programs and insert here:

    public void actionPerformed(ActionEvent e)
    {
     
    String arg = e.getActionCommand();
        if (arg == "Versus Mode")
    {
     
    }
     
        if (arg == "Versus AI")
    {
     
    }
    Last edited by jeboi; October 5th, 2012 at 07:31 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: CALLING ANOTHER PROGRAM

    Please edit you post and wrap the code in code tags.
    if (arg == "Versus Mode")
    {
    You should use the equals() method to compare Strings.
    It would be better to compare the source of the event or to use a String variable that is used in two places than to use two Strings that could be different if you mistype it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CALLING ANOTHER PROGRAM

    I did what you ask but how can i call two java programs
    e.g.
    If I press "Versus Mode" = TicTacToe.java(2 players) will come out
    If I press "Versus AI" = TicTacToeAI.java will come out

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: CALLING ANOTHER PROGRAM

    how can i call two java programs
    You call methods in a class, not programs.
    Get a reference to the class that has the method and use it to call the method:
    refToClass.theMethod();
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CALLING ANOTHER PROGRAM

    e.g.

    I'll call TicTacToe() to be put here:
    String arg = e.getActionCommand();
        if (arg == "Versus Mode")
    {
     
    }

    and I'll call TicTacToeAI() here:
    }
     
        if (arg == "Versus AI")
    {

    or you can post a simple code here so that I can easily understand what will I do

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: CALLING ANOTHER PROGRAM

    Are those classes or methods that you want to call? You need to explain which.
    Have instances of those two classes been created? If so where are the variables that point to those instances? If there are instances of the class, you call a method in the class like this:
    if(<some condition>) {
       refToClass.theMethod();
       ...
    }


    If they have not been created then you could create them inside the if statements:
    if(<some condition>) {
       TheClassYouWantToCreate tcls =new TheClassYouWantToCreate();  // create an instance calling the class's constructor
       tcls.theMethod(); // Call the method
      ...
    }
    Last edited by Norm; October 5th, 2012 at 08:28 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CALLING ANOTHER PROGRAM

    I have these 2 programs:
    TicTacToe 2PLAYERS
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class TicTacToe extends JFrame implements ActionListener
    {
     JPanel panel;
     JButton[] button;
     private int count = 0;
     private int sign = 0;
     private boolean win = false;
     private String player = "";
     
     
     public TicTacToe()
     {
      panel = new JPanel();
      panel.setLayout(new GridLayout(3,3));
      this.add(panel);
      button = new JButton[9];
      for(int i = 0; i <= 8; i++){
       button[i] = new JButton();
       panel.add(button[i]);
       button[i].setEnabled(true);
       button[i].addActionListener(this);
      }
      //this.pack();
      //this.setDefaultCloseOperation(EXIT_ON_CLOSE);
      //this.setVisible(true);
     }
     
     public void createAndShow()
     {
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setPreferredSize(new Dimension(300, 300));
      setLocation((1000/2), (400/2));
      pack();
      setVisible(true);
     }
     
     public void actionPerformed(ActionEvent e)
     {
      count++;
      for(int i =0; i <=8; i++){
       if(button[i] == e.getSource()){
        if(sign%2 == 0){
         button[i].setText("X");
         button[i].setEnabled(false);
        }
        else{
         button[i].setText("O");
         button[i].setEnabled(false);
        } 
       }
      }
     
      // The Players (turns)
      if(count%2==0){
        player = "P2";
           } 
      else{
        player = "P1";
          }
     
     
      checkWinner();
      if(count >= 9){
       JOptionPane.showMessageDialog(null, "Draw Game!");
       for(int j = 0; j<= 8; j++){
        button[j].setText("");
        button[j].setEnabled(true);
       }
       count = 0;
       sign = 0;
       return;
      }
      sign++;
     
      // Show Player Wins
      if (win == true){
       JOptionPane.showMessageDialog(null, player + " WINS!");
     
       newGameDialog();
       } 
     
     }
     
     public boolean checkWinner()
     {
      // horizontal
       if( button[0].getText() == button[1].getText() && button[1].getText() == button[2].getText() && button[0].getText() != ""){
         win = true;
       }
       else if(button[3].getText() == button[4].getText() && button[4].getText() == button[5].getText() && button[3].getText() != ""){
         win = true;
       }
       else if(button[6].getText() == button[7].getText() && button[7].getText() == button[8].getText() && button[6].getText() != ""){
         win = true;
       }
       // vertical
       else if(button[0].getText() == button[3].getText() && button[3].getText() == button[6].getText() && button[0].getText() != ""){
         win = true;
       }
       else if(button[1].getText() == button[4].getText() && button[4].getText() == button[7].getText() && button[1].getText() != ""){
         win = true;
       }
       else if(button[2].getText() == button[5].getText() && button[5].getText() == button[8].getText() && button[2].getText() != ""){
         win = true;
       }
       // diagonal
       else if(button[0].getText() == button[4].getText() && button[4].getText() == button[8].getText() && button[0].getText() != ""){
         win = true;
       }
       else if(button[2].getText() == button[4].getText() && button[4].getText() == button[6].getText() && button[2].getText() != ""){
         win = true;
       }
     
       return true;
     }
     
     
     public void newGameDialog(){
       int response = JOptionPane.showConfirmDialog(null, "Do you want a new game?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
       if(response == JOptionPane.YES_OPTION)
       reset();
       else{
       System.exit(0);
       }
     
     }
     
       public void reset(){
         for(int i=0;i<=8;i++){
           button[i].setText("");
           button[i].setEnabled(true);
         }
     
         win = false;
         count = 0;
         sign = 0;
     
     
     
     
       }
     
     
     
     public static void main(String[] args)
     {
      TicTacToe frame = new TicTacToe();
      frame.createAndShow();
     }
    }

    TicTacToe AI MODE

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
     
    public class TicTacToeAI extends JFrame implements ActionListener 
    {
     JPanel panel;
     JButton[] button;
     private int count = 0;
     private boolean win = false;
     private String player = "";
     
     public TicTacToeAI()
     {
      panel = new JPanel();
      panel.setLayout(new GridLayout(3,3));
      this.add(panel);
      button = new JButton[9];
      for(int i = 0; i <= 8; i++){
      button[i] = new JButton();
      panel.add(button[i]);
      button[i].setEnabled(true);
      button[i].addActionListener(this);
     }
     }
     
     public void createAndShow()
     {
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setPreferredSize(new Dimension(300, 300));
      setLocation((1000/2), (400/2));
      pack();
      setVisible(true);
     }
     
     
     public void actionPerformed(ActionEvent e)
     {
       for(int i =0; i <=8; i++){
       if(button[i] == e.getSource()){
         button[i].setText("X");
         button[i].setEnabled(false);
       }
       }
       count++;
      AI();
     
     
      if(count % 2 == 0){
        player = "Computer";}
      else{
        player = "P1";}
     
      checkWinner();
      if(count >=9){
       JOptionPane.showMessageDialog(null, "Draw Game!");
       for(int j = 0; j<= 8; j++){
        button[j].setText("");
        button[j].setEnabled(true);
       }
        count = 0;
        return;
       }
       newGameDialog();
     
       if (win == true){
       JOptionPane.showMessageDialog(null, player + " WINS!");
       newGameDialog();
       }
     }
     
     public void AI(){
      count++;
                    if(button[0].getText().equals("O") && button[1].getText().equals("O") && button[2].getText().equals("")){
                            button[2].setText("O");
                            button[2].setEnabled(false);
                    } else if(button[3].getText().equals("O") && button[4].getText().equals("O") && button[5].getText().equals("")){
                            button[5].setText("O");
                            button[5].setEnabled(false);
                    } else if(button[6].getText().equals("O") && button[7].getText().equals("O") && button[8].getText().equals("")){
                            button[8].setText("O");
                            button[8].setEnabled(false);                           
                    } 
     
                    else if(button[1].getText().equals("O") && button[2].getText().equals("O") && button[0].getText().equals("")){
                            button[0].setText("O");
                            button[0].setEnabled(false);                           
                    } else if(button[4].getText().equals("O") && button[5].getText().equals("O") && button[3].getText().equals("")){
                            button[3].setText("O");
                            button[3].setEnabled(false);                           
                    } else if(button[7].getText().equals("O") && button[8].getText().equals("O") && button[6].getText().equals("")){
                            button[6].setText("O");
                            button[6].setEnabled(false);                           
                    }
     
                    else if(button[0].getText().equals("O") && button[2].getText().equals("O") && button[1].getText().equals("")){
                            button[1].setText("O");
                            button[1].setEnabled(false);                           
                    } else if(button[3].getText().equals("O") && button[5].getText().equals("O") && button[4].getText().equals("")){
                            button[4].setText("O");
                            button[4].setEnabled(false);                           
                    } else if(button[6].getText().equals("O") && button[8].getText().equals("O") && button[7].getText().equals("")){
                            button[7].setText("O");
                            button[7].setEnabled(false);                           
                    }
     
                    else if(button[0].getText().equals("O") && button[3].getText().equals("O") && button[6].getText().equals("")){
                            button[6].setText("O");
                            button[6].setEnabled(false);                           
                    } else if(button[1].getText().equals("O") && button[4].getText().equals("O") && button[7].getText().equals("")){
                            button[5].setText("O");
                            button[5].setEnabled(false);                           
                    } else if(button[2].getText().equals("O") && button[5].getText().equals("O") && button[8].getText().equals("")){
                            button[8].setText("O");
                            button[8].setEnabled(false);                           
                    }
     
                    else if(button[3].getText().equals("O") && button[6].getText().equals("O") && button[0].getText().equals("")){
                            button[0].setText("O");
                            button[0].setEnabled(false);                           
                    } else if(button[4].getText().equals("O") && button[7].getText().equals("O") && button[1].getText().equals("")){
                            button[1].setText("O");
                            button[1].setEnabled(false);                           
                    } else if(button[5].getText().equals("O") && button[8].getText().equals("O") && button[2].getText().equals("")){
                            button[2].setText("O");
                            button[2].setEnabled(false);                           
                    }
     
                    else if(button[0].getText().equals("O") && button[6].getText().equals("O") && button[3].getText().equals("")){
                            button[3].setText("O");
                            button[3].setEnabled(false);                           
                    } else if(button[1].getText().equals("O") && button[7].getText().equals("O") && button[4].getText().equals("")){
                            button[4].setText("O");
                            button[4].setEnabled(false);                           
                    } else if(button[2].getText().equals("O") && button[8].getText().equals("O") && button[5].getText().equals("")){
                            button[5].setText("O");
                            button[5].setEnabled(false);                           
                    }
     
                    else if(button[0].getText().equals("O") && button[4].getText().equals("O") && button[8].getText().equals("")){
                            button[8].setText("O");
                            button[8].setEnabled(false);                           
                    } else if(button[4].getText().equals("O") && button[8].getText().equals("O") && button[0].getText().equals("")){
                            button[0].setText("O");
                            button[0].setEnabled(false);                           
                    } else if(button[0].getText().equals("O") && button[8].getText().equals("O") && button[4].getText().equals("")){
                            button[4].setText("O");
                            button[4].setEnabled(false);                           
                    }
     
                    else if(button[2].getText().equals("O") && button[3].getText().equals("O") && button[6].getText().equals("")){
                            button[6].setText("O");
                            button[6].setEnabled(false);                           
                    } else if(button[6].getText().equals("O") && button[4].getText().equals("O") && button[2].getText().equals("")){
                            button[2].setText("O");
                            button[2].setEnabled(false);                           
                    } else if(button[6].getText().equals("O") && button[2].getText().equals("O") && button[4].getText().equals("")){
                            button[4].setText("O");
                            button[4].setEnabled(false);                           
                    }
     
                    else if(button[0].getText().equals("X") && button[1].getText().equals("X") && button[2].getText().equals("")){
                            button[2].setText("O");
                            button[2].setEnabled(false);
                    } else if(button[3].getText().equals("X") && button[4].getText().equals("X") && button[5].getText().equals("")){
                            button[5].setText("O");
                            button[5].setEnabled(false);                           
                    } else if(button[6].getText().equals("X") && button[7].getText().equals("X") && button[8].getText().equals("")){
                            button[8].setText("O");
                            button[8].setEnabled(false);                           
                    } 
     
                    else if(button[1].getText().equals("X") && button[2].getText().equals("X") && button[0].getText().equals("")){
                            button[0].setText("O");
                            button[0].setEnabled(false);                           
                    } else if(button[4].getText().equals("X") && button[5].getText().equals("X") && button[3].getText().equals("")){
                            button[3].setText("O");
                            button[3].setEnabled(false);                           
                    } else if(button[7].getText().equals("X") && button[8].getText().equals("X") && button[6].getText().equals("")){
                            button[6].setText("O");
                            button[6].setEnabled(false);                           
                    }
     
                    else if(button[0].getText().equals("X") && button[2].getText().equals("X") && button[1].getText().equals("")){
                            button[1].setText("O");
                            button[1].setEnabled(false);                           
                    } else if(button[3].getText().equals("X") && button[5].getText().equals("X") && button[4].getText().equals("")){
                            button[4].setText("O");
                            button[4].setEnabled(false);                           
                    } else if(button[6].getText().equals("X") && button[8].getText().equals("X") && button[7].getText().equals("")){
                            button[7].setText("O");
                            button[7].setEnabled(false);                           
                    }
     
                    else if(button[0].getText().equals("X") && button[3].getText().equals("X") && button[6].getText().equals("")){
                            button[6].setText("O");
                            button[6].setEnabled(false);                           
                    } else if(button[1].getText().equals("X") && button[4].getText().equals("X") && button[7].getText().equals("")){
                            button[7].setText("O");
                            button[7].setEnabled(false);                           
                    } else if(button[2].getText().equals("X") && button[5].getText().equals("X") && button[8].getText().equals("")){
                            button[8].setText("O");
                            button[8].setEnabled(false);                           
                    }
     
                    else if(button[3].getText().equals("X") && button[6].getText().equals("X") && button[0].getText().equals("")){
                            button[0].setText("O");
                            button[0].setEnabled(false);                           
                    } else if(button[4].getText().equals("X") && button[7].getText().equals("X") && button[1].getText().equals("")){
                            button[1].setText("O");
                            button[1].setEnabled(false);                           
                    } else if(button[5].getText().equals("X") && button[8].getText().equals("X") && button[2].getText().equals("")){
                            button[2].setText("O");
                            button[2].setEnabled(false);                           
                    }
     
                    else if(button[0].getText().equals("X") && button[6].getText().equals("X") && button[3].getText().equals("")){
                            button[3].setText("O");
                            button[3].setEnabled(false);                           
                    } else if(button[1].getText().equals("X") && button[7].getText().equals("X") && button[4].getText().equals("")){
                            button[4].setText("O");
                            button[4].setEnabled(false);                           
                    } else if(button[2].getText().equals("X") && button[8].getText().equals("X") && button[5].getText().equals("")){
                            button[5].setText("O");
                            button[5].setEnabled(false);                           
                    }
     
                    else if(button[0].getText().equals("X") && button[4].getText().equals("X") && button[8].getText().equals("")){
                            button[8].setText("O");
                            button[8].setEnabled(false);                           
                    } else if(button[4].getText().equals("X") && button[8].getText().equals("X") && button[0].getText().equals("")){
                            button[0].setText("O");
                            button[0].setEnabled(false);                           
                    } else if(button[0].getText().equals("X") && button[8].getText().equals("X") && button[4].getText().equals("")){
                            button[4].setText("O");
                            button[4].setEnabled(false);                           
                    }
     
                    else if(button[2].getText().equals("X") && button[4].getText().equals("X") && button[6].getText().equals("")){
                            button[6].setText("O");
                            button[6].setEnabled(false);                           
                    } else if(button[6].getText().equals("X") && button[4].getText().equals("X") && button[2].getText().equals("")){
                            button[2].setText("O");
                            button[2].setEnabled(false);                           
                    } else if(button[6].getText().equals("X") && button[2].getText().equals("X") && button[4].getText().equals("")){
                            button[4].setText("O");
                            button[4].setEnabled(false);                           
                    }
     
                    else if(button[0].getText().equals("X") && button[4].getText().equals("O") && button[8].getText().equals("X")) {
                            button[5].setText("O");
                            button[5].setEnabled(false);                   
                    }       
     
                    else if(button[2].getText().equals("X") && button[4].getText().equals("O") && button[6].getText().equals("X")) {
                            button[3].setText("O");
                            button[3].setEnabled(false);                   
                    }
     
                    else if(button[4].getText().equals("")){
                            button[4].setText("O");
                            button[4].setEnabled(false);                           
                    }
     
                    else if(button[0].getText().equals("")){
                            button[0].setText("O");
                            button[0].setEnabled(false);                           
                    }
                    else {
                            if(count >= 9)
                                    checkWinner();
                            else
                                    RandomMove();
                    }
     
                    checkWinner();
     
            }
     
       public void RandomMove(){
         Random x = new Random();
         int y = 0 + x.nextInt(8);
         if(button[y].getText().equals("O") || button[y].getText().equals("X") ){
           RandomMove();
          } else {
          button[y].setText("O");
          button[y].setEnabled(false);
          }
          }
     
     
     
     
     public void checkWinner()
     {
       // horizontal
       if( button[0].getText() == button[1].getText() && button[1].getText() == button[2].getText() && button[0].getText() != ""){
         win = true;
       }
       else if(button[3].getText() == button[4].getText() && button[4].getText() == button[5].getText() && button[3].getText() != ""){
         win = true;
       }
       else if(button[6].getText() == button[7].getText() && button[7].getText() == button[8].getText() && button[6].getText() != ""){
         win = true;
       }
       // vertical
       else if(button[0].getText() == button[3].getText() && button[3].getText() == button[6].getText() && button[0].getText() != ""){
         win = true;
       }
       else if(button[1].getText() == button[4].getText() && button[4].getText() == button[7].getText() && button[1].getText() != ""){
         win = true;
       }
       else if(button[2].getText() == button[5].getText() && button[5].getText() == button[8].getText() && button[2].getText() != ""){
         win = true;
       }
       // diagonal
       else if(button[0].getText() == button[4].getText() && button[4].getText() == button[8].getText() && button[0].getText() != ""){
         win = true;
       }
       else if(button[2].getText() == button[4].getText() && button[4].getText() == button[6].getText() && button[2].getText() != ""){
         win = true;
       }
     
     }
     
     
     
      public void newGameDialog(){
       int response = JOptionPane.showConfirmDialog(null, "Do you want a new game?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
       if(response == JOptionPane.YES_OPTION)
       reset();
       else{
       System.exit(0);
       }
     
     }
     
       public void reset(){
         for(int i=0;i<=8;i++){
           button[i].setText("");
           button[i].setEnabled(true);
         }
     
         win = false;
         count = 0;
     
     
     
     
       }
     
     
     
    public static void main(String[] args)
    {
       TicTacToeAI frame = new TicTacToeAI();
       frame.createAndShow();
    }
    }



    How to insert here calling the class or method?

    String click = e.getActionCommand();
        if (click.equals("Versus Mode"))
    {
     
    }
     
        if (click.equals("Versus AI"))
    {
     
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: CALLING ANOTHER PROGRAM

    What method do you want to call? Those classes have several methods in them.
    Is there a reference to an instance of the class with that method available in the if statements where you want to call that method?
    Or do you want to call the class's constructor?

    I gave some samples in post#6.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CALLING ANOTHER PROGRAM

    in the menubar if a user clicks the 'Game' 2 options will come out
    Versus mode and Versus AI

    If the user will click the 'Versus Mode' TicTacToe.java will appear
    If the user will click the 'Versus AI' TicTacToeAI.java will appear

    and I dont know what class or methods will be called
    Can you help me

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: CALLING ANOTHER PROGRAM

    TicTacToe.java will appear
    Your terminology is confusing. Classes like JFrame can cause GUI items to appear on the screen as windows.
    The source file: TicTacToe.java does not appear. The source file contains the definition of a class that can be compiled and executed.

    I dont know what class or methods will be called
    If you don't know what you want to do, how can anyone know? Can you explain?

    Are you asking how to call the constructor for the TicTacToe class?
    You can do that by using the new statement. There is an example in post#6
    TheClassYouWantToCreate tcls = new TheClassYouWantToCreate(); // create an instance calling the class's constructor
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CALLING ANOTHER PROGRAM

    It would be like this

    String click = e.getActionCommand();
        if (click.equals("Versus Mode"))
    {
     TicTacToe ttt =new TicTacToe();
       ttt.theMethod();
    }
     
        if (click.equals("Versus AI"))
    {
     TicTacToeAI tttai =new TicTacToeAI();
       tttai.theMethod();
    }

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: CALLING ANOTHER PROGRAM

    That could work.
    Did you try it? What happened?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CALLING ANOTHER PROGRAM

    I've tried combining them in 1 program but there's an error
    1 error found:
    File: E:\FinalProject1.java [line: 54]
    Error: The method theMethod() is undefined for the type FinalProject1.TicTacToe



    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class FinalProject1 extends JFrame implements ActionListener
    {
     
     
     public FinalProject1()
     {
    MenuBar menuBar = new MenuBar();
    setMenuBar(menuBar);
     
    Menu menuFile = new Menu("Game", true);
    menuBar.add(menuFile);
    MenuItem menuTTT = new MenuItem("Versus Mode");
    menuFile.add(menuTTT);
    MenuItem menuTTTAI = new MenuItem("Versus AI");
    menuFile.add(menuTTTAI);
     
    Menu menuAbout = new Menu("About", true);
    menuBar.add(menuAbout);
    MenuItem menuAboutTicTacToe = new MenuItem("About TicTacToe");
    menuAbout.add(menuAboutTicTacToe);
     
    menuTTT.addActionListener(this);
    menuTTTAI.addActionListener(this);
    menuAboutTicTacToe.addActionListener(this);
     
    menuTTT.setActionCommand("Versus Mode");
    menuTTTAI.setActionCommand("Versus AI");
    menuAboutTicTacToe.setActionCommand("About");
     }
     
    public void createAndShow()
     {
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setPreferredSize(new Dimension(100, 80));
      setLocation((1000/2), (400/2));
      pack();
      setVisible(true);
     }
     
     
     
     
    public void actionPerformed(ActionEvent e)
    {
     
    String click = e.getActionCommand();
        if (click.equals("Versus Mode"))
    {
      TicTacToe ttt =new TicTacToe();
       ttt.theMethod();    
    }
     
        if (click.equals("Versus AI"))
    {
        //TicTacToeAI tttai =new TicTacToeAI();
      // tttai.theMethod();
    }
     
     
        if (click.equals("About"))
    {
        String message = "Elson's TicTacToe\nComputer Programming II\nCopyright 2011\nAll rights reserved";
        JOptionPane.showMessageDialog(null,message,
        "The Programmer", JOptionPane.INFORMATION_MESSAGE);
    }
    }
     
     
     
    //**********************************************************************************************************************************
     
     
    public class TicTacToe extends JFrame implements ActionListener
    {
     JPanel panel;
     JButton[] button;
     private int count = 0;
     private int sign = 0;
     private boolean win = false;
     private String player = "";
     private int p1wins = 0;
     private int p2wins = 0;
     
     public TicTacToe()
     {
      panel = new JPanel();
      panel.setLayout(new GridLayout(3,4));
      this.add(panel);
      button = new JButton[9];
      for(int i = 0; i <= 8; i++){
       button[i] = new JButton();
       panel.add(button[i]);
       button[i].setEnabled(true);
       button[i].addActionListener(this);
      }
      //this.pack();
      //this.setDefaultCloseOperation(EXIT_ON_CLOSE);
      //this.setVisible(true);
     }
     
     
     
     public void createAndShow()
     {
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setPreferredSize(new Dimension(300, 400));
      setLocation((1000/2), (400/2));
      pack();
      setVisible(true);
     }
     
     public void actionPerformed(ActionEvent e)
     {
      count++;
      for(int i =0; i <=8; i++){
       if(button[i] == e.getSource()){
        if(sign%2 == 0){
         button[i].setText("X");
         button[i].setEnabled(false);
        }
        else{
         button[i].setText("O");
         button[i].setEnabled(false);
        } 
       }
      }
     
      // The Players (turns)
      if(count%2==0){
        player = "P2";
           } 
      else{
        player = "P1";
          }
     
     
      checkWinner();
      if(count >= 9){
       JOptionPane.showMessageDialog(null, "Draw Game!");
       for(int j = 0; j<= 8; j++){
        button[j].setText("");
        button[j].setEnabled(true);
       }
       count = 0;
       sign = 0;
       return;
      }
      sign++;
     
      // Show Player Wins
      if (win == true){
       JOptionPane.showMessageDialog(null, player + " WINS!");
     
       newGameDialog();
       } 
     
     }
     
     public boolean checkWinner()
     {
      // horizontal
       if( button[0].getText() == button[1].getText() && button[1].getText() == button[2].getText() && button[0].getText() != ""){
         win = true;
       }
       else if(button[3].getText() == button[4].getText() && button[4].getText() == button[5].getText() && button[3].getText() != ""){
         win = true;
       }
       else if(button[6].getText() == button[7].getText() && button[7].getText() == button[8].getText() && button[6].getText() != ""){
         win = true;
       }
       // vertical
       else if(button[0].getText() == button[3].getText() && button[3].getText() == button[6].getText() && button[0].getText() != ""){
         win = true;
       }
       else if(button[1].getText() == button[4].getText() && button[4].getText() == button[7].getText() && button[1].getText() != ""){
         win = true;
       }
       else if(button[2].getText() == button[5].getText() && button[5].getText() == button[8].getText() && button[2].getText() != ""){
         win = true;
       }
       // diagonal
       else if(button[0].getText() == button[4].getText() && button[4].getText() == button[8].getText() && button[0].getText() != ""){
         win = true;
       }
       else if(button[2].getText() == button[4].getText() && button[4].getText() == button[6].getText() && button[2].getText() != ""){
         win = true;
       }
     
       return true;
     }
     
     
     public void newGameDialog(){
       if(player.equals("P1"))
         p1wins++;
       else
         p2wins++;
     
       int response = JOptionPane.showConfirmDialog(null, "Do you want a new game?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
       if(response == JOptionPane.YES_OPTION)
       reset();
       else{
       System.exit(0);
       }
     
     }
     
       public void reset(){
         for(int i=0;i<=8;i++){
           button[i].setText("");
           button[i].setEnabled(true);
         }
     
         win = false;
         count = 0;
         sign = 0;
     
     
     
     
     
       }
    }
     
     
     
    public static void main(String[] args)
     {
      FinalProject1 frame = new FinalProject1();
      frame.createAndShow();
     }
     
    }

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: CALLING ANOTHER PROGRAM

    The method theMethod() is undefined
    The compiler can not find a method named: theMethod() in the class: TicTacToe
    What method in the class are you trying to call? Check the class definition for the methods that it defines.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CALLING ANOTHER PROGRAM

    I want to call
    public TicTacToe()
     {
      panel = new JPanel();
      panel.setLayout(new GridLayout(3,4));
      this.add(panel);
      button = new JButton[9];
      for(int i = 0; i <= 8; i++){
       button[i] = new JButton();
       panel.add(button[i]);
       button[i].setEnabled(true);
       button[i].addActionListener(this);
      }
      //this.pack();
      //this.setDefaultCloseOperation(EXIT_ON_CLOSE);
      //this.setVisible(true);
     }
     
     
     
     public void createAndShow()
     {
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setPreferredSize(new Dimension(300, 400));
      setLocation((1000/2), (400/2));
      pack();
      setVisible(true);
     }
     
     public void actionPerformed(ActionEvent e)
     {
      count++;
      for(int i =0; i <=8; i++){
       if(button[i] == e.getSource()){
        if(sign%2 == 0){
         button[i].setText("X");
         button[i].setEnabled(false);
        }
        else{
         button[i].setText("O");
         button[i].setEnabled(false);
        } 
       }
      }
     
      // The Players (turns)
      if(count%2==0){
        player = "P2";
           } 
      else{
        player = "P1";
          }
     
     
      checkWinner();
      if(count >= 9){
       JOptionPane.showMessageDialog(null, "Draw Game!");
       for(int j = 0; j<= 8; j++){
        button[j].setText("");
        button[j].setEnabled(true);
       }
       count = 0;
       sign = 0;
       return;
      }
      sign++;
     
      // Show Player Wins
      if (win == true){
       JOptionPane.showMessageDialog(null, player + " WINS!");
     
       newGameDialog();
       } 
     
     }
     
     public boolean checkWinner()
     {
      // horizontal
       if( button[0].getText() == button[1].getText() && button[1].getText() == button[2].getText() && button[0].getText() != ""){
         win = true;
       }
       else if(button[3].getText() == button[4].getText() && button[4].getText() == button[5].getText() && button[3].getText() != ""){
         win = true;
       }
       else if(button[6].getText() == button[7].getText() && button[7].getText() == button[8].getText() && button[6].getText() != ""){
         win = true;
       }
       // vertical
       else if(button[0].getText() == button[3].getText() && button[3].getText() == button[6].getText() && button[0].getText() != ""){
         win = true;
       }
       else if(button[1].getText() == button[4].getText() && button[4].getText() == button[7].getText() && button[1].getText() != ""){
         win = true;
       }
       else if(button[2].getText() == button[5].getText() && button[5].getText() == button[8].getText() && button[2].getText() != ""){
         win = true;
       }
       // diagonal
       else if(button[0].getText() == button[4].getText() && button[4].getText() == button[8].getText() && button[0].getText() != ""){
         win = true;
       }
       else if(button[2].getText() == button[4].getText() && button[4].getText() == button[6].getText() && button[2].getText() != ""){
         win = true;
       }
     
       return true;
     }
     
     
     public void newGameDialog(){
       if(player.equals("P1"))
         p1wins++;
       else
         p2wins++;
     
       int response = JOptionPane.showConfirmDialog(null, "Do you want a new game?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
       if(response == JOptionPane.YES_OPTION)
       reset();
       else{
       System.exit(0);
       }
     
     }
     
       public void reset(){
         for(int i=0;i<=8;i++){
           button[i].setText("");
           button[i].setEnabled(true);
         }
     
         win = false;
         count = 0;
         sign = 0;
     
     
     
     
     
       }
    }

    so this must be TicTacToe()

    so I change 'theMethod()' to 'TicTacToe()'
    {
      TicTacToe ttt =new TicTacToe();
       ttt.TicTacToe();    
    }

    but same error:

    1 error found:
    File: E:\FinalProject1.java [line: 54]
    Error: The method TicTacToe() is undefined for the type FinalProject1.TicTacToe

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: CALLING ANOTHER PROGRAM

    I want to call
    You can call either a class's constructor or a method in a class or both.
    What method in the class do you want to call? Look at the class definition and find the method you want to call.

    The method TicTacToe() is undefined
    The compiler can not find the method TicTacToe() in the class: TicTacToe.


    Where did you get these classes? Can you ask the person that wrote the classes how to use them?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Program not compiling + calling methods help
    By alex067 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 11th, 2012, 07:41 AM
  2. Calling upon methods
    By jonathanfox in forum Java Theory & Questions
    Replies: 4
    Last Post: August 3rd, 2012, 11:58 AM
  3. calling .rpt from jsp
    By nagaraj200788@gmail.com in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: July 13th, 2012, 02:15 AM
  4. Replies: 1
    Last Post: November 21st, 2011, 06:22 AM
  5. about calling sub class
    By pokuri in forum Object Oriented Programming
    Replies: 3
    Last Post: January 11th, 2011, 03:30 PM