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

时间:2014-10-02 14:43:41

标签: java applet awt paintcomponent

我正在按照此处的教程开发Java applet游戏:http://www.kilobolt.com/unit-2-creating-a-game-i.html 我正在使用它作为扩展我自己的应用程序的基础。但这是问题所在。

我想将JButtons添加到applet以获取某些功能(开始,选项等)。但是当我将鼠标悬停在它们上面时,我发现它们会闪烁,如果保持不变则完全隐藏起来。我读到它与双缓冲有关,我应该使用paintComponent而不是绘画例如

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

这解决了Jbuttons的闪烁问题,但是g.drawimage方法已经破裂,或者我完全不理解它,因为我试图绘制的图像没有显示。我不知道他们是隐藏还是无法加载或是什么。如果有人可以指出正确的方向,那就太好了。

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

这是尝试使用paintComponents方法。我非常感谢帮助。

编辑:我已经改为使用Japplet并在Jpanel中使用paintComponent来绘制所有内容并且它工作正常,所有这些都基于JApplet - super.paint(); causes flicker

1 个答案:

答案 0 :(得分:1)

我已经改为使用Applet并在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);


    }