简体   繁体   English

在Java中的2D网格数组中绘制圆

[英]Drawing circles in 2d grid array in java

I understand how to make 2d array grid. 我了解如何制作2D阵列网格。 I experimented to put random numbers in it but i do not know how to draw them on jframe. 我尝试将随机数放入其中,但我不知道如何在jframe上绘制它们。 For instance, 0 for red circle , 1 for green circle and so on. 例如,0代表红色圆圈,1代表绿色圆圈,依此类推。 I need to figure out how represent them in grid manner too. 我还需要弄清楚如何以网格方式表示它们。

public class Game {


    public static void initGrid(){
        //clumn and row 4 x 4
        int col = 4;
        int row = 4;

        //initialize 2d grid array
        int[][] a = new int[row][col];

        Random rand = new Random();

        //for loop to fill it with random number
        for(int x = 0 ; x < col ; x++) {
            for(int y = 0; y < row; y++) {
                a[x][y] = (int) rand.nextInt(4);
                System.out.print(a[x][y]);
            }//inner for
            System.out.println();
        }//outer for


    }//method


    public static void main(String[] args){
        initGrid();
    }


}

I understand JFrame and JPanel as far as drawing on empty canvas but not the way i want. 我了解JFrame和JPanel甚至可以在空画布上绘制,但不了解我想要的方式。 I want to combine both code but my knowledge is limited. 我想结合两个代码,但我的知识有限。

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.geom.Ellipse2D;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

  @SuppressWarnings("serial")
  public class Game2 extends JPanel{

    @Override
    public void paint(Graphics g){
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.RED);
        g2d.fillOval(0, 0, 30, 30);
        g2d.drawOval(0, 50, 30, 30);
        g2d.fillRect(50, 0, 30, 30);
        g2d.drawRect(50, 50, 30, 30);

        g2d.draw(new Ellipse2D.Double(0, 100, 30 ,30));
    }

     public static void main(String[] args){
        JFrame frame = new JFrame("Mini Tennis");
        frame.add(new Game2());
        frame.setSize(300, 300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
} 

My suggestion is: 我的建议是:

  1. Pass the Game class as a parameter to the constructor of Game2 and store it as a local variable in the Game2 class as illustrated below: Game类作为参数传递给Game2的构造Game2 ,并将其作为本地变量存储在Game2类中,如下所示:

     Game game; public Game2(Game game){ this.game = game; //Rest of your constructor. } 
  2. Next you declare a getter method in the Game class to retrieve the array which stores the position grid like as below: 接下来,在Game类中声明一个getter方法,以检索存储位置网格的数组,如下所示:

     public int[][] getPositions(){ return this.a; } 
  3. Create a method that will return the colour to paint based on the int value stored as the element of the grid like this: 创建一个方法,该方法将基于存储为网格元素的int值将颜色返回到绘制,如下所示:

     private Color getColor(int col){ switch(col){ case 0: return Color.red; case 1: . . . . } } 
  4. Now instead of overriding the paint method of your Game2 class override the paintComponent and draw the circles in the paintComponent method as illustrated(Here I have considered the circles to be of 30px diameter with a gap of 20px between them): 现在,而不是覆盖Game2类的paint方法, Game2覆盖paintComponent并在paintComponent方法中绘制圆,如图所示(此处,我认为圆的直径为30px,它们之间的间隙为20px):

     public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; int[][] pos = this.game.getPositions(); for(int i = 0; i < pos.length; i++){ for(int j = 0; j < pos[i].length; j++){ g2d.setColor(getColor(pos[i][j])); g2d.fillOval(i*50, j*50, 30, 30); } } } 

I hope this will solve your problem of accessing the Game representing the model from the Game2 class representing the view. 我希望这能解决您从代表视图的Game2类访问代表模型的Game问题。

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

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