绘制网格问题

时间:2014-01-02 10:24:08

标签: java java-2d

我想在Java中创建一个简单的网格,作为使用tile的地图编辑器。在我的渲染方法中,我把这个

public void render(Graphics g){
    renderGrid(g,96,64);

}

private void renderGrid(Graphics g,int width, int height) {
    g.setColor(Color.white);
    for (int y = 0; y < height; y += 32){
        for (int x = 0;x<width; x+= 32){
                g.drawRect(x, y, x+32, y+32);
                System.out.println("x =" + x + " y= " + y + " width= " + (x + 32 - x) + " height=" + (y + 32 - y)+ " column No= " + x/32 + " row No= " + y / 32);
        }
    }
}

但是当我启动程序时,它给了我这个:

error

这是调试消息

x =0 y= 0 width= 32 height=32 column No= 0 row No= 0
x =32 y= 0 width= 32 height=32 column No= 1 row No= 0
x =64 y= 0 width= 32 height=32 column No= 2 row No= 0
x =0 y= 32 width= 32 height=32 column No= 0 row No= 1
x =32 y= 32 width= 32 height=32 column No= 1 row No= 1
x =64 y= 32 width= 32 height=32 column No= 2 row No= 1

有什么建议吗? 为什么细胞不是正方形?

3 个答案:

答案 0 :(得分:4)

您的问题是drawRect takes起始位置和尺寸,而不是起始位置和结束位置。所以它应该是: -

//drawRect(int x, int y, int width, int height)
g.drawRect(x, y, 32, 32);

<强>另外

(x + 32 - x)和此(y + 32 - y)(32)

相同

答案 1 :(得分:4)

问题就在这里:

g.drawRect(x, y, x+32, y+32);

应该是:

g.drawRect(x, y, 32, 32);

第3和第4个参数是宽度和高度,而不是“绘制到那个点”。可见:

enter image description here

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

class GridCells {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                JOptionPane.showMessageDialog(null, new GridCellPanel());
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}

class GridCellPanel extends JPanel {

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        setBackground(Color.BLACK);
        render(g);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 100);
    }

    public void render(Graphics g) {
        renderGrid(g, 96, 64);
    }

    private void renderGrid(Graphics g, int width, int height) {
        g.setColor(Color.white);
        for (int y = 0; y < height; y += 32) {
            for (int x = 0; x < width; x += 32) {
                g.drawRect(x, y, 32, 32);
                System.out.println("x =" + x + " y= " + y + " width= " + 
                        (x + 32 - x) + " height=" + (y + 32 - y) + 
                        " column No= " + x / 32 + " row No= " + y / 32);
            }
        }
    }
}

答案 2 :(得分:0)

接受答案后我不确定你是否想要填写整个scrren,所以在我看到接受的答案之前,我正在研究这个问题。我会保留它以防万一你正在寻找


不完全确定您的代码逻辑,但我提出了自己的逻辑,似乎更容易理解。

  1. 获取绘图表面的宽度

    private static final int SCREEN_SIZE = 300;
    
  2. 为每个sqaure设置所需的尺寸。

    private static final int G_W = 30;
    private static final int G_H = 30;
    
  3. 通过将SCREEN_SIZE除以方形宽度/高度来获取行数和列数

    int columns = SCREEN_WIDTH / G_W;
    int rows = SCREEN_WIDTH / G_H;
    
  4. 执行上述操作后,循环更容易管理

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            g.drawRect(x, y, G_W, G_H);
            x += G_W;
        }
        y += G_H;
        x = 0;
    }
    

  5. 完整代码

    import java.awt.Dimension;
    import java.awt.Graphics;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    
    public class Grid extends JPanel{
    
        private static final int SCREEN_SIZE = 300;
        private static final int G_W = 30;
        private static final int G_H = 30;
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
    
            int x = 0;
            int y = 0;
    
            int columns = SCREEN_SIZE / G_W;
            int rows = SCREEN_SIZE / G_W;
    
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < columns; j++) {
                    g.drawRect(x, y, G_W, G_H);
                    x += G_W;
                }
                y += G_H;
                x = 0;
            }
    
        }
    
        public Dimension getPreferredSize() {
            return new Dimension(SCREEN_SIZE, SCREEN_SIZE);
    
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JFrame frame = new JFrame();
                    frame.add(new Grid());
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
    
                }
            });
        }
    }
    

    结果

    enter image description here