BUCLE INFINITO DE JFRAMES
Publicado por Bonzo96 (3 intervenciones) el 19/11/2018 00:56:07
Que tal amigos, quisiera saber por que esta pasando este pequeño problema en mi programa.
Les comento que es un juego basado en simón dice,
se implemento un método random, que, al equivocarte en la secuencia que te da el juego aparece un mensaje de FALLASTE, después automáticamente y de forma random se abre un jframe con una imagen, de los 32 jframes que hay en el proyecto, aquí solo utilice dos como una prueba para ver si funcionaba.
Quisiera saber que estoy haciendo mal y poderlo arreglar lo antes posible.
les paso el documento con el código
gracias
Les comento que es un juego basado en simón dice,
se implemento un método random, que, al equivocarte en la secuencia que te da el juego aparece un mensaje de FALLASTE, después automáticamente y de forma random se abre un jframe con una imagen, de los 32 jframes que hay en el proyecto, aquí solo utilice dos como una prueba para ver si funcionaba.
Quisiera saber que estoy haciendo mal y poderlo arreglar lo antes posible.
les paso el documento con el código
gracias
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
package simon;
import simon.Renderer;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.Timer;
public class Simon
implements ActionListener, MouseListener
{
public static Simon simon;
public Renderer renderer;
public static final int WIDTH = 800;
public static final int HEIGHT = 800;
public int flashed = 0;
public int dark;
public int ticks;
public int indexPattern;
public boolean creatingPattern = true;
public ArrayList<Integer> pattern;
public Random random;
private boolean gameOver;
public Simon()
{
JFrame frame = new JFrame("Simon");
Timer timer = new Timer(20, this);
this.renderer = new Renderer();
frame.setSize(810, 830);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.addMouseListener(this);
frame.setResizable(false);
frame.add(this.renderer);
frame.setDefaultCloseOperation(3);
start();
timer.start();
}
void calculo() {
Random rand = new Random();
int x = rand.nextInt(1);
switch (x) {
case 0:
problema1 p1 = new problema1();
p1.setVisible(true);
break;
case 1:
problema2 p2 = new problema2();
p2.setVisible(true);
break;
}
}
public void start()
{
this.random = new Random();
this.pattern = new ArrayList();
this.indexPattern = 0;
this.dark = 2;
this.flashed = 0;
this.ticks = 0;
}
public static void main(String[] args)
{
simon = new Simon();
}
public void actionPerformed(ActionEvent e)
{
this.ticks += 1;
if (this.ticks % 20 == 0)
{
this.flashed = 0;
if (this.dark >= 0) {
this.dark -= 1;
}
}
if (this.creatingPattern)
{
if (this.dark <= 0)
{
if (this.indexPattern >= this.pattern.size())
{
this.flashed = (this.random.nextInt(40) % 4 + 1);
this.pattern.add(Integer.valueOf(this.flashed));
this.indexPattern = 0;
this.creatingPattern = false;
}
else
{
this.flashed = ((Integer)this.pattern.get(this.indexPattern)).intValue();
this.indexPattern += 1;
}
this.dark = 2;
}
}
else if (this.indexPattern == this.pattern.size())
{
this.creatingPattern = true;
this.indexPattern = 0;
this.dark = 2;
}
this.renderer.repaint();
}
public void paint(Graphics2D g)
{
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (this.flashed == 1) {
g.setColor(Color.GREEN);
} else {
g.setColor(Color.GREEN.darker());
}
g.fillRect(0, 0, 400, 400);
if (this.flashed == 2) {
g.setColor(Color.RED);
} else {
g.setColor(Color.RED.darker());
}
g.fillRect(400, 0, 400, 400);
if (this.flashed == 3) {
g.setColor(Color.ORANGE);
} else {
g.setColor(Color.ORANGE.darker());
}
g.fillRect(0, 400, 400, 400);
if (this.flashed == 4) {
g.setColor(Color.BLUE);
} else {
g.setColor(Color.BLUE.darker());
}
g.fillRect(400, 400, 400, 400);
g.setColor(Color.BLACK);
g.fillRoundRect(220, 220, 350, 350, 300, 300);
g.fillRect(334, 0, 114, 800);
g.fillRect(0, 334, 800, 114);
g.setColor(Color.CYAN);
g.setStroke(new BasicStroke(200.0F));
g.drawOval(-100, -100, 1000, 1000);
g.setColor(Color.BLACK);
g.setStroke(new BasicStroke(10.0F));
g.drawOval(0, 0, 800, 800);
g.setColor(Color.WHITE);
g.setFont(new Font("Arial", 1, 142));
if (this.gameOver) {
g.setColor(Color.RED);
g.drawString("FALLASTE", 50, 442);
this.calculo();
} else {
g.drawString(this.indexPattern + "/" + this.pattern.size(), 300, 442);
}
}
public void mousePressed(MouseEvent e)
{
int x = e.getX();int y = e.getY();
if ((!this.creatingPattern) && (!this.gameOver))
{
if ((x > 0) && (x < 400) && (y > 0) && (y < 400))
{
this.flashed = 1;
this.ticks = 1;
}
else if ((x > 400) && (x < 800) && (y > 0) && (y < 400))
{
this.flashed = 2;
this.ticks = 1;
}
else if ((x > 0) && (x < 400) && (y > 400) && (y < 800))
{
this.flashed = 3;
this.ticks = 1;
}
else if ((x > 400) && (x < 800) && (y > 400) && (y < 800))
{
this.flashed = 4;
this.ticks = 1;
}
if (this.flashed != 0) {
if (((Integer)this.pattern.get(this.indexPattern)).intValue() == this.flashed) {
this.indexPattern += 1;
} else {
this.gameOver = true;
}
}
}
else if (this.gameOver)
{
start();
this.gameOver = false;
}
}
Valora esta pregunta


0