0

My question is where did i go wrong. it is supposed to make a frame where i can control an oval, move it around back forth left and right, and then make it move with the arrows. but right now i cant even make the oval, or even insert a word into it.

import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;


public class JavaGame extends JFrame{
    int x, y;
    public class AL extends KeyAdapter {
        public void keyPressed(KeyEvent e){
            int keyCode = e.getKeyCode();
            if(keyCode ==e.VK_LEFT){
            x--;
        }
        if(keyCode ==e.VK_RIGHT){
            x++;
        }
        if(keyCode ==e.VK_DOWN){
            y--;
        }
        if(keyCode==e.VK_UP){
                y++;
        }
    }
    public void keyReleased(KeyEvent e){

    }
}
public JavaGame (){
    addKeyListener(new AL());
    setTitle("Game");
    setSize(250,250);
    setResizable(false);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
}
public void Paint(Graphics g){
    x = 150;
    y = 150;
    g.fillOval(x, y, 15, 15);
    repaint();
}
public static void main(String[] Args){
new JavaGame();
}
}
user1241388
  • 147
  • 1
  • 3
  • 12
  • 2
    Off the topic: You should not call `repaint()` inside Paint. It will cause endless repaints that wast CPU time, and many times cause wired flickering graphics. And as suggested, read a GUI tutorial, it requires some time to get it - but then its fun :) – Ramzi Khahil Feb 29 '12 at 23:04

2 Answers2

7

Probably because Paint isn't a standard Java paint method. I don't see anything resembling an event loop, either--have you considered checking out any Swing tutorials/etc.?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • im new so how would i do that. – user1241388 Feb 29 '12 at 22:54
  • 2
    @user1241388: You can find the tutorials here: 1) [The Really Big Index](http://docs.oracle.com/javase/tutorial/reallybigindex.html), 2) [Using Swing Components](http://docs.oracle.com/javase/tutorial/uiswing/components/index.html), 3) [2D Graphics](http://docs.oracle.com/javase/tutorial/2d/index.html), 4) [Custom Painting with Swing](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) – Hovercraft Full Of Eels Feb 29 '12 at 23:05
4

Recommendations:

  • As Dave says, you need to override the correct method
  • even so, you shouldn't be drawing directly in a top-level window but rather in a component that derives from JComponent such as JPanel or JComponent itself.
  • draw in this class's paintComponent(...) method (usually).
  • Use the @Override annotation to make sure that you are truly overriding a class's method.
  • Don't use KeyListener's with Swing but rather Key Bindings.

For example, please have a look at my sample code here: How to make an image move while listening to a keypress in Java.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    He's also setting x and y to 150 every time he "Paints" which overrides what his KeyListener is doing... –  Feb 29 '12 at 22:57