logotipo

img_google
H:\Sun\AppServer\jdk\bin\GrandPrix\src\grandprix\GranPrix.java
/*
 *
 **Autor original del código: Jose Castillo. http://lawebdeljose.blogspot.com 
 *
 *Este código puede ser modificado y usado como se quiera. Pero, por favor, respetad estos comentarios. 
 *Ha sido creado usando el IDE Net Beans 5.0 .
 *
 Este ejercicio sirve para ver el tema del uso de una clase del API como es Timer
 puede ser útil para ver cómo funciona una ventana simple de Java.
 *
 *
 *
 *La ventana es muy simple porque lo que interesa es lo que hace dentro.
 *
 *
 **/

        
package grandprix;
import java.awt.event.*;
import javax.swing.Timer;

public class GranPrix extends javax.swing.JFrame {
    public static int delay=1000; //el retardo
    public static Timer t; //el retardante
    public static int lapatata=0;
    public static int min=0;
    public static int result;
    public static int max=50000;
    /** Creates new form GranPrix */
    public GranPrix() {
        initComponents();
    }
    
    
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
    private void initComponents() {
        jButton2 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jButton2.setText("La patata");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        jTextField1.setText("0");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(22, 22, 22)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton2)
                .add(18, 18, 18)
                .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 82, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(56, 56, 56)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jButton2)
                            .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(layout.createSequentialGroup()
                        .add(25, 25, 25)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 241, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(34, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>                        

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {                                      
//  Aquí es donde va resolviendo el problema, está dentro de un timer para que tarde un poco cada vez
     lapatata=Integer.valueOf( jTextField1.getText() ).intValue();
        ActionListener taskPerformer = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
          //...Perform a task...
          result=(max+min)/2;
          if (result<lapatata){
          min=result;
          jTextArea1.setText(jTextArea1.getText()+"Es "+result+"? \n");
          }
          if(result>lapatata) {
          max=result;
          jTextArea1.setText(jTextArea1.getText()+"Es "+result+"? \n");
          }
          if(result==lapatata){
          max=50000;
          min=0;
           jTextArea1.setText(jTextArea1.getText()+"Resultado: "+result);  
              t.stop();
          
          }
          
          
          
          
      }
  };
  t=new Timer(delay, taskPerformer);
  t.start();
     
    }                                     
    
    
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GranPrix().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration                   
    
}