-1

I am trying to make a simple game in Java. I set a size of my frame in this way:

getContentPane().setPreferredSize(new Dimension(1280, 720));

And then I added a JComponent object, which draws whole graphics in my game:

getContentPane().add(gameGraphics);

And it all seemed to work properly, till I tried to prevent game objects from going out-of-map. "Game engine" teleports every object that went below 0 or above 720 in height or 1280 in width. I am sure that algorithm works perfectly (I checked it very precisely) and just look what happens when I try to reach lower-right corner of the window (while upper-left works properly!):

enter image description here

(A player is that blue rectangle in a corner)

There is a 10-pixel line of free space on the right and down of the window. So a content of the window must be bigger than 1280x720 pixels. Do you have any ideas what is wrong here? Thanks.

There is a whole JFrame code:

MainFrame() {
    super("GameOne");
    setLayout(new BorderLayout());

    getContentPane().setPreferredSize(new Dimension(RES_WIDTH, RES_HEIGHT));

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    gameGraphics = new GameGraphics();
    getContentPane().add(gameGraphics);

    new Player();
    new TestShip(10, 10);

    pack();
    setLocationRelativeTo(null);
    setResizable(false);
    setVisible(true);
}

GameGraphics as I said is a class that extends JComponent and everything which can be seen in the game is generated in a paintComponent method of this class.

RES_WIDTH and RES_HEIGHT are constants that are equal to 1280 and 720 respectively.


PROBLEM SOLVED:

A solution is to override a JComponent class's getPreferredSize() method (in my application it will be in GameGraphics class) (many layout managers ask every component for that and pack() method also does it). You just need to make it return a size (in 'Dimension') you want it to have. Also it's probably better to make a JFrame 'setResizable(false)' before you call pack().

Android developer
  • 1,272
  • 1
  • 12
  • 17
  • 3
    can you provide an [MCVE](http://stackoverflow.com/help/mcve)? without code it's hard to tell what's wrong. – Frakcool Aug 22 '14 at 16:05
  • Thanks for editing. I couldn't paste this image because I have less than 10 reputation. – Android developer Aug 22 '14 at 16:06
  • don't worry happened to me too :) at least you posted link. Now please, can you post relevant code? (The less you can post, but still working code which has the same problem). Would be really awesome. – Frakcool Aug 22 '14 at 16:08
  • Well, everything in this application happens in a paintComponent method of the gameGraphics object (which is added, as posted above, to a ContentPane). I have no idea what else can I say about it... maybe I'll post whole JFrame code. – Android developer Aug 22 '14 at 16:41
  • 1
    `new Player();` and `new TestShip(10, 10);` will throw me an error because I don't have those classes. Please check the link I provided before in order to create a Minimal Complete and Verifiable Example which we can copy-paste and test your own code and actually see the same as you – Frakcool Aug 22 '14 at 16:46
  • 2
    1) Override the preferred size of the custom component to return the required size. 2) Call `setResizable(false)` ***before*** 3) Calling `pack()`. -- If that does not fix the problem, post an MCVE as advised/linked by @Frakcool. – Andrew Thompson Aug 22 '14 at 16:51
  • Ah, well. I would have to post a whole application here (11 classes and 2 .png files). There is no way to make it shorter if you want to test it. But look at this image I just made: https://dl.dropboxusercontent.com/u/49637860/soprob2.png you can check these values by yourself, they are correct. A Problem must be somewhere in JFrame, because I don't set anything related to a window later. – Android developer Aug 22 '14 at 16:57
  • OK Andrew, give me a second... – Android developer Aug 22 '14 at 16:57
  • It helped!! Thank you so much :) problem solved. And I would also like to thank you, Frakcool for being polite for such a noob on StackOverflow like me :) – Android developer Aug 22 '14 at 17:06
  • @user3629787 don't worry we're here to help you. Now I wanna ask you for 2 more things, well 1 is a request and another is a tip. **Tip** if you want to notify someone for example me, add an @ followed by user name (as I did with yours), you can only notify one person per comment. and now **Request** post a little piece of code (which of Andrew's suggestions worked) showing the solution, and whenever system allows you to accept an answer do it with your own answer. – Frakcool Aug 22 '14 at 17:28
  • @Frakcool wow. I was so bad at this :P Thanks again. Okay... now how to accept an answer? – Android developer Aug 22 '14 at 17:43
  • 1st you must write it and then once done that, there will be a "tick" icon on the left, clic it and it's done :) sorry for late answer I had no light – Frakcool Aug 22 '14 at 19:19
  • @Frakcool OK, it's done but I have to wait 2 days to accept it :( – Android developer Aug 22 '14 at 20:02

1 Answers1

0

PROBLEM SOLVED:

A solution is to override a JComponent class's getPreferredSize() method (in my application it will be in GameGraphics class) (many layout managers ask every component for that and pack() method also does it). You just need to make it return a size (in 'Dimension') you want it to have. Also it's probably better to make a JFrame 'setResizable(false)' before you call pack().

Android developer
  • 1,272
  • 1
  • 12
  • 17
  • post that code C: would be better – Frakcool Aug 22 '14 at 20:38
  • You're just giving the same answer that camickr gave you in your [previous question](http://stackoverflow.com/questions/23617127/creating-a-space-for-graphics2d-drawings). – Hovercraft Full Of Eels Aug 22 '14 at 21:21
  • @Frakcool come on, you want me to post that one line of code? Okay... return new Dimension(MainFrame.RES_WIDTH, MainFrame.RES_HEIGHT) – Android developer Aug 23 '14 at 11:25
  • @Hovercraft Full Of Eels (I have no idea how to give you a notification lol). Yes, an answer is same, but a problem was quite different. Anyway I will try to remember that now... it was a few months ago and I simply forgot about this damn getPreferredSize() overriding... – Android developer Aug 23 '14 at 11:29