Search code examples
javaappletawtpaintcomponent

Images not drawn/showing using paintComponent()


I'm in the process of developing a java applet game following a tutorial found here: http://www.kilobolt.com/unit-2-creating-a-game-i.html I'm using this as a foundation to extend my own app. But here is the problem.

I want to add JButtons to the applet for certain features (start, options etc). But I found them to be flickering when i hover over them and completely hidden if left untouched. I read that it has to do with double buffering and that I should use the paintComponent rather than paint eg

public void paintComponents(Graphics g) {
    super.paintComponents(g);
}

That solves the Jbuttons from flickering but then the g.drawimage methods are broke or I don't understand it fully, as the images I'm trying to draw are not showing. I don't know if they are hidden or can't be load or what. If someone could please point to the right direction that would be great.

public class StartingClass extends Applet implements Runnable, KeyListener, ActionListener {

    private PlayerChar playerChar;
    private Image image, currentSprite, character, characterDown, characterJumped, background;
    private Graphics second;
    private URL base;
    private static Background bg1, bg2;
    private JButton start, options;
    private boolean running = false;
    private int score;

    @Override
    public void init() {

        setSize(800, 480);
        setFocusable(true);
        addKeyListener(this);
        Frame frame = (Frame) this.getParent().getParent();
        frame.setTitle("GraviOn-Alpha");
        try {
            base = getDocumentBase();

        }
        catch (Exception e) {
            // TODO: handle exception
        }

        // Image Setups
        character = getImage(base, "data/character.png");
        characterDown = getImage(base, "data/down.png");
        characterJumped = getImage(base, "data/jumped.png");
        currentSprite = character;
        background = getImage(base, "data/background.png");

        score = 0;
    }

    @Override
    public void start() {

        bg1 = new Background(0, 0);
        bg2 = new Background(2160, 0);
        playerChar = new PlayerChar();

        JPanel myPanel = new JPanel();
        start = new JButton("Start");
        start.addActionListener(this);
        options = new JButton("Change Colour");
        options.addActionListener(this);
        myPanel.add(start);
        myPanel.add(options);
        myPanel.setBackground(Color.BLACK);
        myPanel.setPreferredSize(new Dimension(100, 75));
        this.add(myPanel);

        Thread thread = new Thread(this);
        thread.start();

    }

    @Override
    public void stop() {
        // TODO Auto-generated method stub
    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
    }

    @Override
    public void run() {
        while (true) {
            if (running == true) {
                playerChar.moveRight();
                score += 3;
                start.setVisible(false);
                options.setVisible(false);
            }
            else {

            }
            playerChar.update();
            if (playerChar.isJumped()) {
                currentSprite = characterJumped;
            }
            else if (playerChar.isJumped() == false && playerChar.isDucked() == false) {
                currentSprite = character;
            }

            bg1.update();
            bg2.update();
            repaint();

            try {
                Thread.sleep(17);
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void update(Graphics g) {
        if (image == null) {
            image = createImage(this.getWidth(), this.getHeight());
            second = image.getGraphics();
        }

        second.setColor(getBackground());
        second.fillRect(0, 0, getWidth(), getHeight());
        second.setColor(getForeground());
        paint(second);

        g.drawImage(image, 0, 0, this);

    }

    @Override
    public void paintComponents(Graphics g) {
        super.paintComponents(g);
        g.drawImage(background, bg1.getBgX(), bg1.getBgY(), this);
        g.drawImage(background, bg2.getBgX(), bg2.getBgY(), this);
        g.drawImage(currentSprite, playerChar.getCenterX() - 61, playerChar.getCenterY() - 63, this);
        g.drawString("Score: " + score, 50, 50);

    }

    @Override
    public void keyPressed(KeyEvent e) {

        switch (e.getKeyCode()) {
            case KeyEvent.VK_UP:
                System.out.println("Move up");
                break;

            case KeyEvent.VK_DOWN:
                currentSprite = characterDown;
                if (playerChar.isJumped() == false) {
                    playerChar.setDucked(true);
                    playerChar.setSpeedX(0);
                }
                break;

            case KeyEvent.VK_LEFT:
                // playerChar.moveLeft();
                playerChar.setMovingLeft(true);
                break;

            case KeyEvent.VK_RIGHT:
                // playerChar.moveRight();
                playerChar.setMovingRight(true);
                break;

            case KeyEvent.VK_SPACE:
                playerChar.jump();
                break;

        }

    }

    @Override
    public void keyReleased(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_UP:
                System.out.println("Stop moving up");
                break;

            case KeyEvent.VK_DOWN:
                currentSprite = character;
                playerChar.setDucked(false);
                break;

            case KeyEvent.VK_LEFT:
                playerChar.stopLeft();
                break;

            case KeyEvent.VK_RIGHT:
                playerChar.stopRight();
                break;

            case KeyEvent.VK_SPACE:
                break;

        }

    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    public static Background getBg1() {
        return bg1;
    }

    public static Background getBg2() {
        return bg2;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Start")) {
            running = true;

        }
        if (e.getActionCommand().equals("Change Colour") && character == getImage(base, "data/character.png")) {

            character = getImage(base, "data/character2.png");
        }
        else if (e.getActionCommand().equals("Change Colour") && character == getImage(base, "data/character2.png")) {

            character = getImage(base, "data/character.png");
        }
        repaint();
    }
}

This is with trying to utilize the paintComponents method. I would greatly appreciate help.

EDIT: I have changed to using Japplet and used paintComponent in a Jpanel to draw everything and it worked fine, all based on JApplet - super.paint(); causes flicker.


Solution

  • I have changed to using Japplet and used paintComponent in a Jpanel to draw everything and it worked fine.

    JPanel myPanel = new JPanel(){
    
            @Override    
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(background, bg1.getBgX(), bg1.getBgY(), this);
                g.drawImage(background, bg2.getBgX(), bg2.getBgY(), this);
                g.drawImage(currentSprite, playerChar.getCenterX() - 61, playerChar.getCenterY() - 63, this);
                g.drawString("Score: " + score, 50, 50);
    
    
        }