2013-08-27 48 views
0

我從一個擁有所有爲賓果遊戲工作的所有方法和構造函數的類開始。然後有一個「測試員班」,通過使用System.out.print打印和更新賓果卡。現在我需要爲賓果遊戲添加一些圖形。我應該使用我的第一堂課來支持一個新班級在每次更新時抽出卡片。將java圖形實現爲賓果卡遊戲

我能夠從做賓果卡顯卡採用單獨的代碼的第一次迭代我原來的2:

import java.awt.Rectangle; 
import javax.swing.JComponent; 
import java.awt.Color; 
import java.awt.Font; 
import java.awt.geom.Ellipse2D; 

public class BingoComponent extends JComponent { 
    public void paintComponent(Graphics g){ 
     Graphics2D g2 = (Graphics2D) g;  
     Rectangle box = new Rectangle(28,105,80,80); 
    for(int j = 0;j<5;j++){ 
     g2.draw(box); 
     for(int i = 0;i<4;i++){ 
      box.translate(80,0); 
      g2.draw(box); 
     } 
     box.translate(-320,80); 
    } 

    g2.setPaint(Color.BLUE); 
    g2.setFont(new Font("Arial", Font.BOLD,44)); 
    g2.drawString("B",50,100); 
    g2.drawString("I",140,100); 
    g2.drawString("N",215,100); 
    g2.drawString("G",290,100); 
    g2.drawString("O",370,100); 

    g2.setPaint(Color.RED);   
    Ellipse2D.Double ellipse = new Ellipse2D.Double(198,275,60,60); 
    g2.draw(ellipse); 
    g2.fill(ellipse); 

    Bingo_Card test_card = new Bingo_Card(); 
    g2.setPaint(Color.BLACK); 
    g2.setFont(new Font("Times", Font.PLAIN,24)); 
    int x_location = 60; 
    int y_location = 150; 
    int number_value; 
    for(int j = 0;j<5;j++){ 
     number_value = test_card.get_number(j); 
     g2.drawString(""+number_value,x_location,y_location); 
     x_location += 80; 
    } 
    x_location = 60; 
    y_location += 80; 
    for(int j = 5;j<10;j++){ 
     number_value = test_card.get_number(j); 
     g2.drawString(""+number_value,x_location,y_location); 
     x_location += 80; 
    } 
    x_location = 60; 
    y_location += 80; 
    for(int j = 10;j<15;j++){ 
     number_value = test_card.get_number(j); 
     if(number_value!=0){ 
     g2.drawString(""+number_value,x_location,y_location); 
     } 
     x_location += 80; 
    } 
    x_location = 60; 
    y_location += 80; 
    for(int j = 15;j<20;j++){ 
     number_value = test_card.get_number(j); 
     g2.drawString(""+number_value,x_location,y_location); 
     x_location += 80; 
    } 
    x_location = 60; 
    y_location += 80; 
    for(int j = 20;j<25;j++){ 
     number_value = test_card.get_number(j); 
     g2.drawString(""+number_value,x_location,y_location); 
     x_location += 80; 
    } 


} 
} 

我與此代碼中使用:

public class makecard { 
    public static void main(String args[]){   
     JFrame frame = new JFrame(); 

     frame.setSize(800,800); 
     frame.setTitle("BINGO Card"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     BingoComponent component = new BingoComponent(); 
     frame.add(component); 

     frame.setVisible(true); 

    } 
} 

但我不明白是如何實現這個到我原來的兩個代碼。 它們是:

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Rectangle; 
import javax.swing.JComponent; 
import java.awt.Color; 
import java.awt.Font; 
import java.awt.geom.Ellipse2D; 

public class Bingo_Card { 

private ArrayList<Integer> bingo_card; 
public boolean bingo = false; 

public Bingo_Card(){ 
    /*ArrayList<Integer>*/ bingo_card = new ArrayList<>(); 
    ArrayList<Integer> columnBlist = new ArrayList<>(); 
    for(int i = 1;i<=15;i++){ 
     columnBlist.add(i); 
    } 
    Collections.shuffle(columnBlist); 
    ArrayList<Integer> columnB = new ArrayList<>(); 
    for(int j = 0;j<5;j++){ 
     columnB.add(columnBlist.get(j)); 
    } 

    ArrayList<Integer> columnIlist = new ArrayList<>(); 
    for(int i = 16;i<=30;i++){ 
     columnIlist.add(i); 
    } 
    Collections.shuffle(columnIlist); 
    ArrayList<Integer> columnI = new ArrayList<>(); 
    for(int j = 0;j<5;j++){ 
     columnI.add(columnIlist.get(j)); 
    } 

    List<Integer> columnNlist = new ArrayList<>(); 
    for(int i = 31;i<=45;i++){ 
     columnNlist.add(i); 
    } 
    Collections.shuffle(columnNlist); 
    ArrayList<Integer> columnN = new ArrayList<>(); 
    for(int j = 0;j<4;j++){ 
     columnN.add(columnNlist.get(j)); 
    } 
    columnN.add(2,0); 



    List<Integer> columnGlist = new ArrayList<>(); 
    for(int i = 46;i<=60;i++){ 
     columnGlist.add(i); 
    } 
    Collections.shuffle(columnGlist); 
    ArrayList<Integer> columnG = new ArrayList<>(); 
    for(int j = 0;j<5;j++){ 
     columnG.add(columnGlist.get(j)); 
    } 

    List<Integer> columnOlist = new ArrayList<>(); 
    for(int i = 61;i<=75;i++){ 
     columnOlist.add(i); 
    } 
    Collections.shuffle(columnOlist); 
    ArrayList<Integer> columnO = new ArrayList<>(); 
    for(int j = 0;j<5;j++){ 
     columnO.add(columnOlist.get(j)); 
    } 

    for(int x=0;x<5;x++){ 
    bingo_card.add(columnB.get(x)); 
    bingo_card.add(columnI.get(x)); 
    bingo_card.add(columnN.get(x)); 
    bingo_card.add(columnG.get(x)); 
    bingo_card.add(columnO.get(x)); 
    } 
} 

public int get_number(int index){ 
    int number = bingo_card.get(index); 
    return(number); 
} 

public void print_card(){ 
    System.out.println(" B I N G O "); 
    System.out.println("----------------"); 
    for(int a = 0;a<5;a++){ 
     if(bingo_card.get(a)<10){ 
      System.out.print("| "+bingo_card.get(a)); 
     } 
     else{ 
      System.out.print("| "+bingo_card.get(a)); 
     } 
    } 
    System.out.println(); 
    System.out.println("----------------"); 
    for(int b = 5;b<10;b++){ 
     if(bingo_card.get(b)<10){ 
      System.out.print("| "+bingo_card.get(b)); 
     } 
     else{ 
      System.out.print("| "+bingo_card.get(b)); 
     } 
    } 
    System.out.println(); 
    System.out.println("----------------"); 
    for(int c = 10;c<15;c++){ 
     if(bingo_card.get(c)<10){ 
      System.out.print("| "+bingo_card.get(c)); 
     } 
     else{ 
      System.out.print("| "+bingo_card.get(c)); 
     } 
    } 
    System.out.println(); 
    System.out.println("----------------"); 
    for(int d = 15;d<20;d++){ 
     if(bingo_card.get(d)<10){ 
      System.out.print("| "+bingo_card.get(d)); 
     } 
     else{ 
      System.out.print("| "+bingo_card.get(d)); 
     } 
    } 
    System.out.println(); 
    System.out.println("----------------"); 
    for(int e = 20;e<25;e++){ 
     if(bingo_card.get(e)<10){ 
      System.out.print("| "+bingo_card.get(e)); 
     } 
     else{ 
      System.out.print("| "+bingo_card.get(e)); 
     } 
    } 
    System.out.println(); 
    System.out.println("----------------");   
    } 

    public void check_match(int call_number){ 
     for(int i = 0; i<(bingo_card.size());i++){ 
      if(call_number == bingo_card.get(i)){ 
       bingo_card.set(i,0); 
      } 
     } 
    } 

    public boolean check_bingo(){ 
     if(bingo_card.get(0)==0&bingo_card.get(1)==0&bingo_card.get(2)==0&bingo_card.get(3)==0&bingo_card.get(4)==0){ 
      bingo = true; 
     } 
     else if(bingo_card.get(5)==0&bingo_card.get(6)==0&bingo_card.get(7)==0&bingo_card.get(8)==0&bingo_card.get(9)==0){ 
      bingo = true; 
     } 
     else if(bingo_card.get(10)==0&bingo_card.get(11)==0&bingo_card.get(12)==0&bingo_card.get(13)==0&bingo_card.get(14)==0){ 
      bingo = true; 
     } 
     else if(bingo_card.get(15)==0&bingo_card.get(16)==0&bingo_card.get(17)==0&bingo_card.get(18)==0&bingo_card.get(19)==0){ 
      bingo = true; 
     } 
     else if(bingo_card.get(20)==0&bingo_card.get(21)==0&bingo_card.get(22)==0&bingo_card.get(23)==0&bingo_card.get(24)==0){ 
      bingo = true; 
     } 
     else if(bingo_card.get(0)==0&bingo_card.get(5)==0&bingo_card.get(10)==0&bingo_card.get(15)==0&bingo_card.get(20)==0){ 
      bingo = true; 
     } 
     else if(bingo_card.get(1)==0&bingo_card.get(6)==0&bingo_card.get(11)==0&bingo_card.get(16)==0&bingo_card.get(21)==0){ 
      bingo = true; 
     } 
     else if(bingo_card.get(2)==0&bingo_card.get(7)==0&bingo_card.get(12)==0&bingo_card.get(17)==0&bingo_card.get(22)==0){ 
      bingo = true; 
     } 
     else if(bingo_card.get(3)==0&bingo_card.get(8)==0&bingo_card.get(13)==0&bingo_card.get(18)==0&bingo_card.get(23)==0){ 
      bingo = true; 
     } 
     else if(bingo_card.get(4)==0&bingo_card.get(9)==0&bingo_card.get(14)==0&bingo_card.get(19)==0&bingo_card.get(24)==0){ 
      bingo = true; 
     } 
     else if(bingo_card.get(0)==0&bingo_card.get(6)==0&bingo_card.get(12)==0&bingo_card.get(18)==0&bingo_card.get(24)==0){ 
      bingo = true; 
     } 
     else if(bingo_card.get(4)==0&bingo_card.get(8)==0&bingo_card.get(12)==0&bingo_card.get(16)==0&bingo_card.get(20)==0){ 
      bingo = true; 
     } 
     else{ 
     bingo = false; 
     } 
     return(bingo); 
    } 

和測試:

public class BINGOFINAL { 
public static void main(String args[]){ 
    Bingo_Card test_card = new Bingo_Card(); 
    test_card.print_card(); 

    while(test_card.check_bingo() == false){ 
    System.out.println("Please input the called out number: "); 
    Scanner input = new Scanner(System.in); 
    int call_out = input.nextInt(); 
    test_card.check_match(call_out); 
    test_card.check_bingo(); 
    test_card.print_card(); 
    } 
    System.out.println("BINGO!"); 
    } 

} 

我需要實現BingoComponent到原來的兩個,每次都卡更新。

回答

2

基本概念保持不變。

你需要某種形式的模型,它是維持賓果卡的狀態,你需要某種觀點,即顯示該模型和某種控制/管理器,它經理更新模型...

在你BingoComponentpaintComponent方法,你這樣做...... Bingo_Card test_card = new Bingo_Card(); ...

我會說這是一個糟糕的主意,因爲你是每個組件繪製時間restting卡/模型的狀態,這不是你想要的。相反,你要保持一個參考到卡上,然後您可以使用paintComponent渲染...

相反,我會很想做這樣的事情,而不是...

public class BingoComponent extends JComponent { 
    private Bingo_Card test_card; 

    public BingoComponent(Bingo_Card card) { 
     test_card = card; 
    } 

    /*...*/ 

}  

這意味着該實例不會改變,但每次我們改變它的狀態時,我們都可以重新渲染它。

下一部分將回顧您想要如何實現它。如果是我,我會將ChangeListener支持添加到Bingo_Card,以便UI可以監控卡的更改並相應地進行更新,但是這可能會稍微超出您的範圍。

您將需要某種方式讓用戶在您的UI中輸入一個值。爲此,您可以使用JTextField。您可以將ActionListener直接附加到該字段,以便每次用戶按輸入時,您將收到關於它的通知和/或添加用戶可以點擊的JButton(附帶ActionListener,以便您知道用戶何時點擊按鈕)。

在這一點上,你需要採取的值,並更新模型,就像你在你的BINGOFINAL類,而是,你會更新UI ...

比如...

public void actionPerformed(ActionEvent evt) { 
    try { 
     // inputField is a JTextField 
     // testCard is an instance of Bingo_Card which you 
     // need to create. It is also the same instance 
     // you passed to your BingoComponent 
     int callOut = Integer.parseInt(inputField.getText()); 
     test_card.check_match(call_out); 
     test_card.check_bingo(); 
     test_card.print_card(); 
     // bingoComponent is an instance of BingoComponent 
     // which is begin displayed on the screen... 
     bingoComponent.repaint(); 
    } catch (NumberFormatException exp) { 
     JOptionPane.showMessageDialog(this, inputField.getText() + 
      " is not a valid number", "Error", JOptionPane.ERROR_MESSAGE); 
    } 
} 

現在,就個人而言,我更喜歡它,如果模型提供某種形式的通知,關於變化到它的內部狀態,但讓我們儘量保持簡單的時間開始......

檢查出Creating a GUI with Swing欲瞭解更多詳情