Encerrar processo [RESOLVIDO]

5 respostas
ThiagoA

E ai pessoal tudo beleza!??

Estou tendo problema para encerra um joguinho,

apos passar 15 segundos, queria que a janela fosse fechada !

Alguma dica ??? :cry:

package jog;

// Bibliotecas utilizadas
import java.awt.Color;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class TelaJogo {

    public TelaJogo() {
        JanelaJOG();
    }

    final static boolean shouldFill = true;
    final static boolean shouldWeightX = true;
    final static boolean RIGHT_TO_LEFT = false;
    Barra bar = new Barra();
    static Cronometro tempofigura; 
    static Cronometro tempojogo;
    static boolean loop = false;
    static Component figura;
    static GeraFigura pegafigura = new GeraFigura();
    static JButton botao;
    static Teste_painel_fiugras painelfiguras;
    static JPanel painel;
    static int pontos = 0;
    static String pontotexto = "Pontuação: ";
    static JLabel pontuacao = new JLabel();
    static JFrame frame = new JFrame();
    static JLabel tf = new JLabel();


    // Criando uma orientação das posições dos containers dentro do layot
    public static void addComponentesNoPainel(final Container container) {
        if (RIGHT_TO_LEFT) {
            container.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

        }


        container.setLayout(new GridBagLayout());
        container.setBackground(new Color(0, 153, 153));

// Determinam como um gerenciador do tipo GridBagLayout deve posicionar 
// um dado componente em seu container.
        final GridBagConstraints posicao = new GridBagConstraints();

        if (shouldFill) {
            //natural height, maximum width
            posicao.fill = GridBagConstraints.CENTER;
            posicao.insets = new Insets(5, 5, 5, 5);
        }



        botao = new JButton("Circulo");
        if (shouldWeightX) {
            posicao.weightx = 0.5;
        }
        posicao.fill = GridBagConstraints.HORIZONTAL;
        posicao.gridx = 0;
        posicao.gridy = 2;
        container.add(botao, posicao);
        botao.addActionListener(new AcaoBotao());

        botao = new JButton("Quadrado");
        if (shouldWeightX) {
            posicao.weightx = 0.5;
        }
        posicao.fill = GridBagConstraints.HORIZONTAL;
        posicao.gridx = 1;
        posicao.gridy = 2;
        container.add(botao, posicao);
        botao.addActionListener(new AcaoBotao());

        botao = new JButton("Retangulo");
        if (shouldWeightX) {
            posicao.weightx = 0.5;
        }
        posicao.fill = GridBagConstraints.HORIZONTAL;
        posicao.gridx = 2;
        posicao.gridy = 2;
        container.add(botao, posicao);
        botao.addActionListener(new AcaoBotao());

        botao = new JButton("Losango");
        if (shouldWeightX) {
            posicao.weightx = 0.5;
        }
        posicao.fill = GridBagConstraints.HORIZONTAL;
        posicao.gridx = 1;
        posicao.gridy = 3;
        container.add(botao, posicao);
        botao.addActionListener(new AcaoBotao());

        botao = new JButton("Hexagono");
        if (shouldWeightX) {
            posicao.weightx = 0.5;
        }
        posicao.fill = GridBagConstraints.HORIZONTAL;
        posicao.gridx = 0;
        posicao.gridy = 3;
        container.add(botao, posicao);
        botao.addActionListener(new AcaoBotao());

        botao = new JButton("Triangulo");
        if (shouldWeightX) {
            posicao.weightx = 0.5;
        }
        posicao.fill = GridBagConstraints.HORIZONTAL;
        posicao.gridx = 2;
        posicao.gridy = 3;
        container.add(botao, posicao);
        botao.addActionListener(new AcaoBotao());
        
         botao = new JButton("Sair");
        if (shouldWeightX) {
            posicao.weightx = 0.1;
        }
        posicao.fill = GridBagConstraints.HORIZONTAL;
        posicao.gridx = 3;
        posicao.gridy = 3;
        container.add(botao, posicao);
        botao.addActionListener(new AcaoBotao());


        painel = new Painel();
        figura = pegafigura.PegarFigura();
        painel.add(figura);
        posicao.fill = GridBagConstraints.HORIZONTAL;
        posicao.insets = new Insets(10, 10, 50, 10);  //Espaço entre os componentes
        posicao.ipadx = 0;
        posicao.ipady = 600;      //make this component tall
        posicao.weightx = 0.0;
        posicao.gridwidth = 3;
        posicao.gridx = 0;
        posicao.gridy = 1;
        container.add(painel, posicao);



        posicao.fill = GridBagConstraints.HORIZONTAL;
        posicao.ipady = 0;       //Volta as definições padrão
        posicao.weighty = 0.2;   //Espaço extra entre os componentes
        posicao.weightx = 0.0;
        posicao.anchor = GridBagConstraints.PAGE_END;
        posicao.insets = new Insets(10, 10, 10, 10);  //Espaço entre os componentes
        posicao.gridx = 2;       //aligned with button 2
        posicao.gridwidth = 1;   //1 columns wide
        posicao.gridy = 1;       //third row
        container.add(tempofigura, posicao);
        
        
        posicao.fill = GridBagConstraints.HORIZONTAL;
        posicao.ipady = 0;       //Volta as definições padrão
        posicao.weighty = 0.2;   //Espaço extra entre os componentes
        posicao.weightx = 0.0;
        posicao.anchor = GridBagConstraints.PAGE_END;
        posicao.insets = new Insets(10, 10, 10, 10);  //Espaço entre os componentes
        posicao.gridx = 1;       //aligned with button 2
        posicao.gridwidth = 1;   //1 columns wide
        posicao.gridy = 1;       //third row
        container.add(tempojogo, posicao);

       
        
        
        pontuacao = new JLabel(pontotexto);
        
        posicao.fill = GridBagConstraints.HORIZONTAL;
        posicao.ipady = 0;       //Volta as definições padrão
        posicao.weighty = 0.2;   //Espaço extra entre os componentes
        posicao.weightx = 0.0;
        posicao.anchor = GridBagConstraints.PAGE_END;
        posicao.insets = new Insets(10, 10, 10, 10);  //Espaço entre os componentes
        posicao.gridx = 0;       //aligned with button 2
        posicao.gridwidth = 1;   //1 columns wide
        posicao.gridy = 1;       //third row
        container.add(pontuacao, posicao);
        
        
            }

    //Classe interna para fazer a troca da figura no painel
    public static void TrocaFigura() {

        painel.remove(figura);
        painel.validate();
        painel.repaint();

        figura = pegafigura.PegarFigura();
        painel.add(figura);
        painel.validate();
        painel.repaint();
        
       tempofigura.Reiniciar();
    }
    
     public static void Pontuacao(Component figura, String actionCommand) {
       
        if(figura.getName().equals(actionCommand)){
        pontos = pontos + 5;       
        pontuacao.setText(pontotexto + pontos);
        pontuacao.validate();
        pontuacao.repaint();
        }
        
        else{
        pontos = pontos - 3;       
        pontuacao.setText(pontotexto + pontos);
        pontuacao.validate();
        pontuacao.repaint();
        }
    }
     
          
   
    //  Classe interna para capturar as açoes dos botõas
    private static class AcaoBotao implements ActionListener {
        



        @Override
        public void actionPerformed(ActionEvent event) {


            if ("Circulo".equals(event.getActionCommand())) {
                Pontuacao(figura,event.getActionCommand());       
                
                TrocaFigura();
               
                
            }

            if ("Quadrado".equals(event.getActionCommand())) {
                Pontuacao(figura,event.getActionCommand());
                TrocaFigura();
            }

            if ("Retangulo".equals(event.getActionCommand())) {
                Pontuacao(figura,event.getActionCommand());
                TrocaFigura();
            }

            if ("Losango".equals(event.getActionCommand())) {
                Pontuacao(figura,event.getActionCommand());
                TrocaFigura();
            }

            if ("Hexagono".equals(event.getActionCommand())) {
                Pontuacao(figura,event.getActionCommand());
                TrocaFigura();
            }

            if ("Triangulo".equals(event.getActionCommand())) {
                Pontuacao(figura,event.getActionCommand());
                 System.out.println(tempojogo.getCurrent()); 
                TrocaFigura();
            }
            
             if ("Sair".equals(event.getActionCommand())) {
               frame.dispose();
               MenuPrincipal.MenuPrincipal();
             }

        }

      
    }

    private static void JanelaJOG() {

    tempofigura = new Cronometro(5);
    tempojogo =  new Cronometro(15);
    pontos = 0;
     
       //  Criar e torna visivel a janela principal do jogo
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//  Adiciona todos os componentes as painel.
        addComponentesNoPainel(frame.getContentPane());
        
        
// Tornar a janela visivel ao usuario
        frame.setTitle("Jog - Jogo das Figuras Geometricas");
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
//        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);  
        frame.setVisible(true);
      
     
    }

    
  
  
  
    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this applications GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            private boolean cancelado;

@Override
       
            public void run() {
     

      new TelaJogo();
  

            }
        });
    }}

OBIRGADO!!

5 Respostas

luxu

sleep() do Thread. pesquisa sobre isso.

ViniGodoy

Use a classe javax.swing.Timer.

O sleep da classe Thread não irá te ajudar nesse caso.

ThiagoA

Valeu pelas dicas pessoal!

Fiz uma class que tem a capaciade de criar e

destruir as threads sempre que eu precissar!

obrigado!

ViniGodoy

E que tal postar a solução no fórum e ajudar futuros GUJnautas?

ThiagoA

Assim que eu terminar o projeto

posto ele por completo, para ficar mais

facil entender onde e como cada classe é utilizada!

Valeu!

Criado 2 de outubro de 2011
Ultima resposta 3 de out. de 2011
Respostas 5
Participantes 3