Botão Java pausando atualizações gráficas
Então, eu tenho uma aula onde eu tenho que fazer um programa para fazer o Simon. Eu sei que o jeito que estou fazendo isso não é necessariamente o melhor, no entanto, ele tinha alguns requisitos obscuros; é por isso que estou fazendo dessa maneira.
Meu programa está quase concluído, mas eu tenho um problema MAJOR. Quando pressiono o botão de redefinição, chamo um método chamado redefinir que, por sua vez, configura o computador para executar sua primeira jogada.
Durante isso, há atualizações gráficas.
Quando eu chamo o método de redefinição por si só, ele funciona conforme o esperado. Quando pressiono o botão de redefinição, ele faz todas as atualizações gráficas até que seja concluído. Existe uma maneira de executar o método depois que o botão foi pressionado?
Meu programa principal
package game;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import components.*;
@SuppressWarnings("serial")
public class Simonish extends JFrame implements ActionListener, MouseListener {
private Color[][] ColorSwatch = {{Color.RED,Color.PINK},{Color.GREEN,Color.YELLOW}};
private int width = 2;
private int height = 2;
private int panSize = 200;
private SPane[][] panBoard;
private int[] Sequence;
private int CurrentSequenceLeingth = 0;
private int SequenceLeingth = 10000;
private Random r = new Random();
boolean LastButtonClicked = false;
private int LastButtonPressed = 0;
private int sequencePart = 0;
private boolean turn = false; //f=computer t=player
Container pane;
JPanel boardPanel;
ScoreBoard scorePanel;
private Simonish(){
scorePanel = new ScoreBoard(0,width,panSize);
scorePanel.getResetBtn().addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
resetGame();
}
});
this.setTitle("Simonish");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
pane = this.getContentPane();
pane.setLayout(null);
resetGame();
}
private void play(){
if(!turn)
ComputerTurn();
else
PlayerTurn();
}
private void ComputerTurn(){
CurrentSequenceLeingth++;
scorePanel.AddScore(1);
PlaySequenc();
turn = true;
}
private void PlayerTurn(){
if((LastButtonPressed == Sequence[sequencePart]))
{
sequencePart++;
LastButtonClicked = false;
}else
{
loose();
return;
}
if(sequencePart >= CurrentSequenceLeingth)
{
sequencePart = 0;
turn = false;
play();
}
}
private void loose(){
System.out.println("you loose");
resetGame();
}
private void PlaySequenc(){
for(int i = 0 ; i < CurrentSequenceLeingth ; i++)
{
panBoard[Sequence[i]/2][Sequence[i]%2].pressPane();
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
}
}
private void resetGame() {
pane.removeAll();
pane.setPreferredSize(new Dimension(width*panSize, (height) * panSize + 75));
pane.add(scorePanel);
initPanes();
LastButtonClicked = false;
LastButtonPressed = 0;
initBtns();
turn = false;
CurrentSequenceLeingth = 3;
Sequence = new int[SequenceLeingth];
initSeq();
pane.update(pane.getGraphics());
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
play();
}
private void initSeq() {
for(int i = 0 ; i < SequenceLeingth ; i++)
{
Sequence[i] = r.nextInt(4);
}
}
private void initBtns() {
this.panBoard = new SPane[width][height];
for(int w = 0; w < width; w++) {
for(int h = 0; h < height; h++) {
panBoard[w][h] = new SPane(w, h, panSize,ColorSwatch[w][h]);
panBoard[w][h].addMouseListener(this);
pane.add(panBoard[w][h]);
pane.addMouseListener(this);
}
}
}
public static void main(String[] args){
new Simonish();
}
private void initPanes() {
//TODO
}
@Override
public void actionPerformed(ActionEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
}
A classe ScoreBoard
package components;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.Timer;
@SuppressWarnings({ "serial", "unused" })
public class ScoreBoard extends JPanel {
private int score;
private JLabel SimonNumberLabel;
private JLabel timerLabel;
private JButton resetBtn;
public ScoreBoard(int bombsCnt,int w,int w2) {
score = bombsCnt;
SimonNumberLabel = new JLabel("Sequence Leingth: " + Integer.toString(bombsCnt), SwingConstants.CENTER);
resetBtn = new JButton("reset");
setBounds(0, 0, w*w2, 3*25);
this.setLayout(new GridLayout(1,3));
add(SimonNumberLabel);
add(resetBtn);
}
public void AddScore(int update) {
score += update;
SimonNumberLabel.setText("Sequence Leingth: " + Integer.toString(score));
}
public JButton getResetBtn() {
return resetBtn;
}
}
A classe SPane
package components;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
@SuppressWarnings("serial")
public class SPane extends JPanel{
Color C;
Color DC;
int x;
int y;
public SPane(int x, int y, int size, Color colorSwatch) {
this();
this.x = x;
this.y = y;
this.setLayout(new GridLayout(x+1,y+1));
this.setBounds(x*size, y*size+75, size, size);
C = colorSwatch;
DC = C;
DC = DC.darker();
this.setBackground(DC);
this.setVisible(true);
}
public SPane() {
this.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
}
public void resetSPane() {
// TODO Auto-generated method stub
}
public void pressPane()
{
this.setBackground(C);
System.out.println("dsfdsfsdfsdf");
try{
Thread.sleep(1000);
}catch (Exception e) {}
this.setBackground(DC);
}
public void clicked() {
this.setBackground(Color.GREEN);
}
}