在Swing中移动背景图像

时间:2014-07-10 13:06:46

标签: java swing paint keylistener imageicon

你好我是编程方面的新手,我们有一个项目。我们创建了一个简单的赛车游戏,背景动人,但我仍然坚持我的代码,我不知道该怎么做。游戏开始时我需要一个移动的背景请有人帮助我。我乞求T_T

enter image description here

这是我的代码:

public class Game extends JFrame implements KeyListener, ActionListener {

    Random s = new Random();
    int x = 0, y = 50, a = 0, b = 250, f = 900, a1 = 0, b2 = 350, a2 = 0, b3 = 150;
    int Move;
    int value;
    JLabel spriteLabel1 = new JLabel(new ImageIcon("ss.gif"));
    JLabel spriteLabel2 = new JLabel(new ImageIcon("ss.gif"));
    JLabel spriteLabel3 = new JLabel(new ImageIcon("ss.gif"));
    JLabel spriteLabel4 = new JLabel(new ImageIcon("ss.gif"));
    JLabel background = new JLabel(new ImageIcon("geren.png"));
    Timer T1 = new Timer(5000, this);
    Timer T = new Timer(5, this);

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

    public Game() {
        Container c = getContentPane();
        c.setLayout(null);
        c.add(spriteLabel1);
        c.add(spriteLabel2);
        c.add(spriteLabel3);
        c.add(spriteLabel4);
        c.add(background);
        background.setBounds(0, 0, 1024, 768);
        addKeyListener(this);
        setSize(1000, 750);
        setVisible(true);
    }

    public void paint(Graphics g) {
        super.paint(g);
        spriteLabel1.setBounds(x, y, 60, 1000);
        spriteLabel2.setBounds(a, b, 60, 800);
        spriteLabel3.setBounds(a1, b2, 60, 500);
        spriteLabel4.setBounds(a2, b3, 60, 650);
    }

    public void keyPressed(KeyEvent e) {
        String k = e.getKeyText(e.getKeyCode());
        if (k.equals("D")) {
            if (a != f) {
                x = x + 15;
                spriteLabel1.setIcon(new ImageIcon("ss.gif"));
                repaint();
                if (x > f) {
                    x = f;
                    spriteLabel1.setIcon(new ImageIcon("ss.gif"));
                    //JOptionPane.showMessageDialog(null,"Congratulations!! 
                    //Sanji wins!","Result",JOptionPane.PLAIN_MESSAGE,
                    //new ImageIcon("evolve sanji.gif"));
                    //System.exit(0);
                }
                repaint();
            }
        }
        T.stop();
    }

    if(k.equals ( 
        "D")) 
    {
        if (x != f) {
            T.start();
            Move = 3;
        }
    }

    public void keyReleased(KeyEvent e) {
    }

    public void keyTyped(KeyEvent e) {
    }

    public void actionPerformed(ActionEvent e) {
        if (Move == 3) {
            a = a + (s.nextInt(3));
            a = a + value;
            if (a > f) {
                a = f;
            }
            spriteLabel2.setIcon(new ImageIcon("ss.gif"));
            spriteLabel2.setBounds(a, b, 100, 800);
        }
        if (a == f) {
            if (Move == 3) {
                a1 = a1 + (s.nextInt(3));
                a1 = a1 + value;
                if (a1 > f) {
                    a1 = f;
                }
                spriteLabel3.setIcon(new ImageIcon("ss.gif"));
                spriteLabel3.setBounds(a1, b2, 100, 500);
            }
            if (Move == 3) {
                a2 = a2 + (s.nextInt(5));
                a2 = a2 + value;
                if (a2 > f) {
                    a2 = f;
                }
                spriteLabel4.setIcon(new ImageIcon("ss.gif"));
                spriteLabel4.setBounds(a2, b3, 100, 650);
            }

        }
    }
}

1 个答案:

答案 0 :(得分:7)

这里是剧透

enter image description here

这里是肉

我看到你做错了一些事情:

  • 不断创建新图像。只需使用相同的图像。

  • 我不会使用标签,只是将一个Image / BufferedImage绘制到绘画表面上。

  • 忘了两个计时器。你可以用一个到期。这就是你管理国家的方式。

  • 不要在paint方法中做任何逻辑。在你目前的情况下,因为甚至没有画任何东西,你甚至不需要它。在我的例子中,虽然我画了图像。

  • 不要在JFrame上画画并覆盖paint。在paintComponent中覆盖JPanel,然后将该面板添加到框架中。

  • 最后,关键成分,使用方法(来自Graphics) -

    public abstract boolean drawImage(Image img,
            int dx1,
            int dy1,
            int dx2,
            int dy2,
            int sx1,
            int sy1,
            int sx2,
            int sy2,
            ImageObserver observer)
    

    您可以获得更好的解释in this post。基本上,s点是源图像,因此您可以在动画期间移动sx1sx2,它将移动图像的一部分以在{ {1}}绘画表面的点。

如果您不知道如何真正进行自定义绘画,建议您浏览Performing Custom Painting

以下是完整示例

(注意我用于背景的图像是2000x350)

d