Bonjour,

Je suis débutant en java et je suis en train de créer un labyrinthe.
Mon programme ouvre une image(150x150 px) et le joueur est symbolisé par un pixel jaune et se déplace sur les zones blanches.
Il me faut donc empêcher le pixel jaune d'aller sur le noir.
Le joueur se déplace au moyen des flèches du clavier.
J'avais pensé scanner mon image pour remplir un tableau booléen à 2 dimensions avec pixel noir mouvement = false. Mais je n'arrive pas à le faire.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package test;
 
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import javax.swing.ImageIcon;
 
public class Test extends javax.swing.JFrame {
 
    public Test() {
        initComponents();
    }
 
    public BufferedImage scale(BufferedImage bImage, double factor)
    {
        int destWidth=(int) (bImage.getWidth() * factor);
        int destHeight=(int) (bImage.getHeight() * factor);
        GraphicsConfiguration configuration = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
        BufferedImage bImageNew = configuration.createCompatibleImage(destWidth, destHeight);
        Graphics2D graphics = bImageNew.createGraphics();
        graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
        graphics.drawImage(bImage, 0, 0, destWidth, destHeight, 0, 0, bImage.getWidth(), bImage.getHeight(), null);
        graphics.dispose();
        return bImageNew;
    }
 
    int x;
    int y;
    int i;
    int j;
    BufferedImage img = null;
    Color blanc = new Color(255, 255, 255);
    boolean[][] mvt = new boolean [150][150];
 
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
 
        image = new javax.swing.JButton();
 
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 
        image.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                imageActionPerformed(evt);
            }
        });
        image.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                imageKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                imageKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                imageKeyTyped(evt);
            }
        });
 
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(image, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(image, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
 
        pack();
    }// </editor-fold>                        
 
    private void imageActionPerformed(java.awt.event.ActionEvent evt) {                                      
 
        try 
        {
            img = ImageIO.read(new File("C:\\Users\\Alexis\\Desktop\\f1.png"));
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
        img.setRGB(x = 73, y = 1, -256);
        image.setIcon(new ImageIcon(img));
        for(y = 0; y < img.getHeight(); y++)
        {
            for(x = 0; x < img.getWidth(); x++)
            {
                for (i = 0; i < mvt.length; i++)
                {
                    for (j = 0; j < mvt[i].length; j++)
                    {
                        mvt[i][j]=true;
                        if(img.getRGB(x, y) == 0)
                        {
                            mvt[i][j]=false;
                            System.out.println("Noir");
                        }
                    }
                }     
            }
        }                 
    }                                     
 
    private void imageKeyPressed(java.awt.event.KeyEvent evt) {                                 
        int up = KeyEvent.VK_UP;
        if (evt.getKeyCode() == up && mvt[i][j] == true)
        {
            image.setIcon(null);
            img.setRGB(x, y-1, -256);
            img.setRGB(x, y, blanc.getRGB());
            image.setIcon(new ImageIcon(scale(img,5)));
        }
    }                                
 
    private void imageKeyReleased(java.awt.event.KeyEvent evt) {                                  
    }                                 
 
    private void imageKeyTyped(java.awt.event.KeyEvent evt) {                               
 
    }                              
 
    public static void main(String args[]) {
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see <a href="http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html" target="_blank">http://download.oracle.com/javase/tu...feel/plaf.html</a> 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
 
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new Test().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton image;
    // End of variables declaration                   
}
PS : Ceci est ma classe de test mais elle reprend le code de mon labyrinthe. C'est pour ça qu'il n'y a que la touche "Haut" de codée.

Merci d'avance