0

私が持っているものに基づいて、コマンドラインのargsまたはstdinをmainからdrawOnGridという名前のクラスに渡す方法の例を誰かに教えてもらえますか?私はそれを理解するのに苦労しています。基本的には「g.drawString(argOne、10、10);」を使用する必要があります。drawOvalまたはdrawLineの代わりに。コードを同封しました。

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

                }
            }

        }
    }
}
}

とても感謝しております。ありがとうございました。

4

1 に答える 1

2

これを試して:

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

                }
            }

        }
    }
}
于 2012-09-04T17:54:56.767 に答える