Java - Vais a flipar con esto.

 
Vista:

Vais a flipar con esto.

Publicado por Jord (4 intervenciones) el 08/04/2017 15:06:06
Hola a todos,

llevo un rato haciendo un pequeño y sencillo juego que usa un jframe para representar varias casillas, cada una tiene un valor entre 1 y 3, no se pueden pisar las que tienen un 3 y te desplazas tantas casillas como valor tenga la que estés posicionado. Hasta ahí todo perfecto, pero a la hora de poner que pulsando R el tablero se reinicia me he encontrado que al reiniciarlo varias veces el tamaño de este se va reduciendo... Aquí os dejo el código por si alguien le encuentra explicación a este hecho cuanto menos curioso.

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
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
 
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
 
public class FinestraCompleta implements KeyListener{
 
	int x = 0;
	int y = 0;
 
	int valors[][] = new int[10][10];
	JPanel[][] panel = new JPanel[10][10];
	JLabel[][] label = new JLabel[10][10];
	JFrame finestra;
	GridBagConstraints gbc = new GridBagConstraints();
	ImageIcon bandera = new ImageIcon("src/Codig/Bandera.png");
 
	public FinestraCompleta(){
 
		creacioFinestra();
		creacioGraella();
 
		finestra.setSize(351, 351);
 
	}
 
	public void creacioFinestra(){
 
		finestra = new JFrame("Laberint");
		finestra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		finestra.setSize(350, 350);
		finestra.setLocationRelativeTo(finestra);
		finestra.setLayout(new GridBagLayout());
		finestra.addKeyListener(this);
		finestra.setVisible(true);
 
	}
 
	public void creacioGraella(){
 
		for(int x = 0; x < 10; x++){
			for(int y = 0; y < 10; y++){
 
				valors[x][y] = (int)(Math.random()* 3 + 1);
				gbc.gridx = x;
				gbc.gridy = y;
				gbc.weightx = 1.0;
				gbc.weighty = 1.0;
				gbc.fill = GridBagConstraints.BOTH;
				panel[x][y] = new JPanel();
				finestra.getContentPane().add(panel[x][y], gbc);
				label[x][y] = new JLabel((Integer.toString(valors[x][y])));
				panel[x][y].add(label[x][y]);
			}
		}
 
			panel[0][0].remove(label[0][0]);
			valors[0][0] = 1;
			gbc.gridx = 0;
			gbc.gridy = 0;
			label[0][0] = new JLabel("1");
			panel[0][0].add(label[0][0], gbc);
 
			/**/
 
	}
 
	public void moviment(KeyEvent arg0){
 
		if(arg0.getKeyCode() == 37){
			if(x - valors[x][y] < 0){
 
				finestraError("No pots sortir del mapa!");
 
			} else if(valors[x - valors[x][y]][y] == 3){
 
				finestraError("No pots trepitjar el nombre 3...");
 
			} else {
 
				decolorar(x, y);
				x -= valors[x][y];
				colorar(x, y);
 
			}
 
		}
		if(arg0.getKeyCode() == 38){
			if(y - valors[x][y] < 0){
 
				finestraError("No pots sortir del mapa!");
 
			} else if(valors[x][y - valors[x][y]] == 3){
 
				finestraError("No pots trepitjar el nombre 3...");
 
			} else {
 
				decolorar(x, y);
				y -= valors[x][y];
				colorar(x, y);
 
			}
 
		}
		if(arg0.getKeyCode() == 39){
			if(x + valors[x][y] > 9){
 
				finestraError("No pots sortir del mapa!");
 
			} else if(valors[x + valors[x][y]][y] == 3){
 
				finestraError("No pots trepitjar el nombre 3...");
 
			} else {
 
				decolorar(x, y);
				x += valors[x][y];
				colorar(x, y);
 
			}
 
		}
		if(arg0.getKeyCode() == 40){
			if(y + valors[x][y] > 9){
 
				finestraError("No pots sortir del mapa!");
 
			} else if(valors[x][y + valors[x][y]] == 3){
 
				finestraError("No pots trepitjar el nombre 3...");
 
			} else {
 
				decolorar(x, y);
				y += valors[x][y];
				colorar(x, y);
 
			}
 
		}
 
	}
 
	public void colorar(int x, int y){
 
		panel[x][y].setBackground(Color.BLACK);
		label[x][y].setForeground(Color.WHITE);
 
	}
	public void decolorar(int x, int y){
 
		panel[x][y].setBackground(null);
		label[x][y].setForeground(Color.BLACK);
 
	}
 
	public void finestraError(String s){
 
		JFrame finestraERROR = new JFrame("ERROR");
		finestraERROR.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		finestraERROR.add(new JLabel(s));
		finestraERROR.setSize(240, 50);
		finestraERROR.setLocationRelativeTo(finestra);
		finestraERROR.setVisible(true);
 
	}
 
	public void reiniciar(){
 
		System.out.println(finestra.getSize());
		Dimension aux = finestra.getSize();
 
		finestra.dispose();
		creacioFinestra();
		creacioGraella();
 
		finestra.setSize(aux);
 
	}
 
	@Override
	public void keyPressed(KeyEvent arg0) {}
 
	@Override
	public void keyReleased(KeyEvent arg0) {
		if(arg0.getKeyCode() == 82){
 
			reiniciar();
 
		}
 
		System.out.println(arg0.getKeyCode());
		moviment(arg0);
 
	}
 
	@Override
	public void keyTyped(KeyEvent arg0) {}
 
	public static void main (String[] args){
		new FinestraCompleta();
	}
 
}
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
sin imagen de perfil
Val: 349
Bronce
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

Vais a flipar con esto.

Publicado por Andrés (340 intervenciones) el 08/04/2017 18:12:45
Es verdad, se hace pequeño, prueba modificando el método reiniciar con este:

1
2
3
4
5
6
7
8
9
10
11
12
public void reiniciar() {
 
        System.out.println(finestra.getSize());
 
        finestra.getContentPane().removeAll();
 
        creacioGraella();
 
        finestra.validate();
        finestra.repaint();
 
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar

Vais a flipar con esto.

Publicado por Jordi (3 intervenciones) el 09/04/2017 22:26:29
Me ha ido súper bien, muchas gracias y perdón por no haber respondido antes.
Estoy encantado con este nuevo método para reiniciar. ¡Muchas gracias!
P.D: ¿Cómo es que de la otra forma el ancho se iba reduciendo? No entiendo que podía causarlo...
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