Java - Ayuda repaint

 
Vista:

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.



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
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Ayuda repaint

Publicado por Tom (1831 intervenciones) el 21/10/2014 12:44:55
Sin mirar mucho más, lo primero es que necesitas hacer pack().

1
2
3
4
5
6
7
8
9
10
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);
    frame.pack();
}

Y además lo más probable es que Animacion no esté encontrando los ficheros .png que intentas abrir. Te aparecería la traza ERROR.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Ayuda repaint

Publicado por Ernesto (2 intervenciones) el 21/10/2014 16:34:25
Si que los encuentra... si hago el mismo método Anim() sin los bucles funciona... Pero le tengo que dar a la tecla cada vez... para que pase a la siguiente imagen. Gracias por la respuesta, lo probaré.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar