Image is not being drawn in correct place.

When I go to draw an image to the screen, idk why, but it places the image higher than desired. Can someone tell me what i’m doing wrong with this?
Also, screen is a BufferedImage


Graphics g = this.screen.getGraphics();
Graphics2D g2d = (Graphics2D)g;
Graphics appG = getGraphics();

g2d = (Graphics2D) screen.getGraphics();
		g2d.setColor(Color.white);
		g2d.fillRect(0, 0, screen.getWidth(), screen.getHeight());
		g2d.scale(3, 3);
		for (Entity e:this.entities){
			e.Draw(g2d);
		}
		g2d.setColor(Color.green);
		//g2d.fillRect(10, 10, 100, 50);
		appG.drawImage(screen, 0 , 0, screen.getWidth(),screen.getHeight(),null);

which image? the entities?

yes, Sorry, i forget to put that. I also forgot to put that the line “appG = getGraphics()” is getting the graphics from the JFrame that this class extends.

A shot in the dark here. The graphics you get from your JFrame starts to draw from the full JFrames top left position (i.e, including the JFrames top bar) so that your screen image is getting drawn behind the top bar.

I’d suggest you draw your stuff on a Canvas image instead of a JFrame. Then you can add that Canvas to your JFrame and JFrame will automatically place it under its top bar.

So basically in code perspective:

public class Game extends Canvas {
   public void render() {
      Graphics g = getGraphics();
      for (Entity e : entities) {
         e.Draw(g);
      }
   }

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

      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.add(game);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

}

Yep, that fixed it. I was trying to think of a way to NOT use canvas, but I guess that I have to. Thanks.

Why wouldn’t you want to use a Canvas?

I mean a JFrame is basically just an empty bucket with a window bar. You’re MEANT to put components inside it.

You could easily just draw your image at the window bars height offset in your JFrame if you don’t want to use Canvas. But, as JFrame works well with components it does this automatically for you. It places the component under its window bar automatically.

There’s really no reason not to use a Canvas.

:}

@JonJava
A bit unrelated but you want to call setLocationRelativeTo(null) after you call pack :slight_smile:

oh I just typed in all the related Frame method “chores” from memory. Preferably I’d still add the size methods and setResizable(false).

Never really thought about that though but it does make sense when you say it.

I’m just a bit curious, what exactly does Frame.pack(); do? I’ve never had to use it before.

[quote]public void pack()
Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. If the window and/or its owner are not yet displayable, both are made displayable before calculating the preferred size. The Window will be validated after the preferredSize is calculated.
[/quote]
source: http://docs.oracle.com/javase/6/docs/api/java/awt/Window.html#pack()
source: http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Window.html#pack()

[quote]The pack method sizes the frame so that all its contents are at or above their preferred sizes. An alternative to pack is to establish a frame size explicitly by calling setSize or setBounds (which also sets the frame location). In general, using pack is preferable to calling setSize, since pack leaves the frame layout manager in charge of the frame size, and layout managers are good at adjusting to platform dependencies and other factors that affect component size.
[/quote]
source: http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html

source: http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Window.html#pack()
[/quote]
AHHHH!!! Stop linking to the 1.4.2 javadocs T_____T ;D