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

  • Home
  • SEARCH
  • 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 6738785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:24:34+00:00 2026-05-26T11:24:34+00:00

I am having a bit of trouble making this grid be drawn 10 pixels

  • 0

I am having a bit of trouble making this grid be drawn 10 pixels from the top and 10 pixels from the left of the Frame.

I can make it do it by increasing this.getY() + 10 to a higher number, just wondering why if is remove the + 10 it getting drawn off screen.

Ignore the variable names and any formatting I just threw this together

package griddrawing;

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

public class Grid extends JFrame
{

    private int TILEWIDTH;
    private int TILEHEIGHT;
    private int COLS;
    private int ROWS;
    private int defaultX;
    private int defaultY;
    private int currentX;
    private int currentY;

    public Grid()
    {
        setSize(800,400);
        TILEWIDTH = 30;
        TILEHEIGHT = 30;
        COLS = 10;
        ROWS = 10;
        defaultX = this.getX() + 10;
        defaultY = this.getY() + 10;
        currentX = 0;
        currentY = 0;
    }

    @Override
    public void paint(Graphics g)
    {
        super.paint(g);

        currentX = defaultX;
        currentY = defaultY;

        g.setColor(Color.black);

        for(int i = 0; i < COLS; i++)
        {
            for(int k = 0 ; k < ROWS; k++)
            {
                g.drawRect(currentX - (TILEWIDTH / 2), currentY - (TILEHEIGHT / 2), TILEWIDTH, TILEHEIGHT);
                g.drawString("" + k, currentX, currentY);
                currentY += TILEWIDTH;
                System.out.println("COL: " + i + " ROW: " + k + " Current X: " + currentX + " Current Y: " + currentY);
            }
            currentY = defaultY;
            currentX += TILEHEIGHT;
        }
    }
}
  • 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-05-26T11:24:35+00:00Added an answer on May 26, 2026 at 11:24 am
    1. Don’t set the size of the frame.
    2. Don’t paint directly to the frame either.

    Instead of both:

    1. Override the paintComponent(Graphics) method of a JComponent or JPanel.
    2. Either call theComponent.setPreferredSize(Dimension) or override that same method.
    3. Add the custom component to the frame and call pack().

    That lot should mean you no longer need to account for any offset (which might change by platform or PLAF).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a little bit of trouble making OpenID work from within an iframe.
I'm coming from a Rails background, and am having a bit of trouble making
I'm having a bit of trouble making this JQuery work in Internet Explorer, it
Having a bit of trouble wrapping my head around this one. I'm sure the
I'm new to python and experimenting a bit but having trouble making a list
I've got a client-server app I'm making and I'm having a bit of trouble
Sorry if this is a basic question - I'm having some trouble making the
Having a bit of trouble wrapping my head around Qtimer this morning. Here's the
Im having a bit of trouble changing the the weather degrees from fahrenheit to
I am having a little bit of trouble making the table look like I

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.