如何使用keyStroke更新BufferedImage

时间:2017-03-31 11:31:15

标签: java arrays bufferedimage

目前我正在2D ArrayList中加载图片,但现在我想在用户按下' W'时更新相同的图像。键。我使用Keybindings检查用户按下了什么,System.out.print确实有效,为什么不能更新图像?

玩家类

public class Vak extends JFrame {

private static  int IMAGE_TYPE = BufferedImage.TYPE_INT_ARGB;
//public KeyInput input;

private BufferedImage img;
BufferedImage gras = null;
BufferedImage muur = null;
BufferedImage speler = null;
BufferedImage speler2 = null;

KeyInput inputSpeler;
private static final int IFW = JComponent.WHEN_IN_FOCUSED_WINDOW;
private static final String MOVE_UP = "move up";
static JLabel obj1 = new JLabel();
private JPanel panel;
String text;

public Vak() {
    super();

    this.add(new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
           g.drawImage(img, 0, 0, null);
        }

    });

    try{
        gras = ImageIO.read(new FileInputStream("res/grassBackground.png"));
    }catch(Exception e){
        e.printStackTrace();
    }
    try{
        muur = ImageIO.read(new FileInputStream("res/muur.png"));
    }catch(Exception e){
        e.printStackTrace();
    }
    try{
        speler = ImageIO.read(new FileInputStream("res/sara-1.png"));
    }catch(Exception e){
        e.printStackTrace();
    }
    try{
        speler2 = ImageIO.read(new FileInputStream("res/sara-down.png"));
    }catch(Exception e){
        e.printStackTrace();
    }

    img = new BufferedImage(660, 500, IMAGE_TYPE );   // here you should create a compatible BufferedImage
    this.setSize(img.getWidth(), img.getHeight());
    this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    int NB_TILES = 4;
    BufferedImage[] tiles = new BufferedImage[NB_TILES];
    tiles[0] = gras;
    tiles[1] = muur;
    tiles[2] = speler;
    tiles[3] = gras;

    int[][] map = new int[][] {
        { 1, 1, 1, 1, 1, 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1 ,1, 1, 1, 1},
        { 1, 2, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0 ,0, 0, 0, 1},
        { 1, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0 ,0, 0, 0, 1},
        { 1, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0 ,0, 0, 0, 1},
        { 1, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0 ,0, 0, 0, 1},
        { 1, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0 ,0, 0, 0, 1},
        { 1, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0 ,0, 0, 0, 1},
        { 1, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0 ,0, 0, 0, 1},
        { 1, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0 ,0, 0, 0, 1},
        { 1, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0 ,0, 0, 0, 1},
        { 1, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0 ,0, 0, 0, 1},
        { 1, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0 ,0, 0, 0, 1},
        { 1, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0 ,0, 0, 0, 1},
        { 1, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0 ,0, 0, 0, 1},
        { 1, 1, 1, 1, 1, 1, 1, 1, 1 ,1, 1, 1,1, 1, 1 ,1, 1, 1,  1},
    };

    panel = new JPanel();
    panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_W, 0), "forward");
    panel.getActionMap().put("forward", new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) {
            System.out.print("Test");
            speler = speler2;
        }
    });  

     for (int i = 0; i < map[0].length; i++) {
            for (int j = 0; j < map.length; j++) {
            BufferedImage tile = tiles[map[j][i]];
            for (int x = 0; x < tile.getWidth(); x++) {
                for (int y = 0; y < tile.getHeight(); y++) {
                    img.setRGB( x + i * 32, y + j * 32, tile.getRGB(x,y) );
                }
            }
        }
    } 


    this.setVisible( true );
    add(panel);
}

主要方法: -

public static void main(String[] args){
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            }

            new Vak();
        }
    });
  }
 }

0 个答案:

没有答案