Bonjours,

Je développe un petit programme pour mobile, qui consiste à convertir une valeur de résistance dans le code des couleurs. On rentre la valeur dans un textBox, puis la résistance s'affiche avec les couleurs sur un graphique Canvas. Le problème c'est que je n'arrive pas comprendre comment on utilise le keyCode. J'ai essayé pas mal de truc, mais soit ça plante ou soit sa fait rien. Voici un exemple de mon code :
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
 
 
package hello;
 
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
 
 
public class HelloMIDlet extends MIDlet implements CommandListener {
 
    private boolean midletPaused = false;
 
 
    private Command okCommand;
    private Command Valider;
    private Command Retour;
    private Command Valider1;
    private Command Retour1;
    private Command Retour2;
    private Command backCommand;
    private List list;
    private TextBox textBox;
    private Form form;
 
    public HelloMIDlet() {
    }
 
 
 
 
    private void initialize() {
        // write pre-initialize user code here
        Valider1 = new Command("Ok", Command.OK, 0);
        form = new Form("form", new Item[] { });
        form.addCommand(getBackCommand());
        form.setCommandListener(this);
 
    }  
    public void startMIDlet() {
           switchDisplayable(null, getList());
    }
 
 
    public void resumeMIDlet() {
 
    }
 
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
        // write pre-switch user code here
        Display display = getDisplay();
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }
 
    }
 
    public void commandAction(Command command, Displayable displayable) {
 
        if (displayable == form) {
            if (command == backCommand) {
 
                switchDisplayable(null, getList());
 
            }
        } else if (displayable == list) {
            if (command == List.SELECT_COMMAND) {
 
                listAction();
 
            } else if (command == Retour) {
 
                exitMIDlet();
 
            } else if (command == Valider) {
 
                switchDisplayable(null, getTextBox());
 
            }
        } else if (displayable == textBox) {
            if (command == Retour1) {
 
                switchDisplayable(null, getList());
 
            } else if (command == okCommand) {
                switchDisplayable(null, c);
 
                traitement();
            }
        }
 
    }
 
    public Command getValider() {
        if (Valider == null) {
 
            Valider = new Command("Ok", Command.OK, 0);
 
        }
        return Valider;
    }
 
    public Command getRetour() {
        if (Retour == null) {
 
            Retour = new Command("Back", Command.BACK, 0);
 
        }
        return Retour;
    }
 
    public Command getRetour1() {
        if (Retour1 == null) {
 
            Retour1 = new Command("Cancel", Command.CANCEL, 0);
 
        }
        return Retour1;
    }
 
    public List getList() {
        if (list == null) {
 
            list = new List("list", Choice.IMPLICIT);
            list.append("Conv valeur->couleur", null);
            list.addCommand(getValider());
            list.addCommand(getRetour());
            list.setCommandListener(this);
            list.setSelectedFlags(new boolean[] { false });
 
        }
        return list;
    }
 
    public void listAction() {
        // enter pre-action user code here
        String __selectedString = getList().getString(getList().getSelectedIndex());
        if (__selectedString != null) {
            if (__selectedString.equals("Conv valeur->couleur")) {
 
            }
        }
 
    }
 
    public TextBox getTextBox() {
        if (textBox == null) {
 
            textBox = new TextBox("textBox", null, 100, TextField.ANY);
            textBox.addCommand(getRetour1());
            textBox.addCommand(getOkCommand());
            textBox.setCommandListener(this);
 
        }
        return textBox;
    }
 
    public Form getForm() {
        if (form == null) {
 
            form = new Form("form");
            form.addCommand(getBackCommand());
            form.setCommandListener(this);
 
 
        }
        return form;
    }
 
 
public Command getRetour2() {
    if (Retour2 == null) {
 
        Retour2 = new Command("Cancel", Command.CANCEL, 0);
 
    }
    return Retour2;
}
 
public Command getBackCommand() {
    if (backCommand == null) {
 
        backCommand = new Command("Back", Command.BACK, 0);
 
    }
    return backCommand;
}
 
public Command getOkCommand() {
    if (okCommand == null) {
 
        okCommand = new Command("Ok", Command.OK, 0);
 
    }
    return okCommand;
}
 
    public Display getDisplay() {
        return Display.getDisplay(this);
    }
 
    public void exitMIDlet() {
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }
 
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet();
        } else {
            initialize();
            startMIDlet();
        }
        midletPaused = false;
    }
    public void pauseApp() {
        midletPaused = true;
    }
    public void destroyApp(boolean unconditional) {
    }
    int[][] couleurs = {
        {0, 0x000000},
        {1, 0x993300},
        {2, 0xFF0000},
        {3, 0xFF3300},
        {4, 0xFFFF00},
        {5, 0x00FF00},
        {6, 0x0000FF},
        {7, 0x6600FF},
        {8, 0x999999},
        {9, 0xFFFFFF}};
    int premierChiffreBis;
    int deuxiemeChiffreBis;
    int multiplicateurBis;
 
    public void traitement() {
        String str_valeur = textBox.getString();//Récupère la valeur 
        String str_valeurBis = str_valeur.substring(2);
        int multiplicateur = str_valeurBis.length();
        int premierChiffre = Integer.parseInt(str_valeur.substring(0, multiplicateur - 1));
        int deuxiemeChiffre = Integer.parseInt(str_valeur.substring(1, multiplicateur));
        System.out.println(premierChiffre);
        System.out.println(deuxiemeChiffre);
        premierChiffreBis = couleurs[premierChiffre][1];
        deuxiemeChiffreBis = couleurs[deuxiemeChiffre][1];
        multiplicateurBis = couleurs[multiplicateur][1];
    }
    Canvas c = new Canvas() {
 
        protected void paint(Graphics g) {
            g.setColor(59, 94, 97);//couleur font
            g.fillRect(0, 0, 176, 220);
            g.setColor(255, 255, 255);//couleur résistance
            g.fillRect(26, 80, 118, 60);
            g.setColor(0, 0, 0);//Couleur ligne
            g.drawLine(26, 80, 144, 80);
            g.drawLine(26, 80, 26, 140);
            g.drawLine(26, 140, 144, 140);
            g.drawLine(144, 80, 144, 140);
            //Voila pour moi on pourrait faire comme ça mais évidement ça 
            //marche pas, je pense que c'est pas la bonne méthode
	    int i = 1;
            do{
	    	if(2==Canvas.LEFT){
                	switchDisplayable(null, getList());
            	}
	    }while(i==1)
        }
        };
}
Désolé s'il y a beaucoup de code, mais j'utilise Netbeans pour mes applications, et il génère beaucoup de ligne. Pourriez vous m'expliquer comment on utilise les boutons avec la class Canvas, pour régler se problème.
Merci d'avance.

P.S. : je suis interne donc je ne pourrais peut être pas réponse tout de suite à une ou des réponses.