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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:39:51+00:00 2026-05-31T05:39:51+00:00

Im working on a game in Java and having an issue (i believe its

  • 0

Im working on a game in Java and having an issue (i believe its with the content pane) when rendering. I have a screen class which draws the background and all sprites to an Image. The frame then displays the image using a doubleBuffer. For some odd reason tho the image is rendering off the edge of the frame. You can see in the link below that the image is rendering 3 pixels to the left and 28 pixels above where it should be. Does anyone have any idea what could be causing this?
![enter image description here][1]

http://imageshack.us/photo/my-images/41/weirdg.png/

public class Game extends JFrame implements Runnable{
private static final long serialVersionUID = 1L;

//graphics
public BufferStrategy buffy;
BufferedImage image;
Screen screen;

public Boolean running = false;
public Boolean playerTurn = false;

public InputManager input;
public Level level;
//JButton b;

public static final int HEIGHT = 452;
public static final int WIDTH = 768;

public Game() {
    super("GridWars");
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JPanel drawPanel = new JPanel();
    drawPanel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
    drawPanel.setLayout(null);
    drawPanel.setOpaque(false);
    //drawPanel.setLocation(50,50);

    setContentPane(drawPanel);
    setResizable(false);
    pack();
    setLocationRelativeTo(null);
    setVisible(true);
    requestFocus();
    createBufferStrategy(2);

    //b = new JButton("this sucks");

    //getContentPane().add(b);
    //b.setBounds(300, 300, 100, 50);


    buffy = getBufferStrategy();
    image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    screen = new Screen(WIDTH, HEIGHT);
    input = new InputManager(this);
    level = new Level(WIDTH, HEIGHT, input, this);
}

public void start() {
    running = true;
    new Thread(this).start();
}
public void setup(){

}
public void run() {
    final double TICKS = 30.0;
    final double UPDATE_INTERVAL_NS = 1000000000 / TICKS;
    double pastUpdateNS = System.nanoTime();

    int updateCount = 0;
    int frameCount = 0;

    final double FRAPS = 60.0; 
    final double RENDER_INTERVAL_NS = 1000000000 / FRAPS;
    double pastRenderNS = System.nanoTime();

    int pastSecondNS = (int) (pastUpdateNS/1000000000);

    while(running) {
        double nowNS = System.nanoTime();

        if(nowNS - pastUpdateNS >= UPDATE_INTERVAL_NS) {
            update();
            pastUpdateNS += UPDATE_INTERVAL_NS;
            updateCount++;
        }

        float interp = Math.min(1.0f, (float) ((nowNS - pastUpdateNS) / UPDATE_INTERVAL_NS) );
        render(interp);
        pastRenderNS += RENDER_INTERVAL_NS;
        frameCount++;

        int thisSecondNS = (int) (pastUpdateNS/1000000000);
        if (thisSecondNS > pastSecondNS) {
            //System.out.println("TICKS: "+updateCount+" | FRAPS: "+frameCount);
            updateCount = 0;
            frameCount = 0;
            pastSecondNS = thisSecondNS;
        }

        while( nowNS - pastRenderNS < RENDER_INTERVAL_NS && nowNS - pastUpdateNS < UPDATE_INTERVAL_NS) {
            try { Thread.sleep(1); } catch(Exception e) {};
            nowNS = System.nanoTime();
        }
    }
}

public void update() {
    input.update();
    level.update();
}

public void render(float interp) {
    level.render(screen, interp);

    image = screen.getImage();
    Graphics g = buffy.getDrawGraphics();
    g.drawImage(image, 0, 0, null, null);
    //b.repaint();
    g.dispose();
    buffy.show();
}

public static void main(String[] args) {
    Game game = new Game();
    game.start();
}
}
  • 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-31T05:39:52+00:00Added an answer on May 31, 2026 at 5:39 am

    The 0,0 coordinate of Graphics object you obtain from buffy.getDrawGraphics(); is exactly at top left corner of JFrame and it is ignoring frame decorations.

    1. UPD I forgot one obvious option. JFrame.getInsets() provides information about decorations. You could simply shift your rendering.

    2. You would make frame undecorated (setUndecorated(true)) and render/manage window controls yourself.

    3. Or, and i think it is easier way, you would forget about direct rendering on JFrame, place Canvas on it, and use it instead. Canvas also contains createBufferStrategy method, so you need few simple changes.

      JPanel drawPanel = new JPanel();
      drawPanel.setLayout(new BorderLayout());
      Canvas canvas = new Canvas();
      canvas.setPreferredSize(new Dimension(WIDTH, HEIGHT));
      drawPanel.add(canvas, BorderLayout.CENTER);
      
      // some code skipped
      
      canvas.setIgnoreRepaint(true); //important
      canvas.createBufferStrategy(2);
      
      buffy = canvas.getBufferStrategy();
      

    I’ve created simple demo with similar render few days ago for another answer. Maybe it will helpful.

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

Sidebar

Related Questions

I'm working on a simple 2D game engine in Java, and having no trouble
I'm working on a class project to build a little Connect4 game in Java.
I have an online Java game I'm working on, how would I go about
For a class project, we have to create a hangman game in Java (we're
I'm currently working on a game engine in java, however I'm having preformance issues
So I have been working on a 2 player Tic-Tac-Toe game in java that
I am running a complicated multithread java game, which is working perfectly with the
I'm working on building a chess game in Java, and I'm currently having a
I have implemented Conway's Game of Life problem in Java swing. Everything is working
I'm working on a little Java game in which all sorts of events can

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.