1

I have started making slime volleyball. I have run into this problem where the image for the slime character flickers insanely. the image is disappeared just about the same amount of time that it is visible.

I tried removing the super.paint(g); in the paint method and that fixed the problem of flickering, but it created a new problem that wouldn't remove the image from previous locations.

Ball

Source Code:

package slimevolleyball.main;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class Main extends JFrame {

    private static final long serialVersionUID = 1L;
    public static int x = 275;
    public static int y = 300;
    public static boolean right = false;
    public static boolean left = false;
    public static boolean jump = false;
    public static int startjump = 0;
    public static int low = 300;
    public static double gravity = 0;
    public static double time = 0;
    public static int startVelocity = 0;
    public static double velocity = 0;

    public static BufferedImage slime1;
    public static BufferedImage slime2;
    public static BufferedImage background;

    static Rectangle FrameSize;

    static JFrame frame = new JFrame();

    public Main() {

        addKeyListener(new KeyListener() {

            public void keyTyped(KeyEvent e) {
            }

            public void keyReleased(KeyEvent e) {

                int keyCode = e.getKeyCode();

                switch (keyCode) {

                    case KeyEvent.VK_UP:

                        break;

                    case KeyEvent.VK_DOWN:

                        break;

                    case KeyEvent.VK_LEFT:

                        left = false;

                        break;

                    case KeyEvent.VK_RIGHT:

                        right = false;

                }

            }

            public void keyPressed(KeyEvent e) {

                int keyCode = e.getKeyCode();

                switch (keyCode) {

                    case KeyEvent.VK_UP:

                        if (jump == false) {

                            jump = true;

                            startjump = 1;

                        }

                        break;

                    case KeyEvent.VK_DOWN:

                        break;

                    case KeyEvent.VK_LEFT:

                        left = true;

                        right = false;

                        break;

                    case KeyEvent.VK_RIGHT:

                        right = true;

                        left = false;

                        break;

                }

            }

        });

        setFocusable(true);

        setTitle("Slime VolleyBall");

        setSize(600, 400);

        setResizable(true);

        setVisible(true);

        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

    public void paint(Graphics g) {

        super.paint(g);

        g.drawImage(slime1, x, y, null);

    }

    public static void main(String[] args) throws InterruptedException, IOException {

        Main game = new Main();

        slime1 = ImageIO.read(new File("red.png"));

        while (true) {

            MoveSlime1();

            Update();

            game.repaint();

            Thread.sleep(2);

        }

    }

    private static void Update() {

        FrameSize = frame.getBounds();

    }

    private static void MoveSlime1() {

        if (right == true && x <= FrameSize.getWidth() / 2) {

            x += 1;

        } else if (left == true && x >= 0) {

            x -= 1;

        }

        if (jump == true) {

            if (startjump == 1) {

                low = 300;

                gravity = -9.8;

                time = 0;

                startVelocity = 3;

                velocity = 0;

                startjump = 0;

            } else {

                velocity = startVelocity + (gravity * time);

                y -= velocity;

                time += 0.005;

                if (time > 0.01 && y >= low) {

                    jump = false;

                }

            }

        }

    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
UnXnown
  • 11
  • 1

1 Answers1

4

JFrame is not double buffered, hence the flicker, which is just one of a list of reasons why you should not be extending from JFrame and overriding it's paint method.

Instead, move all your logic over to a class which extends JPanel and then override it's paintComponent method, this way, you'll get double buffering for free.

Also, you should avoid using KeyListeners and favor the Key Bindings API which solves the focus related issues of KeyListener

Also, static is not your friend, you should learn to live without it

user1803551
  • 12,965
  • 5
  • 47
  • 74
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366