Jump to content
  • Advertisement
Sign in to follow this  

[java] origin for graphics context off screen

This topic is 4470 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Don't draw directly to the JFrame. Add a JPanel, or an AWT Canvas, to the frame and draw to that instead. Then you can keep the title bar on the frame and still use (0,0) as the origin.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!