0% found this document useful (0 votes)
29 views5 pages

Timer

Metodo para el movimiento de un JButon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views5 pages

Timer

Metodo para el movimiento de un JButon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change


this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this
template
*/
package com.mycompany.practica_1;

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() {

jPanel1 = new javax.swing.JPanel();


jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
abrirVentana(evt);
}
});

jPanel1.setBackground(new java.awt.Color(0, 255, 255));

jButton1.setBackground(new java.awt.Color(0, 255, 255));


jButton1.setForeground(new java.awt.Color(0, 0, 0));
jButton1.setText("jButton1");
jButton1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
mover(evt);
}
});

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);


jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addContainerGap(203, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(198, 198, 198))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(191, 191, 191)
.addComponent(jButton1)
.addContainerGap(202, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);

pack();
}// </editor-fold>//GEN-END:initComponents

private void abrirVentana(java.awt.event.WindowEvent evt) {//GEN-


FIRST:event_abrirVentana
jButton1.requestFocus();
ActionListener Listener=null;
final javax.swing.Timer tiempo=new javax.swing.Timer(100,Listener);
ActionListener tarea=new ActionListener(){

public void actionPerformed(ActionEvent e){

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>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Timer().setVisible(true);
}
});
}

// Variables declaration - do not modify//GEN-BEGIN:variables


private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;
// End of variables declaration//GEN-END:variables
}

You might also like