Ayuda repaint
Publicado por Ernesto (2 intervenciones) el 21/10/2014 01:27:59
Buenas, Alguien me puede explicar por qué no funciona el repaint() del método anim()?
Gracias y un saludo.
Gracias y un saludo.
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
public class Main2 {
public static void main(String[] args) {
JFrame Frame=new JFrame("JUEGO");
Frame.setBounds(200, 100, 500, 400);
Frame.setDefaultCloseOperation(Frame.EXIT_ON_CLOSE);
Frame.setVisible(true);
Animacion A1= new Animacion();
Frame.addKeyListener(A1);
Frame.add(A1);
}
}
class Animacion extends JPanel implements KeyListener{
private File IMG[]=new File[5];
private int pos=0;
private Image[] Imagen= new Image[5];
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for(int i= 0; i<5;i++){
IMG[i]=new File("src/movePackage/p/p"+i+".png");
}
for(int i= 0; i<5;i++){
try{
Imagen[i]=ImageIO.read(IMG[i]);
}
catch(IOException e){
System.out.println("ERROR");
}
}
g.drawImage(Imagen[pos], 100, 100, null);
}
public void Anim(){
for(int i = 0; i <5;i++){
pos =i;
repaint();
System.out.println("blucle");
try{
Thread.sleep(200);
}
catch(Exception e){
}
}
for(int i = 4; i>-1;i--){
pos =i;
repaint();
System.out.println("blucle2");
}
}
private int codigo;
public void keyPressed(KeyEvent e) {
codigo=e.getKeyCode();
System.out.println("EVENT");
if (codigo == 82){
Anim();
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
Valora esta pregunta


0