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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:05:44+00:00 2026-06-06T23:05:44+00:00

I am trying to write a game using JPanle and JFrame. I set up

  • 0

I am trying to write a game using JPanle and JFrame. I set up a panel inside a frame. When I paint on the panel with a rectangle size of the panel at 0,0. The rectangle shifts up and left for some pixels. How do I fix this problem? I googled and I saw the insets method, but I don’t want to use that calculate my coordinate everytime I draw.

Here are the codes

public class Game extends JFrame{

public Game(){
    this.getContentPane().setPreferredSize(new Dimension(800,600));  
    pane p = new pane();
    this.getContentPane().add(p,BorderLayout.CENTER);
    p.setPreferredSize(new Dimension(800,600));
    pack();
    setResizable(false);
    setVisible(true);


}


public static void main(String[] args){
    new Game();
}

}

public class pane extends JPanel{

public pane(){

setDoubleBuffered(false);
setBackground(Color.black);
setPreferredSize( new Dimension(800, 600));

setFocusable(true);
requestFocus(); 
}

@Override
public void paint(Graphics g) {
    g.setColor(Color.white);
    g.fillRect(0, 0, 800, 600);
    g.setColor(Color.black);
    g.fillRect(0, 0, 800, 600);

}
}

screen shot link

  • 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-06T23:05:45+00:00Added an answer on June 6, 2026 at 11:05 pm

    Actually there is no shift of your panel, it is located in 0,0. The problem is that the panel actually gets a size of 810,610 instead of 800,600. For some reason (which so far I was unable to find and if somebody has an idea I would love to learn), when you call setResizable(false) on a JFrame, its insets are modified and eventually this leads to your content pane to be bigger than expected (at least on JDK6/Win7). Call setResizable(false) before adding the components and packing the frame, and it works.

    Also consider painting a rectangle of the size of your panel (g.fillRect(0, 0, getWidth(), getHeight()); so that you are sure to fill the entire area, no matter what happens.

    Any reason to fill the background with white color and then replace it with black?

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

Sidebar

Related Questions

I'm currently trying to write a multiple-file Python (2.6.5) game using PyGame. The problem
I'm trying to write a simple maze game, without using any deprecated OpenGL API
I'm trying to write a game, so every frame I call the doDraw() method
I am trying to write an iPhone game using cocos2d and Box2D, which means
I'm trying to write a game using F# and Silverlight and am struggling a
I'm trying to write a 3D game in ActionScript 3.0 using the Stage3D API.
I'm trying to write a complex tic tac toe game by using a multidimensional
I'm trying to write a simple game to teach some basic Welsh using the
I am trying to write a xml parser for my game, since the size
I'm trying to write a simple game based on selecting the correct answer (A

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.