你如何在Java中创建可视化网格

时间:2014-08-02 16:00:42

标签: java swing jframe

所以我是Swing和JFrames的新手,所以我很喜欢这个主题。我试图创建一个生命游戏应用程序和视觉显示它。他们在数学和#34;已经完成,但我不清楚如何直观地显示它。我正在寻找的是某种我可以着色的网格。这就是我所说的:http://en.wikipedia.org/wiki/Conway' s_Game_of_Life

2 个答案:

答案 0 :(得分:0)

您可以仅使用标准的Java swing库为范围创建网格。 您只需要绘制网格布局的drawLine方法,然后使用fillOvall绘制一些单元格。 这是一个完整且正在运行的示例:

import javax.swing.*;
public class MyGrid extends JApplet  {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setTitle("Grid Panel Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JApplet applet = new MyGrid();
        applet.init();
        frame.getContentPane().add(applet);
        frame.pack();
        frame.setVisible(true);
    }//end main method
    public void init() {
        JPanel panel = new GridPanel();
        getContentPane().add(panel);
    }//end init method
    class GridPanel extends JPanel {
          int n = 30;   //Number of cells of my squeare grid

          public GridPanel() {
                setPreferredSize(new Dimension(480, 480));
                setBackground(Color.BLACK);
          }

          public void paintComponent(Graphics g) {
              super.paintComponent(g);
              Graphics2D g2D = (Graphics2D)g;
              g2D.setColor(Color.lightGray);
              //Set the cell dimension
              int p=0;
              int c=16;
              int len = c*n;
              //Draw the grid
              for (int i = 0; i <= n; i++) {
                  g2D.drawLine(0, p, len, p);
                  g2D.drawLine(p, 0, p, len);
                  p += c;
              }
              //You can paint the (i,j) cell with another color in this way  
                 int i=10;
                 int j=20;
                 g2D.setColor(Color.GREEN);
                 int x = i*c;
                 int y = j*c;
                 g2D.fillOval(x, y, c, c);

          }//end paintComponent
     }//end  inner class GridPanel        
 }//end class

将其复制到文件名MyGrig.java中 然后编译它javac MyGrid.java 最后是java MyGrid

答案 1 :(得分:0)

可能的解决方案就是这样。我为绘制单元格的逻辑创建了一个新类ChangeCellsClass,我在适当的事件中每秒调用它的方法。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;

public class MyGrid extends JApplet  {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setTitle("Grid Panel Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JApplet applet = new MyGrid();
        applet.init();
        frame.getContentPane().add(applet);
        frame.pack();
        frame.setVisible(true);
    }//end main method

    public void init() {
        JPanel panel = new GridPanel();
        getContentPane().add(panel);
    }//end init method


    class GridPanel extends JPanel implements ActionListener {
          int n = 30;   //Number of cells of my squeare grid
          boolean[][] cells; //Grid data model

          public GridPanel() {
                setPreferredSize(new Dimension(480, 480));
                setBackground(Color.BLACK);

                //Initialize data model
                cells = new boolean[n][n];

                //Every seconds fire event for update the stste
                Timer timer = new Timer(1000, this);
                timer.start();              
          }

          public void paintComponent(Graphics g) {
              super.paintComponent(g);
              Graphics2D g2D = (Graphics2D)g;

              g2D.setColor(Color.lightGray);
              //Set the cell dimension
              int p=0;
              int c=16;
              int len = c*n;
              //Draw the grid
              for (int i = 0; i <= n; i++) {
                  g2D.drawLine(0, p, len, p);
                  g2D.drawLine(p, 0, p, len);
                  p += c;
              }

              //Draw active cells
              g2D.setColor(Color.GREEN);
              for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    if (cells[i][j]) {
                       int x = i*c;
                       int y = j*c;
                       g2D.fillOval(x, y, c, c);
                    }
                }
              }

          }//end paintComponent

          //Action performed Event 
          public void actionPerformed(ActionEvent e) {
              ChangeCellsClass ccc = new ChangeCellsClass();
              cells = ccc.setCells(cells);
              repaint();
          }//end actionPerformed          
     }//end  inner class GridPanel        
 }//end class 



//This is your class for compute active cells. ChangeCellsClass.java
 public class ChangeCellsClass  {

    public ChangeCellsClass()  {
       //Some initialization code ....
    }//end constructor

    public boolean[][] setCells(boolean[][] cells) {
        int n = 30; //you may obtain this value dinamically from cells matrix   
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
               cells[i][j] = Math.random() < 0.1;
            }
        } 
        return cells;
    }//end method
}//end class