0

Is there any way that if I set JPanel width and height (900x900) the image inside of JPanel will also be(900x900)

Actually I'm very new to Java Programming. I have made a game by Raw Java coding. And after finishing coding, the game works fine. But the Problem is, I have already set the Background of the Game by an Image size of (900x900) and some game components also set by an Image of Default sizes. Now the 900x900 JPanel Height and Width is perfect for me cuz my Monitor resolution is (1980x1080). So if anyone runs my game on other PC with Monitor resolution of (1366X768) my game will be out of the screen by Height. I could change my JPanel Height and Width but that doesn't change the Background Image or other Game component Image size.

So how can I solve this problem so that whenever I re-size the JFrame or set JPanel Height and Width different, my game component Images will be automatically be re-sized to appropriate size. Otherwise I have to make all new small sized game components and background Images and new Pixel movement modification.

  • 1
    It is *possible* to scale the entire drawing area to fit whatever space is given to the panel (by the layout and the size of the top level container - the frame it is in). Whether it makes sense to do so, is another matter. Note also, that this will not scale, for example, mouse events in the game area, or any components (as in, any `JComponent` or its children) that appear there. – Andrew Thompson May 20 '15 at 17:36
  • .. See [Graphics2D.scale(sx,sy)](https://docs.oracle.com/javase/8/docs/api/java/awt/Graphics2D.html#scale-double-double-) for the simplest implementation of that approach. – Andrew Thompson May 20 '15 at 17:42
  • I realized, Actually the moving components moves by Default Pixel I gave it so, if I still scale Images, the components move by same pixel,so I need to change all over again I think. But Thank you so much. – Md. Irfan Omee May 20 '15 at 17:52
  • Can you give me any suggestion so that I can avoid this type of Problem in future, so that my game component doesn't need to move by relaying on Default Pixel. – Md. Irfan Omee May 20 '15 at 17:57
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). 3) Tip: Add @Newd (or whoever, the `@` is important) to notify the person of a new comment. – Andrew Thompson May 20 '15 at 17:58

1 Answers1

0

Please give some more details. For what i understund you wish to scale painted image? For that, you could override paint(Graphics g) method in your Panel, and calculate scale factor for the new size.

then call:

Graphics2D g2d = (Graphics2D) g;
g2d.setScale(scale,scale);
super.paint(g2d)

it will draw everything, but scaled by that factor. Maybe it will help you?

Krzysztof Cichocki
  • 6,294
  • 1
  • 16
  • 32
  • *"For that, you could override paint(Graphics g) method in your Panel"* Well, if by that you meant **`paintComponent(Graphics g)`** & **`JPanel`** then yes. Note also the Swing tag, and that the AWT is typically expected to pass an actual `Graphics` object, whereas the Swing GUI can expect a `Graphics2D` object (which has the `scale(sx,sy)` method). Also, if you choose to [edit the answer](http://stackoverflow.com/posts/30356419/edit), note that 'understund' should be 'understand' (one 'u', not two). – Andrew Thompson May 20 '15 at 17:54
  • I realized, Actually the moving components moves by Default Pixel I gave it so, if I still scale Images, the components move by same pixel, so I need to change all over again, I think. But Thank you so much for your suggestion. Can you give me any suggestion so that I can avoid this type of Problem in future, so that my game component doesn't need to move by relaying on Default Pixel. – Md. Irfan Omee May 20 '15 at 17:56