Insertar y mover imágenes
Publicado por María de Alejandría (13 intervenciones) el 24/03/2017 19:26:10
Saludos. Tengo el siguiente programa implementado en Java el cual permite mover una esfera usando las flechas del teclado. No obstante me gustaría hacerlo moviendo un pacman a través de una combinación de varias imágenes y no consigo que aparezcan a la hora de ejecutar el programa. Por si es necesario dejo el código:
Clase main:
Clase Animacion:
Muchas gracias de antemano y perdón por las molestias
Clase main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package main;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) throws Exception {
JFrame jf = new JFrame("Ejercicio1");
jf.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
jf.setResizable(false);
Animacion ball = new Animacion();
jf.getContentPane().add(ball);
jf.pack();
jf.setVisible(true);
ball.cicloPrincipalJuego();
}
}
Clase Animacion:
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
package main;
import java.awt.Color;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Animacion extends JComponent{
private final static int ANCHO = 768;
private final static int ALTO = 384;
private final static int DIAMETRO = 40;
private float x, y;
private float vx, vy;
private boolean arriba, abajo, izquierda, derecha;
public Animacion() {
setPreferredSize(new Dimension(ANCHO, ALTO));
//Lugar donde aparece el Pacman al comenzar la ejecución del programa
x = 10;
y = 20;
addKeyListener(new KeyAdapter() {
//Tecla pulsada, el Pacman se mueve
public void keyPressed(KeyEvent e) {
actualizar(e.getKeyCode(), true);
}
//El Pacman no se mueve
public void keyReleased(KeyEvent e) {
actualizar(e.getKeyCode(), false);
}
private void actualizar(int keyCode, boolean pressed) {
//Define el movimiento del Pacman en función de la tecla pulsada
switch (keyCode) {
case KeyEvent.VK_UP:
arriba = pressed;
break;
case KeyEvent.VK_DOWN:
abajo = pressed;
break;
case KeyEvent.VK_LEFT:
izquierda = pressed;
break;
case KeyEvent.VK_RIGHT:
derecha = pressed;
break;
}
}
});
setFocusable(true);
}
//Métodos para determinar las coordenadas del movimiento del Pacman
private float limite(float valor, float min, float max) {
if (valor > max)
valor= max;
if (valor < min)
valor= min;
return valor;
}
private void movimiento(float dt) {
vx = 0;
vy = 0;
if (arriba)
vy = -300;
if (abajo)
vy = 300;
if (izquierda)
vx = -300;
if (derecha)
vx = 300;
x = limite(x + vx * dt, 0, ANCHO - DIAMETRO);
y = limite(y + vy * dt, 0, ALTO - DIAMETRO);
}
//Se define el color del Pacman y sus dimensiones
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, ANCHO, ALTO);
g.setColor(Color.YELLOW);
g.fillOval(Math.round(x), Math.round(y), DIAMETRO, DIAMETRO);
}
private void dibuja() throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
paintImmediately(0, 0, ANCHO, ALTO);
}
});
}
//Define la velocidad de desplazamiento del Pacman en cualquier dirección
public void cicloPrincipalJuego() throws Exception {
long tiempoViejo = System.nanoTime();
while (true) {
long tiempoNuevo = System.nanoTime();
float dt = (tiempoNuevo - tiempoViejo) / 1000000000f;
tiempoViejo = tiempoNuevo;
movimiento(dt);
dibuja();
}
}
}
Muchas gracias de antemano y perdón por las molestias
Valora esta pregunta
0