2012-09-04 32 views
0

有人能給我一個例子,基於我有什麼,關於如何將命令行args或stdin從main傳遞給名爲drawOnGrid的類?我很難理解它。基本上我需要使用「g.drawString(argOne,10,10);」而不是drawOval或drawLine。我附上我的代碼。從主到類傳遞參數和stdin

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

       } 
      } 

     } 
    } 
} 
} 

我真的很感激它。謝謝。

+0

你可以通過'直接args',和/或直接通過任何方法使用獲得的參數相同的機制通過本地變量的方法。 –

+0

您可以爲'drawOnGrid'類創建'String []'屬性,並通過構造函數或setter傳入'args'。 – 2012-09-04 17:49:39

+0

@JackManey你能告訴我一個非精細的例子嗎? – Clint

回答

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

       } 
      } 

     } 
    } 
} 
+0

謝謝!你已經「箭頭」了。 – Clint

+0

不客氣! – aymeric

+0

嘗試了你做了什麼,並在這一行上得到一個錯誤'Tictactoe Tframe = new Tictactoe(argOne);'說明'錯誤:變量argOne可能未被初始化 Tictactoe Tframe = new Tictactoe(argOne); 「有什麼想法? – Clint