2

I have a 500 by 500-pixel image that I'm trying to draw onto a JPanel which lives inside a JFrame. Here's the code I have so far:

public class Game extends JFrame {
    // Other code...

   setTitle("Game");

   JPanel panel = new JPanel();
   panel.setPreferredSize(new Dimension(500, 500));

   add(panel);
   setSize(500, 500);

   setIgnoreRepaint(true);

   // Handle a close event gracefully.
   addWindowListener(new WindowAdapter() {
       public void windowClosing(WindowEvent e) {
           System.exit(0);
       }
   });

   addKeyListener(new MainMenuState(this));

   setResizable(false);
   pack();
   setVisible(true);

I can draw to the JPanel okay but it appears that the JPanel's top left corner is being drawn at 0,0 on the JFrame, which is underneath the title bar. I've tried setting layout managers on the JFrame but that didn't seem to work. I could manually offset the JPanel but I don't think I have to do that (I thought pack() was supposed to take car of that).

  • Could you be more explanatory on what is your question? – Juvanis Feb 18 '12 at 22:41
  • 1
    I fear that your problem may possibly be that you're either adding a MouseListener/MouseMotionListener to the JFrame and not to the drawing component or you're drawing directly in the JFrame. If so then points will be drawn relative to the JFrame. Please show more code so this can be clarified. – Hovercraft Full Of Eels Feb 18 '12 at 22:54
  • Other possibilities are if you give your JPanel `getX()` and `getY()` methods that default to 0 as these will prevent layout managers from placing the component correctly. Or you may be trying to set the components position in other ways -- hard to say at this point. The bottom line is this: *to properly answer your question we're going to need to know a lot more information.* – Hovercraft Full Of Eels Feb 18 '12 at 23:01
  • I guess my comments must be written in invisible ink again... – Hovercraft Full Of Eels Feb 18 '12 at 23:49
  • I added a line that I'd taken out before: `addKeyListener(new MainMenuState(this));` I asked a question here a while ago about adding context to the game so it would behave differently depending on its current state. Someone suggested the state design pattern so I tried to implement that. MainMenuState() has all of the appropriate drawing and input-handling code for the main menu. –  Feb 19 '12 at 00:29
  • For an example of drawing in a JPanel that's held by a JFrame, please see my second example class called "MouseTestHovercraft" in this answer [here](http://stackoverflow.com/a/9345440/522444). – Hovercraft Full Of Eels Feb 19 '12 at 13:52

3 Answers3

4

1) Put this Image as Icon to the JLabel

2) use Image#getScaledInstance(int width, int height, int hints) for scaling

mKorbel
  • 109,525
  • 20
  • 134
  • 319
4

Why do you create JFrame inside a JFrame? If you extends JFrame just do this:

public class Game extends JFrame {

    public Game() {
        this.setTitle("Application");

        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(500, 500));

        this.add(panel);
        this.setIgnoreRepaint(true);

        // Handle a close event gracefully.
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        // example, use your own layout manager - see text below
        this.setLayout(new FlowLayout());

        this.pack();
        this.setResizable(false);
        this.setVisible(true);
    }
}

And now to your question. If you want to have some margin, you will have to use layout manager.

user219882
  • 15,274
  • 23
  • 93
  • 138
  • Why did I create a JFrame inside of my JFrame? Because I made a dumb mistake heh; thanks for pointing it out. I changed my code to read what you had but it still seems to draw the JPanel partially under the title bar. Do I need to write a LayoutManager? I've looked through the ones in the Java API and I'm not sure I see any that would apply. –  Feb 18 '12 at 23:11
  • @offbyone I may have misled you a little. You don't have to create your own - just use one of the Java's. – user219882 Feb 18 '12 at 23:15
  • +1 for answer, but `JFrame` has by default `BorderLayout`, and `add(panel);` is about put `JComponent` to `BorderLayout.CENTER` area, – mKorbel Feb 18 '12 at 23:16
  • @mKorbel I know. I wanted to show him how he can set a layout and point him the right direction – user219882 Feb 18 '12 at 23:18
  • Even with a layout the JFrame still draws over the JPanel (if the JPanel has a layout, that is). If I try to add a layout to the JFrame I get an error. I just want the JPanel's top left corner to be at the bottom left corner of the title bar instead of the top left corner of the window (underneath the title bar). –  Feb 19 '12 at 01:04
  • @offbyone I tried the code by myself and it worked correctly. Maybe there is another problem with a code you didn't show us. – user219882 Feb 19 '12 at 10:12
  • I thought of that and added some simple drawing code right below `this.setVisible(true);`. All the code does is load an image and try to draw it at 0, 0. The result is an image that is still partially drawn underneath the title bar. –  Feb 19 '12 at 17:15
0

I just had and solved the exact same problem.

My main reads:

public static void main(String[] args) {
    JFrame main = new JFrame("Game");
    main.setLayout(new BorderLayout);
    JPanel game = new Game();
    game.setPreferredSize(new Dimension(500, 500));
    main.add(game, BorderLayout.CENTER);
    main.pack();
    main.setVisible(true);
}
justkris
  • 800
  • 4
  • 15