1

hey im trying to make a simple ascii game in java but i cant get the graphics to update on key presses

im trying to make it so the guy is controllable with the arrow keys

heres the code

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.Timer;


public class shooter extends JFrame{
    int x = 100, y = 100, dx, dy;
    Timer time;
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public void move(){
        x = x + dx;
        y = y + dx;
    }
    public int getX(){
        return x;
    }
    public int getY(){
        return y;
    }

    public shooter() {
        setTitle("title of gip");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);

        //keep at bottom
        setVisible(true);
        //dont code here >:(
    }

    ///////////GRAPHICS ////////////////////
    public void paint(Graphics g){
        g.clearRect(0, 0, getWidth(), getHeight());
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.white);
        g.drawString("☺", getX(), getY());
    }
    public void paintComponent(Graphics g){

    }
    //////////////end of graphics//////////////

    public void keyPressed(KeyEvent e){
        int pressed = e.getKeyCode();
        if (pressed == KeyEvent.VK_LEFT){
            dx = -1;
        }
        if (pressed == KeyEvent.VK_RIGHT){
            dx = 1;
        }
    }
    public void keyReleased(KeyEvent e){
        int rel = e.getKeyCode();
        if (rel == KeyEvent.VK_LEFT){
            dx = 0;
        }
        if (rel == KeyEvent.VK_RIGHT){
            dx = 0;
        }
    }
    public void actionPerformed(ActionEvent e) {
        move();
        repaint();
    }
    public static void main(String[] args){
        new shooter();


    }

} 

please help thanks

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user1068310
  • 31
  • 3
  • 6

3 Answers3

3

@dann.dev has well found the issue. You are not hooking up your key strokes and events to the main frame. Also, I just want to comment on the way you are drawing your scene. You should draw in a JPanel instead of a JFrame, this will make your life easier if you are willing to add buttons and menus to the main frame.

Override and draw in paintComponent not paint.

class DrawingPanel{
    // ....
    @Override
    public void paintComponent(Graphics g){
        g.clearRect(0, 0, getWidth(), getHeight());
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.white);
        g.drawString("☺", getX(), getY());
    }
    // ....
}

Once done, set this panel as your frame's content pane.

frame.setContentPane(new DrawingPanel());
GETah
  • 20,922
  • 7
  • 61
  • 103
  • GETah Agreed. User, the way you have set up your program looks nice but I think you may have trouble with it in a bit, sorry I don't have time to go over it right now, try reading 'Killer Java game Programming' it has some great info and a good framework for drawing to the screen at a constant fps – dann.dev Nov 27 '11 at 21:04
  • +!, for drawing on JPanel and overriding paintComponent(). – camickr Nov 27 '11 at 21:11
3

You haven't added any action listener to your frame. The key pressed and action performed methods are called when these events happen, but only from objects with action listeners or Key listeners attached.

Check out http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html.

In short though you need the following, public class TryItOut extends JFrame implements ActionListener,KeyListener{ and the in your constructor addActionListener(this); and addKeyListener(this);

Do that and then try from there.

dann.dev
  • 2,406
  • 3
  • 20
  • 32
3

1) for listening key events from keyboard would be better to implement KeyBindings, example here

2) ClassName would be public class Shooter extends JFrame {

3) paint method for Swing would be public void paintComponent(Graphics g){

4) main method would be

   public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Shooter();
            }
        });
    }

EDIT:

5) by @camickr correct noticed me -> CustomPainting should be done on a JComponent or JPanel, or you can use method paint to the GlassPane or RootPane

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • +1, for suggestion using key bindings, However overriding paintComponent() on a Jframe won't work since that method is not implemented on a JFrame. The custom painting should be done on a JComponent or JPanel. – camickr Nov 27 '11 at 21:14