1

I'm making a 2D game and drawing to a surface inside a Frame. The problem is this, I am looping through the background and drawing the background image (using a image tiled), here is the result:

Note: I set the frame to 640,480

enter image description here

As you can see the top and left sides are off by a few pixels, I'm mainly worried about the top..

I am using this code to initialize the frame:

    frame = new Frame(_title);


    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });


    frame.addMouseListener(new Mouse());
    frame.addKeyListener(new Keyboard());   
    frame.setSize(new Dimension(width, height));
    frame.setResizable(false);
    frame.setBackground(Color.lightGray);
    frame.setVisible(true);
    frame.createBufferStrategy(2);

And here is how I am drawing the background:

    for(int x = 0; x < GameClient.width; x+= 32) {
        for(int y= 0; y< GameClient.height; y+= 32) {
                g.drawImage(floor, x , y, null);

        }
    }

I would have thought that it would work fine, but obviously not..

What am I doing wrong, or how can I fix this?

Thanks.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Duncan Palmer
  • 2,865
  • 11
  • 63
  • 91

2 Answers2

3

First off, you should be using swing and not the old awt (but the principle is the same).


The frame starts at the top left corner including the border. Add a component to the frame and override the paintComponent on that component method to get the correct starting coordinates.


Here is a complete example:

enter image description here

public static void main(String[] args) {

    JFrame frame = new JFrame();
    frame.add(new JComponent() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            g.setColor(Color.GRAY);
            for (int x = 0; x < getWidth(); x += 32)
                for (int y = 0; y < getHeight(); y += 32)
                    g.fill3DRect(x+1, y+1, 30, 30, true);
        }
    });

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600, 600);
    frame.setVisible(true);
}
dacwe
  • 43,066
  • 12
  • 116
  • 140
  • I'm not using a JFrame though, I'm using AWT's Frame. Although i'm not sure if it would matter. – Duncan Palmer Feb 23 '12 at 08:15
  • 1
    Replying to your edit, I got told by users on this website that I should use AWT for 2D game development instead of Swing.. – Duncan Palmer Feb 23 '12 at 08:21
  • 2
    +1 This is the correct answer. Pity the OP chose the less suitable strategy as correct. As to 'told to use AWT'. That advice was probably that to do custom painting, you usually only use classes in the AWT. Things like `Graphics` and `Graphics2D`, `Color` and `Font` are common. As to the ***components*** to use, they should all be Swing. I doubt there is any significant 'performance increase' from using AWT components in this day and age (if there ever was one). – Andrew Thompson Feb 23 '12 at 08:42
  • One slight improvement on this code is to use the size to set the *preferred* size of the *custom component*, then pack the frame. That way you can guarantee a specific size in the drawing area, irrespective of frame decorations. – Andrew Thompson Feb 24 '12 at 04:51
1

Either way, use frame.getInsets.top(), frame.getInsets().left, and then you start drawing at these points.

isah
  • 5,221
  • 3
  • 26
  • 36