运动背景图片

时间:2013-10-10 19:19:06

标签: java swing

有点困难。我正在写一个小游戏(试图写))任何困难。那是游戏代码:

package Game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Board extends JPanel {

    private final String IMAGE_PATH_DOWN1 = "C:\\Users\\дНМ\\workspace\\Game\\image\\Down_Pic1.png";
    private final String IMAGE_PATH_DOWN2 = "C:\\Users\\дНМ\\workspace\\Game\\image\\Down_Pic2.png";

    private final String IMAGE_PATH_RIGHT1 = "C:\\Users\\дНМ\\workspace\\Game\\image\\Right_Pic1.png";
    private final String IMAGE_PATH_RIGHT2 = "C:\\Users\\дНМ\\workspace\\Game\\image\\Right_Pic2.png";

    private final String IMAGE_PATH_TOP1 = "C:\\Users\\дНМ\\workspace\\Game\\image\\Top_Pic1.png";
    private final String IMAGE_PATH_TOP2 = "C:\\Users\\дНМ\\workspace\\Game\\image\\Top_Pic2.png";

    private final String IMAGE_PATH_LEFT = "C:\\Users\\дНМ\\workspace\\Game\\image\\Left_Pic1.png";
    private final String IMAGE_PATH_LEFT2 = "C:\\Users\\дНМ\\workspace\\Game\\image\\Left_Pic2.png";

    private final String IMAGE_PATH_MAP_Z = "C:\\Users\\дНМ\\workspace\\Game\\image\\Map_Z.png";

    private static final long serialVersionUID = 1L;
    private Thing thing;
    private Timer repaintTimer;

    private Image imgCs;
    private Image imgMap;
    private static int speed = 3;

    public Board() {
        init();

    }

    private void init() {

        setFocusable(true);
        setDoubleBuffered(true);
        addKeyListener(new KeyHandler());
        setBackground(Color.BLACK);
        thing = new Thing();

        imgCs = Toolkit.getDefaultToolkit().getImage(IMAGE_PATH_DOWN1);
        imgMap = Toolkit.getDefaultToolkit().getImage(IMAGE_PATH_MAP_Z);

        repaintTimer = new Timer(50, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                repaint();
            }
        });

        repaintTimer.start();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        Graphics2D g2d = (Graphics2D) g;

        int width = imgCs.getWidth(this);
        int height = imgCs.getHeight(this);

        int scale = 4;
        speed = 3;

        int w = scale * width;
        int h = scale * height;

        //MAP START!
        int widthMap = imgMap.getWidth(this);
        int heightMap = imgMap.getHeight(this);
        int scalem = 4;
        int wm = scalem * widthMap;
        int hm = scalem * heightMap;

           for (int s = 0; s < wm * 10; s += wm) {
                for (int i = 0; i < hm * 10; i += hm) {
                    g2d.drawImage(imgMap, thing.getX()+s, thing.getY()+i,(int) wm, (int) hm, this);

                }
            }
        //MAP END

        g2d.drawImage(imgCs, thing.getX() * speed, thing.getY() * speed,
                (int) w, (int) h, this);

        Toolkit.getDefaultToolkit().sync();

    }

    private class KeyHandler extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {
            thing.pressed(e.getKeyCode());
            // LEFT!!!
            if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                if ((thing.getX() * speed % 2) == 0) {
                    imgCs = Toolkit.getDefaultToolkit().getImage(
                            IMAGE_PATH_LEFT);

                } else {
                    imgCs = Toolkit.getDefaultToolkit().getImage(
                            IMAGE_PATH_LEFT2);
                }
            }
            // RIGHT!
            if (e.getKeyCode() == KeyEvent.VK_RIGHT) {

                imgCs = Toolkit.getDefaultToolkit().getImage(IMAGE_PATH_RIGHT1);
                if ((thing.getX() * speed % 2) == 0) {
                    imgCs = Toolkit.getDefaultToolkit().getImage(
                            IMAGE_PATH_RIGHT1);
                } else {

                    imgCs = Toolkit.getDefaultToolkit().getImage(
                            IMAGE_PATH_RIGHT2);
                }
            }
            // DOWN!
            if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                if ((thing.getY() * speed % 2) == 0) {
                    imgCs = Toolkit.getDefaultToolkit().getImage(
                            IMAGE_PATH_DOWN1);

                } else {
                    imgCs = Toolkit.getDefaultToolkit().getImage(
                            IMAGE_PATH_DOWN2);

                }

            }
            // UP!
            if (e.getKeyCode() == KeyEvent.VK_UP) {
                if ((thing.getY() * speed % 2) == 0) {

                    imgCs = Toolkit.getDefaultToolkit().getImage(
                            IMAGE_PATH_TOP1);

                } else {
                    imgCs = Toolkit.getDefaultToolkit().getImage(
                            IMAGE_PATH_TOP2);

                }

            }

        }

        @Override
        public void keyReleased(KeyEvent e) {
            //
        }

    }
}

另一个班级

package Game;

import java.awt.event.KeyEvent;

class Thing {

    private final int STEP = 3;
    private int x_pos;
    private int y_pos;

    public void pressed(int keyCode) {

        switch (keyCode) {
        case KeyEvent.VK_LEFT: {
            x_pos -= STEP;
            break;
        }
        case KeyEvent.VK_UP: {
            y_pos -= STEP;
            break;
        }
        case KeyEvent.VK_RIGHT: {
            x_pos += STEP;
            break;
        }
        case KeyEvent.VK_DOWN: {
            y_pos += STEP;
            break;
        }
        }
    }

    public int getX() {
        return x_pos;
    }

    public int getY() {
        return y_pos;
    }

}

另一堂课)

package Game;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class ThingMove {

    private String NAME = "Title";
    private int WIDTH = 500;
    private int HEIGHT = 470;

    public ThingMove() {

        JFrame frame = new JFrame(NAME);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(WIDTH, HEIGHT);
        frame.setLocationRelativeTo(null);

        frame.add(new Board());

        frame.setVisible(true);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ThingMove();
            }
        });
    }
}

放回去,以免代码发生。

代码有效,图片显示了英雄,但是当他移动到盒子的末端时,它会在它下面消失,我希望当你移动图像和背景移动时,她就是这样。背景是不要静止不动,并与hero.help的图片一起移动以使其成为

http://pastebin.com/w4kR7FuQ

http://pastebin.com/jKTCbUpY

http://pastebin.com/bPxSrrrR

1 个答案:

答案 0 :(得分:2)

不会回答你的问题,但有些想法:

  1. 不要覆盖paint()。通过重写paintComponent()来完成自定义绘制。有关详细信息,请阅读Custom Painting上的Swing教程中的部分。

  2. 不要使用KeyListener。 Swing旨在与Key Bindings一起使用。有关详细信息,请参阅Motion Using the Keyboard

  3. 请勿在图像中读取每个事件。应该在程序开始时读入图像以获得更好的性能。