java - Canvas 中的网格,最后一个单元格的大小与其他单元格不同

标签 java canvas grid

如何使用大小相等的网格绘制 Canvas ? 问题:http://tinypic.com/r/idvc0j/5 该代码绘制的最后一个单元格的大小与其他单元格不平行。 我必须在 Paint() 方法中做哪些更改?

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

public class Grid extends Canvas{

    Cell[][] maze;
    int rows;
    int cols;

    Grid(int rows, int cols) {
        this.rows = rows;
        this.cols = cols;
        maze = new Cell[rows][cols];
    }

    public static void main(String[] args){
        JFrame f = new JFrame("Sample");
        Grid x = new Grid(25,25);
        f.add(x);
        f.pack();
        f.setVisible(true);
        f.setSize(600,600);
        f.setResizable(false);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void paint(Graphics g) {
    int k;
    int width = getSize().width;
    int height = getSize().height;

    int htOfRow = height / (rows);
    for (k = 0; k < rows; k++)
        g.drawLine(0, k * htOfRow , width, k * htOfRow );

    int wdOfRow = width / (cols);
    for (k = 0; k < cols; k++)
        g.drawLine(k*wdOfRow , 0, k*wdOfRow , height);
    }
}

class Cell {

//  private final static int NORTH = 0;
//  private final static int EAST = 1;
//  private final static int WEST = 2;
//  private final static int SOUTH = 3;
//  private final static int NO = 0;
//  private final static int START = 1;
//  private final static int END = 2;
//  boolean[] wall = new boolean[4];
//  boolean[] border = new boolean[4];
//  boolean[] backtrack = new boolean[4];
//  boolean[] solution = new boolean[4];
//  private boolean isVisited = false;
//  private boolean isCurrent = false;
//  private int Key = 0;
//  
//  public Cell(){
//  for(int i=0;i<4;i++){wall[i] = true;}
//  }
//  public void setStart(int i){Key = i;}   
//  public int getKey(){return Key;}
//  public boolean checkVisit(){return isVisited;}
//  public void stepIn(){
//      isCurrent = true;
//      isVisited = true;
//  }
//  public void stepOut(){isCurrent = false;}
}

最佳答案

你四舍五入太早了。 htOfRowwdOfRow 不是精确的 int 值。通过将它们转换为 int,您可以让小错误在整个网格范围内累积。

相反,将它们保留为 double,将它们乘以 k,然后才将它们转换回 int

例如:

如果width = 100cols = 8wdOfRow = 12.5

如果在 k=8 时将其转换为 int,然后再乘以 k,那么您将最后一行放在 12*8=96 处。

如果您乘以 double 然后转换为 int,您的最后一行将位于 12.5*8 = 100

这是固定代码:

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.JFrame;

public class Layout
{
    public static void main( String[] args )
    {
        JFrame f = new JFrame( "Sample" );
        Grid x = new Grid( 50, 50 );
        f.add( x );
        f.pack( );
        f.setVisible( true );
        f.setSize( 600, 600 );
        f.setResizable( false );
        f.setLocationRelativeTo( null );
        f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    }

    static class Cell
    {

    }

    public static class Grid extends Canvas
    {

        Cell[][] maze;
        int rows;
        int cols;

        Grid( int rows, int cols )
        {
            this.rows = rows;
            this.cols = cols;
            maze = new Cell[rows][cols];
        }

        public void paint( Graphics g )
        {
            int k;

            double width = getSize( ).width;
            double height = getSize( ).height;

            double htOfRow = height / ( rows );
            for ( k = 0; k < rows; k++ )
                g.drawLine( 0, ( int ) ( k * htOfRow ), ( int ) width, ( int ) ( k * htOfRow ) );

            double wdOfRow = width / ( cols );
            for ( k = 0; k < cols; k++ )
                g.drawLine( ( int ) ( k * wdOfRow ), 0, ( int ) ( k * wdOfRow ), ( int ) height );
        }
    }
}

关于java - Canvas 中的网格,最后一个单元格的大小与其他单元格不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10059435/

相关文章:

canvas - 在fabric js Canvas 上写入角度图像旋转量

java - 使用java IO将文件从本地复制到远程系统

java - 警告 : SQL Error: 1064, SQLState:42000

Java:获取通用供应商返回对象的类类型

javascript - 如何让球以正确的方向而不是直线从桨上反弹

Javascript Canvas 垂直倾斜图像

grid - 将 UI 图像放入网格布局组 Unity C#

c# - WPF - 通过调整大小在窗口中调整控件大小

javascript - 我找不到一种优雅的方法将数据库源添加到 dgrid

java - 数组操作,在末尾添加元素,将其他元素推后