Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7772829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:58:13+00:00 2026-06-01T16:58:13+00:00

How Do I paint the canvas with the grids having equal sizes? problem: http://tinypic.com/r/idvc0j/5

  • 0

How Do I paint the canvas with the grids having equal sizes?
problem: http://tinypic.com/r/idvc0j/5
the code draws the last cells with sizes not parallel to the other cells.
what change do i have to do in the paint() method?

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;}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:58:14+00:00Added an answer on June 1, 2026 at 4:58 pm

    You are rounding too early. htOfRow and wdOfRow are not exact int values. By casting them to int you let small errors accumulate over the span of the whole grid.

    Instead, keep them as double, multiply them by k and only then cast them back to int.

    For example:

    If width = 100 and cols = 8 then wdOfRow = 12.5.

    If you cast it to an int before multiplying by k when k=8 then you place your last line at 12*8=96.

    If you multiply as double then cast to int, your last line will be at 12.5*8 = 100

    Here’s the fixed code:

    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 );
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with the following code: protected void onDraw(Canvas canvas) { Paint
there is class, that draws on canvas some field package com.cerbertek; import android.content.Context; import
i try to paint an arrow png on canvas with a png bitmap behind
I have external html5 canvas that you can paint on some lines using your
I have some very simple code that 'correctly' draws a short vertical black line
I have implement the Canvas and paint to draw on canvas. i am able
<FrameLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=fill_parent android:layout_height=fill_parent> <test.viewz.MazeView android:id=@+id/mazeView android:layout_width=fill_parent android:layout_height=fill_parent /> </FrameLayout> So this is supposed
I'm having an issue using the canvas.drawText() method. I have a custom view, as
I have this code package com.cerbertek; import java.util.ArrayList; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory;
In my Android paint application i am going to paint on the canvas. But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.