Problemas con MouseListener y KeyListener
Publicado por Jesús (5 intervenciones) el 25/04/2019 14:44:00
Buenas tardes, estoy realizando un trabajo en java con opengl que consiste en la representación de primitvas 2D.
La parte de opengl funciona perfectamente pero ahora quiero añadir entradas de ratón para mayor funcionalidad.
He implementado KeyListener y funciona bien pero MouseListener no funciona. Y, además, cuando hago un click en el JFrame, no solo no muestra nada sino que además me dejan de funcionar las teclas.
Como veréis tengo puesto print para que me diga que he pulsado pero no salen,
La parte de opengl funciona perfectamente pero ahora quiero añadir entradas de ratón para mayor funcionalidad.
He implementado KeyListener y funciona bien pero MouseListener no funciona. Y, además, cuando hago un click en el JFrame, no solo no muestra nada sino que además me dejan de funcionar las teclas.
Como veréis tengo puesto print para que me diga que he pulsado pero no salen,
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
public class Test extends Frame implements GLEventListener, MouseListener,KeyListener{
static int A = 0;
static int B = 1;
static int C = 2;
static int D = 3;
static int HEIGHT = 800;
static int WIDTH = 800;
static int POINT_CLOUD_VERT = 30;
static GL gl; // interface to OpenGL
static GLCanvas canvas; // drawable in a frame
static GLCapabilities capabilities;
static boolean visualizeAxis = true;
static int exercise = A;
static Animator animator;
// Geometric data in memory
// Exercise A
Point p1;
DrawPoint dp1;
PointCloud pc;
DrawPointCloud dc;
SegmentLine s1;
DrawSegment ds1;
RayLine r1;
DrawRay dr1;
Line l1;
DrawLine dl1;
Vector v1;
DrawVector dv1;
Polygon pol;
DrawPolygon dpol;
Circle cir;
DrawCircle dcir;
//Exercise B
Point p2;
DrawPoint dp2;
SegmentLine s2;
DrawSegment ds2;
Line l2;
DrawLine dl2;
RayLine r2;
DrawRay dr2;
Circle cir2;
DrawCircle dcir2;
Circle cir3;
DrawCircle dcir3;
Circle cir4;
DrawCircle dcir4;
TDelaunay td;
DrawTDelaunay dtd;
TDelaunay td2;
DrawTDelaunay dtd2;
PointCloud pcTD;
DrawPointCloud dpcTD;
PointCloud pcTD2;
DrawPointCloud dpcTD2;
PointCloud pcTD2P;
List<Point> puntosNube = new ArrayList<Point>(50);
public Test(String title) {
super(title);
// 1. specify a drawable: canvas
capabilities = new GLCapabilities();
capabilities.setDoubleBuffered(false);
canvas = new GLCanvas();
// 2. listen to the events related to canvas: reshape
canvas.addGLEventListener(this);
// 3. add the canvas to fill the Frame container
add(canvas, BorderLayout.CENTER);
// 4. interface to OpenGL functions
gl = canvas.getGL();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// animator.stop(); // stop animation
System.exit(0);
}
});
addMouseListener(this);
addKeyListener(this);
}
protected void initExerciseA() throws Exception {
RandomGen random = RandomGen.getInst();
pc = new PointCloud(POINT_CLOUD_VERT);
//pc = new PointCloud("nube17.txt");
pc.save("nube17.txt");
dc = new DrawPointCloud(pc);
//Draw a point
p1 = new Point(4,3);
dp1 = new DrawPoint(p1);
//Segments with 2 random points
s1 = new SegmentLine(pc.getPoint(random.nextUInt() % POINT_CLOUD_VERT), pc.getPoint(random.nextUInt() % POINT_CLOUD_VERT));
ds1 = new DrawSegment(s1);
//Ray with random points
r1 = new RayLine(pc.getPoint(random.nextUInt() % POINT_CLOUD_VERT), pc.getPoint(random.nextUInt() % POINT_CLOUD_VERT));
dr1 = new DrawRay(r1);
//Liney with random points
l1 = new Line(pc.getPoint(random.nextUInt() % POINT_CLOUD_VERT), pc.getPoint(random.nextUInt() % POINT_CLOUD_VERT));
dl1 = new DrawLine(l1);
// Polygon
//Polygon poligono = new Polygon("./prueba.txt");
//poligono.save("guardado.txt");
ArrayList< Point> coord = new ArrayList< Point>();
GeomMethods.get4Corners(coord, pc);
Polygon pol = GeomMethods.create4Polygon(coord.get(0), coord.get(1), coord.get(2), coord.get(3));
pol.save("polEsquinas.txt");
encontrarConvexo(pol);
pol.save("convexPol");
dpol = new DrawPolygon(pol);
//Circle located at the most central point
Point center = pc.centralPoint();
Point pr = pc.getPoint(random.nextUInt() % POINT_CLOUD_VERT);
double radio = center.distance(pr);
cir = new Circle(center, radio);
dcir = new DrawCircle(cir);
}
protected void drawExerciseA() {
dc.drawObjectC(gl, 1.0f,1.0f,0.0f);
dp1.drawObjectC(gl, 0.3f, 0.0f, 0.8f);
ds1.drawObjectC(gl, 0.5f, 0.2f, 0.3f);
//ds1.drawObjectC(gl, 1.0f, 0.5f, 0.5f);
dr1.drawObjectC(gl, 0.2f, 0.82f, 0.3f);
dl1.drawObjectC(gl, 0.1f, 0.44f, 0.7f);
dpol.drawObjectC(gl, 0.9f, 0.3f, 0.1f);
//dc.drawObjectC(gl, 1.0f, 0.0f, 0.5f);
dcir.drawObjectC(gl, 1.0f, 0.0f, 0.5f);
}
protected void initExerciseB() {
///XXXX
l2 = new Line(new Point(-60,60), new Point(60,60));
dl2 = new DrawLine(l2);
r2 = new RayLine(new Point(-55,-10),new Point(25,30));
dr2 = new DrawRay(r2);
cir2 = new Circle(new Point(0,0),25.0f);
dcir2 = new DrawCircle(cir2);
cir3 = new Circle(new Point(0,-40),25);
dcir3 = new DrawCircle(cir3);
cir4 = new Circle(new Point(35,-40),10);
dcir4 = new DrawCircle(cir4);
s2 = new SegmentLine(new Point(25,40),new Point(25,-35));
ds2 = new DrawSegment(s2);
p2 = new Point(-40,40);
dp2 = new DrawPoint(p2);
System.out.println("La distancia entre el punto P("+p2.getX()+","+p2.getY()+") y la recta A es: "+l2.distPointLine(new Vector(p2)));
System.out.println("La distancia entre el punto P("+p2.getX()+","+p2.getY()+") y el rayo B es: "+r2.distPointRay(new Vector(p2)));
System.out.println("La distancia entre el punto P("+p2.getX()+","+p2.getY()+") y el circulo C es: "+p2.distance(cir2.getCenter()));
Vector intersecta = new Vector();
Vector intersecta2 = new Vector();
if(l2.intersecta(r2, intersecta)){
System.out.println("La recta A intersecta con el rayo B en el punto "+intersecta.getX()+","+intersecta.getY());
}else{
System.out.println("La recta A NO intersecta con el rayo B");
}
if(l2.intersecta(s2, intersecta)){
System.out.println("La recta A intersecta con el segmento D en el punto "+intersecta.getX()+","+intersecta.getY());
}else{
System.out.println("La recta A NO intersecta con el segmento D");
}
if(r2.intersecta(s2, intersecta)){
System.out.println("El rayo B intersecta con el segmento D en el punto "+intersecta.getX()+","+intersecta.getY());
}else{
System.out.println("El rayo B NO intersecta con el segmento D");
}
Circle.RelationCircleLine relacion = cir2.intersects(l2, intersecta, intersecta2);
if(relacion==Circle.RelationCircleLine.INTERSECTS){
System.out.println("El circulo C intersecta con la recta A en los puntos "+intersecta.getX()+","+intersecta.getY()+
" y "+intersecta2.getX()+","+intersecta2.getY());
}
if(relacion==Circle.RelationCircleLine.TANGENTS){
System.out.println("El circulo C es tangente con la recta A en el punto "+intersecta.getX()+","+intersecta.getY());
}
if(relacion == Circle.RelationCircleLine.NO_INTERSECTS){
System.out.println("El circulo C NO intersecta con la recta A");
}
relacion = cir2.intersects(r2, intersecta, intersecta2);
if(relacion==Circle.RelationCircleLine.INTERSECTS){
System.out.println("El circulo C intersecta con el rayo B en los puntos "+intersecta.getX()+","+intersecta.getY()+
" y "+intersecta2.getX()+","+intersecta2.getY());
}
if(relacion==Circle.RelationCircleLine.TANGENTS){
System.out.println("El circulo C es tangente con el rayo B en el punto "+intersecta.getX()+","+intersecta.getY());
}
if(relacion == Circle.RelationCircleLine.NO_INTERSECTS){
System.out.println("El circulo C NO intersecta con el rayo B");
}
relacion = cir2.intersects(s2, intersecta, intersecta2);
if(relacion==Circle.RelationCircleLine.INTERSECTS){
System.out.println("El circulo C intersecta con el segmento D en los puntos "+intersecta.getX()+","+intersecta.getY()+
" y "+intersecta2.getX()+","+intersecta2.getY());
}
if(relacion==Circle.RelationCircleLine.TANGENTS){
System.out.println("El circulo C es tangente con el segmento D en el punto "+intersecta.getX()+","+intersecta.getY());
}
if(relacion == Circle.RelationCircleLine.NO_INTERSECTS){
System.out.println("El circulo C NO intersecta con el segmento D");
}
System.out.println("Los circulos 1 y 2 son: "+ cir2.circleRelation(cir3));
System.out.println("Los circulos 2 y 3 son: "+cir3.circleRelation(cir4));
}
protected void drawExerciseB() {
//XXXXXX
dp2.drawObjectC(gl, 0.3f, 0.0f, 0.8f);
ds2.drawObjectC(gl, 0.5f, 0.2f, 0.3f);
dr2.drawObjectC(gl, 0.2f, 0.82f, 0.3f);
dl2.drawObjectC(gl, 0.1f, 0.44f, 0.7f);
dcir2.drawObjectC(gl, 1.0f, 0.0f, 0.5f);
dcir3.drawObjectC(gl, 1.0f, 0.0f, 0.5f);
dcir4.drawObjectC(gl, 1.0f, 0.0f, 0.5f);
}
protected void initExerciseTDelanuay()
{
pcTD = new PointCloud(100);
td = new TDelaunay(pcTD);
dtd = new DrawTDelaunay(td);
dpcTD = new DrawPointCloud(pcTD);
}
protected void initExerciseTDelanuay2()
{
pcTD2 = new PointCloud(50);
pcTD2P = new PointCloud(pcTD2);
dpcTD2 = new DrawPointCloud(pcTD2P);
int pos1 = Math.abs(RandomGen.getInst().nextInt())%pcTD2.size();
puntosNube.add(pcTD2.removePoint(pos1));
int pos2 = Math.abs(RandomGen.getInst().nextInt())%pcTD2.size();
puntosNube.add(pcTD2.removePoint(pos2));
int pos3 = Math.abs(RandomGen.getInst().nextInt())%pcTD2.size();
puntosNube.add(pcTD2.removePoint(pos3));
Point p1 = puntosNube.remove(0);
Point p2 = puntosNube.remove(0);
Point p3 = puntosNube.remove(0);
td2 = new TDelaunay(p1,p2,p3);
dtd2 = new DrawTDelaunay(td2);
}
protected void drawTDelanuay()
{
dtd.drawObjectC(gl, 1.0f, 0.0f, 0.0f);
dpcTD.drawObjectC(gl, 0.0f, 0.0f, 1.0f);
}
protected void drawTDelanuay2() throws InterruptedException
{
dpcTD2.drawObjectC(gl, 0.0f, 0.0f, 1.0f);
dtd2.drawObjectC(gl, 1.0f, 0.0f, 0.0f);
if (pcTD2.size() != 0)
{
int posRandom = Math.abs(RandomGen.getInst().nextInt()) % pcTD2.size();
puntosNube.add(pcTD2.removePoint(posRandom));
td2.addPoint(puntosNube.remove(0));
Thread.sleep(1000);
}
}
// called once for OpenGL initialization
public void init(GLAutoDrawable drawable) {
animator = new Animator(canvas);
animator.start(); // start animator thread
// display OpenGL and graphics system information
System.out.println("INIT GL IS: " + gl.getClass().getName());
System.err.println(drawable.getChosenGLCapabilities());
System.err.println("GL_VENDOR: " + gl.glGetString(GL.GL_VENDOR));
System.err.println("GL_RENDERER: " + gl.glGetString(GL.GL_RENDERER));
System.err.println("GL_VERSION: " + gl.glGetString(GL.GL_VERSION));
RandomGen random = RandomGen.getInst();
//random.setSeed(5308170449555L);
System.err.println("SEED: " + random.getSeed());
try {
initExerciseA();
initExerciseB();
initExerciseTDelanuay();
initExerciseTDelanuay2();
} catch (Exception ex) {
System.out.println("Error en el dibujado");
ex.printStackTrace();
}
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
int height) {
Draw.WIDTH = WIDTH = width; // new width and height saved
Draw.HEIGH = HEIGHT = height;
//DEEP = deep;
if (Draw.HEIGH < Draw.WIDTH) {
Draw.WIDTH = Draw.HEIGH;
}
if (Draw.HEIGH > Draw.WIDTH) {
Draw.HEIGH = Draw.WIDTH;
}
// 7. specify the drawing area (frame) coordinates
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(0, width, 0, height, -100, 100);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
if (HEIGHT < WIDTH) {
gl.glTranslatef((WIDTH - HEIGHT) / 2, 0, 0);
}
if (HEIGHT > WIDTH) {
gl.glTranslatef(0, (HEIGHT - WIDTH) / 2, 0);
}
}
// called for OpenGL rendering every reshape
public void display(GLAutoDrawable drawable) {
// limpiar la pantalla
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
/* El color de limpiado ser� cero */
gl.glClearDepth(1.0);
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
if (visualizeAxis) {
DrawAxis ejes = new DrawAxis();
ejes.drawObject(gl);
}
if (exercise == A) {
drawExerciseA();
}
if (exercise == B) {
drawExerciseB();
}
if(exercise == C)
{
drawTDelanuay();
}
if(exercise == D)
{
try
{
drawTDelanuay2();
} catch (InterruptedException ex)
{
System.out.println(ex.getMessage());
}
}
gl.glFlush();
}
// called if display mode or device are changed
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
boolean deviceChanged) {
}
public static void main(String[] args) {
Draw.HEIGH = HEIGHT;
Draw.WIDTH = WIDTH;
Test frame = new Test("Prac1. Algoritmos Geometricos");
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
}
public void encontrarConvexo(Polygon pol)
{
boolean encontrado = false;
int cont = 0;
Point p = pc.getPoint(cont);
int i = 0;
int j = 1;
while(!encontrado)
{
if(p.classify(pol.getVertexAt(i), pol.getVertexAt(j)) == PointClassification.RIGHT)
{
encontrado = true;
pol.add(p, j);
}else{
cont++;
if(cont == POINT_CLOUD_VERT)
{
i = j;
j = (j+1)%pol.vertexSize();
cont = 0;
}
else
{
p = pc.getPoint(cont);
}
}
}
for(int w = 0; w < pol.vertexSize(); w++)
{
pol.set(pol.getVertexAt(w), w);
}
}
public void mouseClicked(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
System.out.println("Pulsado el boton del raton: "+e.getButton());
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
public void keyPressed(KeyEvent e)
{
switch (e.getKeyChar()) {
case 'E':
case 'e':
visualizeAxis = !visualizeAxis;
canvas.repaint();
break;
case 'a':
case 'A':
exercise = A;
break;
case 'b':
case 'B':
exercise = B;
break;
case 'c':
case 'C':
exercise = C;
break;
case 'd':
case 'D':
exercise = D;
break;
case 27: // esc
System.exit(0);
}
}
public void keyReleased(KeyEvent e)
{
}
}
Valora esta pregunta
0