0

My problem is that the ball turns into a line once moved.
Here is the code:

package Java;

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

import javax.swing.*;

public class javagame extends JFrame {

    private Image dbImage;
    private Graphics dbg;

    int x, y;

    public class AL extends KeyAdapter {
        public void keyPressed (KeyEvent e) {

            int keyCode = e.getKeyCode();
            if(keyCode == e.VK_LEFT) {
                x-= 5;
            }
            if(keyCode == e.VK_RIGHT) {
                x+= 5;
            }
            if(keyCode == e.VK_UP) {
                y-= 5;
            }
            if(keyCode == e.VK_DOWN) {
                y+= 5;
            }
        }
        public void keyReleased (KeyEvent e) {
        }
    }

    public javagame() {
        addKeyListener(new AL());

        setTitle("Java Game");
        setSize(750, 750);
        setResizable(false);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        x = 250;
        y = 250;
    }

    public void paint(Graphics g) {
        dbImage = createImage(getWidth(), getHeight());
        dbg = dbImage.getGraphics();
        paintCompenent(dbg);
        g.drawImage(dbImage, 0, 0, this);
    }

    public void paintComponent (Graphics g){
        g.drawString("Copy Right All rights reserved to Aaron Collins 2013-2013", 275, 100);
        g.drawLine(270, 105, 415, 105);

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

        repaint();
    }

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

When I say it turns into a line, I mean the ball moves but does not remove the previous one.
Please help me resolve the problem so I can continue with my game!

Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
  • This code does not compile. Is it the actual code you are using? – Michael Myers Jun 17 '13 at 23:32
  • yes... Im using eclipse –  Jun 17 '13 at 23:33
  • There is no `paintCompenent` method, so I question the line `paintCompenent(dbg);`. And if that line has been altered from the original, what else has been altered? – Michael Myers Jun 17 '13 at 23:34
  • I don't know if it compiles with anything else than eclipse –  Jun 17 '13 at 23:35
  • You'd probably get more mileage out of a pre-made game engine like Libgdx http://libgdx.badlogicgames.com/ – Zutty Jun 17 '13 at 23:35
  • I don't Know... I just followed the youtube video –  Jun 17 '13 at 23:36
  • are you sure that I didn't make the paintCompenent method? –  Jun 17 '13 at 23:37
  • I'm just saying that the code here is not the exact code you are running. There are no two ways about it. `paintCompEnent` (with an e) != `paintCompOnent` (with an o). Perhaps I sound too picky to you. But as I mentioned above, an error in one part calls the whole thing into question. Too often people hand-write what they *think* is the distilled version of their code, but it turns out to be different in some critical way and so we can't give them any help. – Michael Myers Jun 17 '13 at 23:38
  • oh... that explains a lot. So I just have to fix the spelling? –  Jun 17 '13 at 23:42
  • thanks for the vital information! –  Jun 17 '13 at 23:43
  • I DON'T KNOW (please pardon the caps). All I know about your code is that if it actually runs (and you said it does), then it is not what you have posted here. – Michael Myers Jun 17 '13 at 23:45
  • wait! Now the program won't launch now! –  Jun 17 '13 at 23:45
  • here is the error code: Exception in thread "main" java.lang.Error: Unresolved compilation problem: at Java.javagame.main(Java Game.java:103) –  Jun 17 '13 at 23:45
  • please help me get it lauching! –  Jun 17 '13 at 23:46
  • Look for the little red squiggly underlines in Eclipse; each of them should say what is wrong at that spot. – Michael Myers Jun 17 '13 at 23:49
  • yay! i clicked on the red squiggly line and it told me an option! When I took it, it fixed it all! –  Jun 17 '13 at 23:53
  • I'm curious who makes these tutorials? I always recognize them for two reasons: 1) Dissrespecting naming conventions; 2) That `AL` class which extends KeyAdapter. – Branislav Lazic Jun 18 '13 at 00:11
  • @brano88 The Java Hub –  Jul 22 '13 at 20:23

1 Answers1

3
  1. You override paint, but never call super.paint, meaning that component never prepares the Graphics context for a new paint cycle, meaning that what ever you painted before remains
  2. Your paintComponent method will never be called because JFrame does not support this method
  3. You should not be performing custom painting on a top level container (like JFrame), but should be using something like JPanel and override it's paintComponent method (making sure you call super.paintComponent first).
  4. You should also avoid calling any method that may trigger a repaint in any paintXxx method you're overriding...like repaint

The Graphics context is a shared resource, that means that the Graphics context you are given was used to paint all the other components in UI before you. paintComponent is responsible for preparing the Graphics context for painting by clearing the area it wants to paint in.

I'd recommend that you take a read through Custom Painting

I would also avoid KeyListener and use the Key Bindings API instead.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • yes but I already talked it out with another person and fixed it. But thanks for the info. –  Jun 17 '13 at 23:57