0

hey all

I was watching a tutorial on java game development.In that video,the guy was making a window a on the screen,then making an oval,then moving it.

His code worked fine and i am 100% sure that my code is similar to his(i checked it a few times).
What my problem is that the window just display the image behind the window and not displays the blank screen.
for eg. i use netbeans IDE.When i run the program,he window appears on the top left corner of the screen but what it displays is the things behind it such as : "file","edit","view","navigate","run"(basically,al the components behind the window) etc.
THIS IS THE CODE:

package javaapplication1;

import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class JavaApplication1 extends JFrame{
int x,y;
    public class AL extends KeyAdapter{
        public void keyPressed(KeyEvent e){
            int keyCode = e.getKeyCode();
if(keyCode == e.VK_A){
        x-=5;
}
if(keyCode == e.VK_D){
    x+=5;
}
if(keyCode == e.VK_W){
    y-=5;
}
if(keyCode == e.VK_S){
    y+=5;
}

        }
        public void KeyReleased(KeyEvent e){

        }
    }

    public JavaApplication1(){
        addKeyListener(new AL());
         setTitle("Java Game");
    setSize(250,250);
    setVisible(true); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        x = 150;
        y = 150;
    }
    public void paint(Graphics g){    
        g.fillOval(x,y,15,15);
        repaint();
    }

    public static void main(String[] args) {
        new JavaApplication1();


    }
}

The ball is on the copied screen and is moving just fine.
Please i am really going insane due to this frustration.I googles it but could'nt find any solution.Now, this is the place where i rely the most.

Thanks to all.

2
Contributors
1
Reply
21
Views
3 Years
Discussion Span
Last Post by Ezzaral
1

You should not have a repaint() call in your paint() method. Also, you should really be overridding paintComponent() on a JPanel or something instead of paint() on a JFrame. If you really must override paint, make sure you call super.paint() first or other components in the frame won't render correctly.

Edited by Ezzaral

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.