简体   繁体   English

未使用paintComponent()绘制/显示的图像

[英]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. 我正在按照下面的教程开发Java小程序游戏: http : //www.kilobolt.com/unit-2-creating-a-game-i.html我以此为基础扩展我自己的应用。 But here is the problem. 但这是问题所在。

I want to add JButtons to the applet for certain features (start, options etc). 我想为某些功能(开始,选项等)将JButton添加到applet中。 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 我读到它与双缓冲有关,我应该使用paintComponent而不是例如paint

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. 这就解决了Jbuttons闪烁的问题,但是g.drawimage方法坏了,或者我不太了解,因为我尝试绘制的图像没有显示。 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. 这是在尝试利用paintComponents方法。 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(); 编辑:我已更改为使用Japplet并在Jpanel中使用paintComponent绘制所有内容,并且一切正常,所有基于JApplet-super.paint(); causes flicker . 导致闪烁

I have changed to using Japplet and used paintComponent in a Jpanel to draw everything and it worked fine. 我已更改为使用Japplet,并在Jpanel中使用paintComponent来绘制所有内容,并且效果很好。

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);


    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM