Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Image is not being drawn in correct place.  (Read 1489 times)
0 Members and 1 Guest are viewing this topic.
Offline cubemaster21
« Posted 2012-08-26 01:13:38 »

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
1  
2  
3  
Graphics g = this.screen.getGraphics();
Graphics2D g2d = (Graphics2D)g;
Graphics appG = getGraphics();

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
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);

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline jonjava
« Reply #1 - Posted 2012-08-26 01:30:24 »

which image? the entities?

Offline cubemaster21
« Reply #2 - Posted 2012-08-26 01:38:50 »

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.

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jonjava
« Reply #3 - Posted 2012-08-26 01:45:11 »

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.

Offline jonjava
« Reply #4 - Posted 2012-08-26 01:51:26 »

So basically in code perspective:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
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);
   }

}

Offline cubemaster21
« Reply #5 - Posted 2012-08-26 03:52:11 »

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

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline jonjava
« Reply #6 - Posted 2012-08-26 06:46:56 »

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.

:}

Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #7 - Posted 2012-08-26 15:42:48 »

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

Offline jonjava
« Reply #8 - Posted 2012-08-26 17:38:44 »

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.

Offline cubemaster21
« Reply #9 - Posted 2012-08-26 21:00:33 »

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

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jonjava
« Reply #10 - Posted 2012-08-26 21:26:25 »

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.

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.

source: http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html

Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #11 - Posted 2012-08-26 21:58:02 »

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.

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

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (48 views)
2014-04-15 18:08:23

BurntPizza (44 views)
2014-04-15 03:46:01

UprightPath (60 views)
2014-04-14 17:39:50

UprightPath (42 views)
2014-04-14 17:35:47

Porlus (58 views)
2014-04-14 15:48:38

tom_mai78101 (82 views)
2014-04-10 04:04:31

BurntPizza (140 views)
2014-04-08 23:06:04

tom_mai78101 (240 views)
2014-04-05 13:34:39

trollwarrior1 (200 views)
2014-04-04 12:06:45

CJLetsGame (207 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!