Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8478601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:47:26+00:00 2026-06-10T18:47:26+00:00

Can someone give me an example, based on what I have, on how to

  • 0

Can someone give me an example, based on what I have, on how to pass the command line args or stdin from main to the class named drawOnGrid? I am having a hard time understanding it. Basically I need to use “g.drawString (argOne, 10, 10);” instead of drawOval or drawLine. I have enclosed my code.

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

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) {

    String argOne;
    String argTwo;

    Scanner in = new Scanner(System.in);

    int length = args.length;
    if (length <= 0) {
        System.out.println("Please enter player One's symbol: ");
        argOne = in.nextLine();
        System.out.println("Please enter player Two's symbol: ");
        argTwo = in.nextLine();
        in.close();
    }

    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);

                }
            }

        }
    }
}
}

I would really appreciate it. Thank You.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:47:28+00:00Added an answer on June 10, 2026 at 6:47 pm

    Try this:

    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    
    public class Tictactoe extends JFrame {
    
    //construct a figurePanel
    public Tictactoe(String text) {
    
        Container RandomTicTacToePanel = getContentPane();
        RandomTicTacToePanel.setLayout(new GridLayout(3, 3));
    
    
        for (int i = 0; i < 9; i++) {
            RandomTicTacToePanel.add(new drawOnGrid(text));
        }
    }
    
    //Main method
    public static void main(String[] args) {
    
        String argOne = null; // Init with null
        String argTwo = null; // Init with null
    
        Scanner in = new Scanner(System.in);
    
        int length = args.length;
        if (length <= 0) {
            System.out.println("Please enter player One's symbol: ");
            argOne = in.nextLine();
            System.out.println("Please enter player Two's symbol: ");
            argTwo = in.nextLine();
            in.close();
        } else if(length == 1) {
            argOne = args[0];
        } else if(length == 2) {
            argOne = args[0];
            argTwo = args[1];
        }
    
        Tictactoe Tframe = new Tictactoe(argOne);
        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 {
    
        private String text;
    
        public drawOnGrid(String text) {
           this.text = text;
        }
    
        //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);
    
                    }
                }
    
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hope that someone out there can help with this! I'll give an example based
Can someone give an example for finding greatest common divisor algorithm for more than
Can someone give an example of how thread deadlock can be caused in the
Can someone give me an example of how I would delete a row in
can someone give me an example of what a calculated control is in ms-access?
Can someone give me a simple example involving threads in this manner, please. Problem
can someone please give me an example on the internet of how the system
Can someone give me an example of creating a custom set of an Event
I'm making the switch from CodeIgniter to Symfony 2. Can someone please give me
Can someone give an example of a good time to actually use unsafe and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.