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

Thread: Passing command line args from main to another class

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

    Default Passing command line args from main to another class

    Hello everyone.
    As the title mentions, is this possible? I have been tasked with passing command line args from main to the class drawOnGrid. I will have to pass much more but figured starting here would help later on. Could someone show me an example? I would appreciate it and here is what I have.
    import java.awt.*;
    import javax.swing.*;
     
    public class Tictactoe extends JFrame {
     
        //construct a figurePanel
        public Tictactoe() {
     
            Container RandomTicTacToePanel = getContentPane();
            RandomTicTacToePanel.setLayout(new GridLayout(3, 3));
     
     
            for (int i = 0; i < 9; i++) {
                RandomTicTacToePanel.add(new drawOnGrid());
            }
        }
     
        //Main method
        public static void main(String[] args) {
     
            Tictactoe Tframe = new Tictactoe();
            Tframe.setTitle("Tic Tac Toe Panel: Random Entries");
            Tframe.setSize(350, 350);
            Tframe.setResizable(true);
            Tframe.setLocationRelativeTo(null);
            Tframe.setVisible(true);
            Tframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
     
        class drawOnGrid extends JPanel {
     
            //overide the paintComponent
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
     
                int random = (int) (Math.random() * 3);
     
                for (int i = 0; i < 3; i++) {
                    for (int j = 0; j < 3; j++) {
     
                        if (random == 0) {
                            System.out.print(" ");
                        } else if (random == 1) {
                            g.drawOval(10, 10, getWidth() - 20, getHeight() - 20);
                        } else if (random == 2) {
                            g.drawLine(10, 10, getWidth() - 10, getHeight() - 10);
                            g.drawLine(getWidth() - 10, 10, 10, getHeight() - 10);
     
                        }
                    }
     
                }
            }
        }
    }


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

    Default Re: Passing command line args from main to another class

    The command line args are in the String array passed to the main() method: named args in your code.
    Use the normal way to pass an argument to any constructor or method: put the variable inside the ()s that follows the method's name.

    Your code is full of method calls that pass args to methods.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    csharp100 (September 4th, 2012)

  4. #3
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Passing command line args from main to another class

    Quote Originally Posted by Norm View Post
    The command line args are in the String array passed to the main() method: named args in your code.
    Use the normal way to pass an argument to any constructor or method: put the variable inside the ()s that follows the method's name.

    Your code is full of method calls that pass args to methods.
    Thanks Norm! Didn't really look at it that way.

Similar Threads

  1. Not sure if run() or main(String[] args)
    By Brock Lee in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 25th, 2012, 12:19 PM
  2. Why main(String args[]) method needs to be public?
    By rohan22 in forum Java Theory & Questions
    Replies: 11
    Last Post: December 7th, 2011, 11:41 PM
  3. sdtin/command-line
    By TorontoJava1984 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 16th, 2011, 01:51 PM
  4. Replies: 10
    Last Post: September 16th, 2011, 07:49 PM
  5. Java main method args
    By mattxo in forum Java Theory & Questions
    Replies: 3
    Last Post: May 16th, 2011, 09:45 AM