Timer
Timer
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*
* @author Antonio02
*/
public class Timer extends javax.swing.JFrame {
int x,y;
int lado=0;
/**
* Creates new form Timer
*/
public Timer() {
initComponents();
initComponents();
x=(jPanel1.getWidth()/2)-(jButton1.getWidth()/2);
y=jPanel1.getHeight()/2;
jButton1.move(x, y);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-
BEGIN:initComponents
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
abrirVentana(evt);
}
});
pack();
}// </editor-fold>//GEN-END:initComponents
x=lado==1?x=x+10:x;
x=lado==2?x=x-10:x;
y=lado==3?y=y+10:y;
y=lado==4?y=y-10:y;
jButton1.move(x,y);
if(jButton1.getBounds().x>=(jPanel1.getWidth()-jButton1.getWidth())){
lado=2;
}
if(jButton1.getBounds().x<0){
lado=1;
}
if(jButton1.getBounds().y>=(jPanel1.getHeight()-jButton1.getHeight())){
lado=4;
}
if(jButton1.getBounds().y<=0){
lado=3;
}
}//GEN-LAST:event_abrirVentana
};
tiempo.addActionListener(tarea);
tiempo.start();
}
private void mover(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_mover
int codigo=evt.getKeyCode();
if(codigo==39)
lado=1;
if(codigo==37)
lado=2;
if(codigo==40)
lado=3;
if(codigo==38)
lado=4;
jButton1.move(x,y);
}//GEN-LAST:event_mover
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional)
">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and
feel.
* For details see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Timer.class.getName()).log(java.util.logging.Level.SEVER
E, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Timer.class.getName()).log(java.util.logging.Level.SEVER
E, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Timer.class.getName()).log(java.util.logging.Level.SEVER
E, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Timer.class.getName()).log(java.util.logging.Level.SEVER
E, null, ex);
}
//</editor-fold>