1

同じサイズのグリッドでキャンバスをペイントするにはどうすればよいですか?問題: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;}
}
4

2 に答える 2

1

丸めが早すぎます。htOfRowwdOfRowは正確なint​​値ではありません。それらをキャストすることintで、グリッド全体に小さなエラーを蓄積させます。

代わりに、それらをとして保持し、それらをdouble乗算してkから、にキャストバックしintます。

例えば:

もしwidth = 100そうcols = 8ならwdOfRow = 12.5

int乗算する前にkそれをキャストした場合k=8は、最後の行をに配置します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 );
        }
    }
}
于 2012-04-08T04:31:15.797 に答える
0

各方向にもう1本の線が必要です。k<=行とk<=colsを実行すると、必要に応じて分割されます。

于 2012-04-08T01:43:23.523 に答える