如何用Java制作可视化网格

如何用Java制作可视化网格,java,swing,jframe,Java,Swing,Jframe,所以我对Swing和JFrames是个新手,所以我对这个话题有点纠结。我正在尝试创建一个生活游戏应用程序,并将其可视化显示。它们作为“数学”的基础已经完成了,但我不知道如何直观地展示它。我要找的是某种我可以着色的网格。这就是我所说的:“生活的游戏”您可以仅使用标准java swing库为您的范围创建网格。 您所需要的只是网格布局的drawLine方法和绘制某些单元格的fillOvall方法。 这是一个完整的运行示例: import javax.swing.*; public class MyGr

所以我对Swing和JFrames是个新手,所以我对这个话题有点纠结。我正在尝试创建一个生活游戏应用程序,并将其可视化显示。它们作为“数学”的基础已经完成了,但我不知道如何直观地展示它。我要找的是某种我可以着色的网格。这就是我所说的:“生活的游戏”

您可以仅使用标准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
import javax.swing.*;
公共类MyGrid扩展了JApplet{
公共静态void main(字符串[]args){
JFrame=新JFrame();
frame.setTitle(“网格面板示例”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet=new MyGrid();
applet.init();
frame.getContentPane().add(小程序);
frame.pack();
frame.setVisible(true);
}//端主方法
公共void init(){
JPanel panel=新的GridPanel();
getContentPane().add(面板);
}//end init方法
类GridPanel扩展了JPanel{
int n=30;//我的尖叫网格的单元数
公众谘询委员会({
setPreferredSize(新尺寸(480480));
挫折背景(颜色:黑色);
}
公共组件(图形g){
超级组件(g);
Graphics2D g2D=(Graphics2D)g;
g2D.setColor(颜色为浅灰色);
//设置单元格尺寸
int p=0;
int c=16;
int len=c*n;
//画网格

对于(int i=0;i一个可能的解决方案是这样的。我为绘制单元格的逻辑创建一个新的类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 
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入java.awt.geom.*;
公共类MyGrid扩展了JApplet{
公共静态void main(字符串[]args){
JFrame=新JFrame();
frame.setTitle(“网格面板示例”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet=new MyGrid();
applet.init();
frame.getContentPane().add(小程序);
frame.pack();
frame.setVisible(true);
}//端主方法
公共void init(){
JPanel panel=新的GridPanel();
getContentPane().add(面板);
}//end init方法
类GridPanel扩展JPanel实现ActionListener{
int n=30;//我的尖叫网格的单元数
布尔[][]单元格;//网格数据模型
公众谘询委员会({
setPreferredSize(新尺寸(480480));
挫折背景(颜色:黑色);
//初始化数据模型
单元格=新布尔值[n][n];
//每秒钟触发一次事件以更新stste
定时器定时器=新定时器(1000,此);
timer.start();
}
公共组件(图形g){
超级组件(g);
Graphics2D g2D=(Graphics2D)g;
g2D.setColor(颜色为浅灰色);
//设置单元格尺寸
int p=0;
int c=16;
int len=c*n;
//画网格

对于(int i=0;我想你会想搜索这个网站,因为我知道在这种类型的应用程序中有很多问题,有些是代码问题。你可以使用放置在JFrame和GridLayout上的JPanel对象矩阵。例如,请签出。这看起来确实像我想要的,唯一的问题是我不知道如何更改ce我有另一个类,它决定一个细胞是否需要改变颜色,但我怎么能用这个代码实现这一点呢?