import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import javax.swing.Timer;
public class FrameMain extends javax.swing.JFrame {
final int speed = 5;
int dirx = speed;
int diry = speed;
/** Creates new form FrameMain */
public FrameMain() {
initComponents();
setLocationRelativeTo(null);
bullet.setVisible(false);
//ANIMATE THE TARGET
Timer t = new Timer(1, new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(obj1.getLocation().x>=500)dirx = -dirx;
if(obj1.getLocation().x<=0)dirx = speed;
obj1.setLocation(obj1.getLocation().x+dirx, obj1.getLocation().y);
}
});
t.start();
}
/** 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">
private void initComponents() {
obj = new javax.swing.JLabel();
obj1 = new javax.swing.JLabel();
bullet = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setMinimumSize(new java.awt.Dimension(500, 500));
setResizable(false);
addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseMoved(java.awt.event.MouseEvent evt) {
formMouseMoved(evt);
}
});
addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
formKeyPressed(evt);
}
});
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
obj.setBackground(new java.awt.Color(0, 0, 0));
obj.setText("jLabel1");
obj.setOpaque(true);
getContentPane().add(obj, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 370, 20, 20));
obj1.setBackground(new java.awt.Color(0, 0, 0));
obj1.setText("jLabel1");
obj1.setOpaque(true);
getContentPane().add(obj1, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 70, 20, 20));
bullet.setText("*");
getContentPane().add(bullet, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 370, 10, -1));
pack();
}// </editor-fold>
private void formMouseMoved(java.awt.event.MouseEvent evt) {
}
//KEY LISTENERS
private void formKeyPressed(java.awt.event.KeyEvent evt) {
if(evt.getKeyCode()==KeyEvent.VK_RIGHT)
obj.setLocation(obj.getLocation().x+2, obj.getLocation().y);
if(evt.getKeyCode()==KeyEvent.VK_LEFT)
obj.setLocation(obj.getLocation().x-2, obj.getLocation().y);
if(evt.getKeyCode()==KeyEvent.VK_SPACE)
checkCollision();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FrameMain().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel bullet;
private javax.swing.JLabel obj;
private javax.swing.JLabel obj1;
// End of variables declaration
//RELEASE BULLET AND CHECK FOR COLLISION
private void checkCollision() {
obj.setLocation(obj.getLocation().x, obj.getLocation().y);
bullet.setLocation(obj.getLocation());
bullet.setVisible(true);
bullet.setVisible(true);
Timer t = new Timer(5, new ActionListener() {
public void actionPerformed(ActionEvent e) {
bullet.setLocation(bullet.getLocation().x, bullet.getLocation().y-5);
Rectangle r1 = new Rectangle(obj1.getLocation().x, obj1.getLocation().y, obj1.getWidth(), obj1.getHeight());
Rectangle r = new Rectangle(bullet.getLocation().x, bullet.getLocation().y, bullet.getWidth(), bullet.getHeight());
if(r.intersects(r1)){
bullet.setVisible(false);
obj1.setVisible(false);
JOptionPane.showMessageDialog(null, "THIS IS SPARTA!!");
}
}
});
t.start();
}
}