Sign in to follow this  

[java] origin for graphics context off screen

Recommended Posts

I am writing a basic game in java. Every renderable object maintains its own coordinates and size. Each object assumes that the origin (0, 0) is where it should be, namely in the upper left corner of the screen. It seems that the origin is not in the upper left corner but rather somewhere in the negatives. Thus, if I try to draw in object at the origin, it does not appear on screen. Here is the code I use to obtain the Graphics object used for rendering.
private void renderObjects()
		Graphics g = buffer.getDrawGraphics();
		g.fillRect(0, 0, frame.getWidth(), frame.getHeight());
		g.setFont(new Font("Courier", Font.PLAIN, 28));
		g.drawString("Score: " + player.getScore() + "  Hits: " + player.getHits() + "  Misses: " + player.getMisses(), 100, 100);

		for(Creature creature: creatures)

The object "buffer" was obtained and stored in this code:
frame = new JFrame("Duck Hunt");
frame.setPreferredSize(new Dimension(800, 600));
frame.getContentPane().addMouseListener(new ClickHandler());

buffer = frame.getBufferStrategy();

Any help or suggetions is appreciated. Thanks.

Share this post

Link to post
Share on other sites
I can't tell what is wrong from the code shown, but the origin is in the upper-left corner. You will have to show more code to figure it out. Try a simple test.

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

public class Test extends JPanel {

public void paint(Graphics g) {
g.fillRect(10, 10, 100, 100);
public static void main(String args[]) {
JFrame f = new JFrame();
f.getContentPane().add(new Test());
f.setSize(300, 300);;

This will show you where the origin is.

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
I figured it out.

The graphics context of a JFrame includes the title bar. When I do frame.setUndecorated(true), the problem is fixed, however, I can't move the window (not a big issue).

Thanks for your help though.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this