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 3679504
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:28:01+00:00 2026-05-19T03:28:01+00:00

i want to draw objects (incl. putting components) in a container drawn perfectly in

  • 0

i want to draw objects (incl. putting components) in a container drawn perfectly in the middle.

The problem is, using the middle x and y and subtracting/adding the half of the windows width and height is not enough.

When i draw a rectangle using this code:

g.drawRect( 10, 10, this.getWidth() - 20, this.getHeight() - 20);

Then this wont work. With the line above i want to draw a rectangle having a padding of 10 pixels on all sides.
So i start it at 10×10, then i take the full width and full height of the window and subtract 20 of it, because 10 on the left side and 10 on the right side.

In this case i should get a rectangle which is perfectly centered having 10 pixels distance to the windows border.

But the right and bottom side of the rectangle is out of the window, so the width and the height given to the drawRect method is too high.

I think this comes from the windows border.
The point 0x0 is not the point where you SEE the drawings. I guess 0x0 starts at the very top-left point of the window itself (no the drawing area).

So in this case i need to know the drawing area. Something like a rectangle giving me coordinates where i need to start drawing, so i can map my coordinates to the windows coordinates.

So when i want to draw a point at 0x0 (of the visible drawing area), then this point must be mapped to something like 8×30.

I did a test by adding a mouse listener printing the mouse position whenever the mouse is moved. When i move my mouse to the very top-left position of the drawing area, then my app prints the coord 8×30. this means the left border has 8px width and the top bar (showing the windows title and close/minimize buttons) has 30px height.

How can i get this working now. I would like to say something like

g.drawRect( 10, 10, this.getWidth() - 20, this.getHeight() - 20);

so i get my rectangle drawn perfectly centered even when i resize my window.

I think its easier to understand my problem when you try to paint this:

Font font = g.getFont(); 
FontRenderContext frc = g.getFontMetrics().getFontRenderContext(); 
Rectangle2D rect = font.getStringBounds( "Hello", frc); 
g.drawString( "Hello", (int) ( this.getWidth() - rect.getWidth()), (int) ( this.getHeight() - rect.getHeight())); 

This takes the size of the Text “Hello” then tries to print it at the bottom-right. The code is correct, but the Text “Hello” appears to far on the bottom-right, i need to resize my window (making it bigger) to see the text.

  • 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-19T03:28:02+00:00Added an answer on May 19, 2026 at 3:28 am

    The getInsets() method of your container should provide the coordinate offsets you need to apply when drawing.

    Here is a link to some example code.

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

Sidebar

Related Questions

I just want to draw simple 2D objects like circle, line, square etc in
I want to draw many spheres (~100k) using OpenGL. So far, I'm doing something
I know how to draw basic objects using JOGL or LWJGL to connect to
Let's say I want to draw a simple quad like this: glBegin(GL_QUADS); glVertex2f(-1.0,-1.0); glVertex2f(1.0,-1.0);
I have array with colors as its objects. I want to create an image
I'm kind of lost in between UIImage, UIImageView, CG, layers... I want to draw
I'm trying to add plugins to my game and what I'm trying to implement
I'm working on a game in XNA for Xbox 360. The game has 3D
I'm working on a 3d game just for learning. I have a 3d world
I'm quite new to drawing shapes, graphics, bitmaps etc. I googled for a few

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.