简体   繁体   English

将java图形实现到宾果卡游戏中

[英]Implementing java graphics into bingo card game

I started with a class that has all the methods and constructors which are doing all the work for the bingo game. 我从一个拥有所有方法和构造函数的类开始,这些方法和构造函数正在为宾果游戏做所有工作。 Then had a "tester class" which would print out and update the bingo card through the use of System.out.print. 然后有一个“测试器类”,它将打印出来并通过使用System.out.print更新宾果卡。 Now I need to add some graphic for the bingo game. 现在我需要为宾果游戏添加一些图形。 I am supposed to use my first to classes to support a new class to draw the card every time there is an update. 每次有更新时,我应该使用我的第一个类来支持新类来绘制卡片。

I was able to do the first iteration of the bingo card graphics with a separate code from my original 2: 我能够使用与原始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;
    }


}
}

which I used with this code: 我用这个代码:

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

    }
}

but what i dont understand is how to implement this into my original two codes. 但我不明白的是如何将其实现为我原来的两个代码。 Which are: 哪个是:

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

and the tester: 和测试人员:

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!");
    } 

}

i need to implement the BingoComponent into the original two and have the card update each time. 我需要将BingoComponent实现为原始的两个并且每次更新卡。

The basic idea remains the same. 基本思想保持不变。

You need some kind of model which is maintaining the state of the bingo card, you need some kind of view that displays this model and some kind of control/manager which managers the updates to the model... 你需要一种维持宾果卡状态的模型,你需要某种显示这个模型的视图和某种控制/管理器来管理模型的更新......

In you BingoComponent 's paintComponent method, you do this ... Bingo_Card test_card = new Bingo_Card(); BingoComponentpaintComponent方法中,你这样做... Bingo_Card test_card = new Bingo_Card(); ... ...

I would say this is a bad idea, as you are restting the state of the card/model each time the component is painted, this isn't what you want. 我会说这是一个坏主意,因为每次绘制组件时都要重置卡片/模型的状态,这不是你想要的。 Instead, you want to maintain a single reference to the card, which you then use the paintComponent to render... 相反,您希望保持对卡的单个引用,然后使用paintComponent来呈现...

Instead, I'd be tempted to do something like this instead... 相反,我很想做这样的事情......

public class BingoComponent extends JComponent {
    private Bingo_Card test_card;

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

    /*...*/

}    

This means that that instance doesn't change, but each time we change it's state, we can re-render it. 这意味着该实例不会更改,但每次我们更改它的状态时,我们都可以重新呈现它。

The next part will come down to how you want to implement it. 下一部分将归结为您希望如何实现它。 If it was me, I would add ChangeListener support to the Bingo_Card , so that the UI could monitor for changes to the card and update itself accordingly, but this might be a little out of your reach just now. 如果是我,我会将ChangeListener支持添加到Bingo_Card ,以便UI可以监控卡的更改并相应地更新自己,但这可能有点超出您现在的范围。

You are going to need some way for the user enter a value into your UI. 您需要某种方式让用户在UI中输入值。 For this you could use a JTextField . 为此,您可以使用JTextField You can either attach an ActionListener directly to the field, so that each time the user presses Enter you will receive notification about it and/or add a JButton , which the user could click (with an attached ActionListener so you know when the user clicks the button). 您可以将ActionListener直接附加到该字段,这样每次用户按Enter键时,您将收到有关它的通知和/或添加一个JButton ,用户可以单击该JButton (使用附加的ActionListener以便您知道用户何时单击按钮)。

At this point, you need to take the value and update the model, just like you have in your BINGOFINAL class, but instead, you would to update the UI... 此时,您需要获取值并更新模型,就像您在BINGOFINAL类中一样,但您需要更新UI ...

For example... 例如...

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

Now, personally, I'd prefer it if the model provided some kind of notification about changes to it's internal state, but lets try and keep it simpler for the time begin... 现在,就个人而言,如果模型提供了关于其内部状态变化的某种通知,我更喜欢它,但是让我们试着让它在开始时更简单...

Check out Creating a GUI with Swing for more details 有关更多详细信息,请查看使用Swing创建GUI

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM