0

Regarding this short video of basic collision detection:

https://www.youtube.com/watch?v=ptqhnmP8FY0&list=PL6E90696571998DC2

Can someone tell me why the upper boundary needs to be the size of the ball? I thought that an oval was drawn with a bounding rectangle starting from the upper left. If that is the case then it would seem that y==0 would be the upper boundary, but that is obviously not the case.

Summary question: why is the upper boundary 20 and not 0 if the bounding triangle starts in the upper left.

Here is the code:

public class JavaGame extends JFrame {

int x,y,sizeX = 350,sizeY=350;
//boolean erase = false;

private Image dbImage;
private Graphics dbg;

public class AL extends KeyAdapter{

    public void keyPressed(KeyEvent e){
        int keyCode = e.getKeyCode(); 

        if (keyCode == e.VK_LEFT){

            if(x<= 0)
                x=0;
            else
                x-=5;
        }
        if (keyCode == e.VK_RIGHT){
            if(x==sizeX-20)
                x=sizeX-20;
            else
                x+=5;
        }if (keyCode == e.VK_UP){
            if(y==20)
                y=20;
            else
            y-=5;
        }if (keyCode == e.VK_DOWN){
            if(y==sizeY-20)
                y=sizeY-20;
            else
            y+=5;
        }

        /*if (keyCode == e.VK_S){
            erase = true;
        }*/
    }
    public void keyReleased(KeyEvent e){

    }
}

public JavaGame(){
    addKeyListener(new AL());
    x=y=150;
    setTitle("Java Game");
    setBackground(Color.WHITE);
    setSize(sizeX,sizeY);
    setResizable(false);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}


public void paint(Graphics g){

    dbImage = createImage(getWidth(), getHeight());
    dbg = dbImage.getGraphics();
    paintComponent(dbg);
    g.drawImage(dbImage, 0, 0, this);

}

public void paintComponent(Graphics g){

    g.fillOval(x, y, 20, 20);

    /*if(erase){
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, 500, 500);
        erase = false;
    }*/

    repaint();


}

public static void main(String[] args) {


    new JavaGame();

}

}
Braj
  • 46,415
  • 5
  • 60
  • 76

2 Answers2

2

The main problem is, overriding paint of JFrame is allowing you to paint under the frames decorations.

This is one of, the many, reasons why you should avoid overriding paint on a top level container.

As discussed:

In Swing it is generally recommended to perform custom painting within the paintComponent method of a component that extends from JComponent, like JPanel.

This not only allows you to decouple of output from a specific container (JFrame), it also ensures that when added to a top level container, it only occupies the viewable area of the window and does not render under the frames borders.

JComponent based components also benefit from been double buffered, meaning that you don't need to implement your own double buffering strategy, but it won't flicker between repaints

Also, you MUST call the super method before you perform any of your own custom painting, failing to do so can produce paint artifacts

Take a look at Painting in AWT and Swing and Performing Custom Painting for more details

Side Notes:

You should also avoid the user of KeyListener as they are notorious for having focus related issues. Instead you should make use of the Key Bindings API

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Its the height of title bar of JFrame that's why y starts visible after 22.

Same for x that starts visible from 2 due to JFrame outer border width.

Here is the sample code along with snapshot

    g.setColor(Color.RED);
    g.drawRect(3, 22, 200, 200);

enter image description here

--EDIT--

I never suggest you to use it. Read below comments for it's drawbacks.

Braj
  • 46,415
  • 5
  • 60
  • 76
  • 1
    Which means that the number will de different on different OS's that have different titlebar heights... so it would be best to move all of your logic inside a control *inside* the frame, instead of doing it inside the frame itself. – John Gardner Mar 28 '14 at 21:10
  • Yes you are right, We can validate it using different themes on same OS also. – Braj Mar 28 '14 at 21:11
  • 1
    Now run it under a different look and feel and/platform, this will no longer work – MadProgrammer Mar 28 '14 at 22:53
  • I don't know why its downvoted again. What OP wants to know - **Summary question: why is the upper boundary 20 and not 0 if the bounding triangle starts in the upper left.** OP is not looking for solution but I respect all your suggestions also. – Braj Mar 29 '14 at 14:12
  • The answer is been down voted primarily because it's bad and limiting advice. The real answer is simply not to override paint of top level containers. While technically it might solve this instance of the problem, it continues to promote bad use of the API, which will only lead the op into more trouble – MadProgrammer Mar 30 '14 at 06:45