0

Currently i'm loading images in a 2D ArrayList, but now i want to update the same image when the user pressed the 'W' key. I'm using Keybindings to check what the user presses, the System.out.print does work so why won't the image update?

Player class

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

main method:-

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();
        }
    });
  }
 }
djamaile
  • 695
  • 3
  • 12
  • 30

0 Answers0