-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemplirCodeFrame.java
More file actions
258 lines (216 loc) · 8.42 KB
/
RemplirCodeFrame.java
File metadata and controls
258 lines (216 loc) · 8.42 KB
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
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.LineBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.JComponent.*;
import javax.swing.JFileChooser;
import javax.tools.*;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/**
*
* @author Youssef MINYARI et Othmane MOKRANE
*
*/
public class RemplirCodeFrame extends JPanel{
Object reponse;
JLabel exerciceLabel, codeSaisi;
JTextArea zoneCode;
JButton soumettre;
static boolean next = false;
static int score = 0;
static JLabel timer = new JLabel ("00 : 00 : 000") ;
static Compteur count = new Compteur ();
RemplirCodeFrame(RemplirCode exercice, JFrame fenetre) {
exerciceLabel = new JLabel(exercice.getQuestion());
reponse = exercice.getReponse();
soumettre = new JButton("Soumettre");
codeSaisi = new JLabel("Code:");
zoneCode = new JTextArea();
JScrollPane scrollPane = new JScrollPane(zoneCode);
fenetre.add(scrollPane);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBorder(BorderFactory.createLineBorder(Color.gray));
panel.setBackground(Color.DARK_GRAY);
fenetre.setContentPane(panel);
setLayout(null);
setBackground(Color.getHSBColor(154, 254, 25));
setBounds(100,90,600,300);
//setBorder(BorderFactory.createLineBorder(Color.black));
panel.add(this);
add(exerciceLabel); add(zoneCode); add(codeSaisi); add(soumettre);
exerciceLabel.setBounds(40,8,520,50);
exerciceLabel.setBorder(new LineBorder(Color.blue, 2, true));
exerciceLabel.setHorizontalAlignment(JLabel.CENTER);
codeSaisi.setBounds(50,80,100,30);
codeSaisi.setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));
zoneCode.setBounds(200,80,300,150);
zoneCode.setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));
soumettre.setBounds(160,240,100,40);
soumettre.setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));
timer.setBounds(250,420,300,30);
timer.setFont(new Font("Verdana", Font.BOLD, 40));
timer.setHorizontalAlignment(JLabel.CENTER);
timer.setBorder(BorderFactory.createLineBorder(Color.white));
timer.setForeground(Color.white);
panel.add(timer);
fenetre.setVisible(true);
System.out.println(zoneCode.getText());
}
void getCode (int time) throws InterruptedException {
class StringJavaFileObject extends SimpleJavaFileObject {
private final String code;
public StringJavaFileObject(String name, String code) {
super(URI.create("string:///" + name.replace('.', '/') + Kind.SOURCE.extension),
Kind.SOURCE);
this.code = code;
}
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return code;
}
}
class ClassJavaFileObject extends SimpleJavaFileObject {
private final ByteArrayOutputStream outputStream;
private final String className;
protected ClassJavaFileObject(String className, Kind kind) {
super(URI.create("mem:///" + className.replace('.', '/') + kind.extension), kind);
this.className = className;
outputStream = new ByteArrayOutputStream();
}
@Override
public OutputStream openOutputStream() throws IOException {
return outputStream;
}
public byte[] getBytes() {
return outputStream.toByteArray();
}
public String getClassName() {
return className;
}
}
@SuppressWarnings("rawtypes")
class SimpleJavaFileManager extends ForwardingJavaFileManager {
private final List<ClassJavaFileObject> outputFiles;
@SuppressWarnings("unchecked")
protected SimpleJavaFileManager(JavaFileManager fileManager) {
super(fileManager);
outputFiles = new ArrayList<ClassJavaFileObject>();
}
@Override
public JavaFileObject getJavaFileForOutput(Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
ClassJavaFileObject file = new ClassJavaFileObject(className, kind);
outputFiles.add(file);
return file;
}
public List<ClassJavaFileObject> getGeneratedOutputFiles() {
return outputFiles;
}
}
class CompiledClassLoader extends ClassLoader {
private final List<ClassJavaFileObject> files;
private CompiledClassLoader(List<ClassJavaFileObject> files) {
this.files = files;
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
Iterator<ClassJavaFileObject> itr = files.iterator();
while (itr.hasNext()) {
ClassJavaFileObject file = itr.next();
if (file.getClassName().equals(name)) {
itr.remove();
byte[] bytes = file.getBytes();
return super.defineClass(name, bytes, 0, bytes.length);
}
}
return super.findClass(name);
}
}
soumettre.addActionListener((ActionEvent e) -> {
/*File codeFile = new File("code.txt");
BufferedWriter outFile;
try {
outFile = new BufferedWriter(new FileWriter(codeFile));
outFile.write(zoneCode.getText());
outFile.close();
} catch (IOException e1) {
e1.printStackTrace();
}*/
String codeString = zoneCode.getText();
//System.out.println(codeString);
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
JavaFileObject compilationUnit =
new StringJavaFileObject("CodeGenTest", codeString);
SimpleJavaFileManager fileManager =
new SimpleJavaFileManager(compiler.getStandardFileManager(null, null, null));
JavaCompiler.CompilationTask compilationTask = compiler.getTask(
null, fileManager, null, null, null, Arrays.asList(compilationUnit));
compilationTask.call();
CompiledClassLoader classLoader =
new CompiledClassLoader(fileManager.getGeneratedOutputFiles());
Class<?> codeGenTest;
try {
codeGenTest = classLoader.loadClass("CodeGenTest");
//Method main = codeGenTest.getMethod("main", String[].class);
Method somme = codeGenTest.getMethod("somme", int.class, int.class);
Object resultat = somme.invoke(null, new Object[]{null});
if (resultat.equals(reponse)) {
System.out.println("Success");
}
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | ClassNotFoundException e1) {
e1.printStackTrace();
}
//if (resultat.equals(reponse)) score++ ;
//next = true ;
});
while (next == false ) {
timer.setText(String.format("%02d", count.M)+" : "+String.format("%02d", count.S)+" : "+String.format("%03d", count.Ms));
count.Ms++ ;
Thread.sleep(1);
if (count.Ms==999){
count.S++ ;
count.Ms=0 ;
}
if (count.S==59){
count.M++ ;
count.S=0;
}
if ((count.S + count.M*60) > time-3 ) {
timer.setForeground(Color.red);
if ((count.S + count.M*60)==time) {
return ;
}
}
}
next = false ;
}
int getScore() {return score ;}
Compteur getTime() {return count ;}
void Reset() {
count.M=0 ;
count.Ms=0 ;
count.S=0;
score = 0 ;
}
}