import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
public class NewJFrame extends javax.swing.JFrame {
/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
myinit();
getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
//myinit();
}
double speed = 1;
double globalFPS = 60;
boolean magnetism = false;
Timer t = new Timer((int)(1000/(speed*globalFPS)), new ActionListener() {public void actionPerformed(ActionEvent e) {timerfunc3();}});
void timerfunc3()
{
ch.update();
puck1.update();
//puck2.update();
puck3.update();
puck4.update();
if (magnetism)
{
double dist = ch.distance(puck1, puck4);
puck1.velx += 200/globalFPS * (puck4.posx - puck1.posx)/dist;
puck1.vely += 200/globalFPS * (puck4.posy - puck1.posy)/dist;
}
}
/** 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() {
l1 = new javax.swing.JLabel();
l2 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
l3 = new javax.swing.JLabel();
l4 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setMinimumSize(new java.awt.Dimension(400, 500));
setResizable(false);
addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
formMouseClicked(evt);
}
public void mouseEntered(java.awt.event.MouseEvent evt) {
formMouseEntered(evt);
}
public void mousePressed(java.awt.event.MouseEvent evt) {
formMousePressed(evt);
}
public void mouseReleased(java.awt.event.MouseEvent evt) {
formMouseReleased(evt);
}
});
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);
}
public void keyReleased(java.awt.event.KeyEvent evt) {
formKeyReleased(evt);
}
});
getContentPane().setLayout(null);
l1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/transp.png"))); // NOI18N
l1.setText("jLabel1");
getContentPane().add(l1);
l1.setBounds(160, 130, 40, 40);
l2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/transp.png"))); // NOI18N
l2.setText("jLabel1");
getContentPane().add(l2);
l2.setBounds(340, 10, 40, 40);
jButton1.setText("Start Tmer");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.setBounds(40, 20, 83, 23);
jButton2.setText("Stop Timer");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2);
jButton2.setBounds(130, 20, 90, 23);
jButton3.setText("Debug");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
getContentPane().add(jButton3);
jButton3.setBounds(240, 20, 63, 23);
l3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/30ps.png"))); // NOI18N
l3.setText("jLabel1");
getContentPane().add(l3);
l3.setBounds(60, 260, 30, 30);
l4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/transp.png"))); // NOI18N
l4.setText("jLabel1");
getContentPane().add(l4);
l4.setBounds(160, 250, 40, 40);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
t.start();
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
t.stop();
}//GEN-LAST:event_jButton2ActionPerformed
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jButton3ActionPerformed
private void formMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMousePressed
magnetism = true;
}//GEN-LAST:event_formMousePressed
private void formMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseReleased
// TODO add your handling code here:
magnetism = false;
}//GEN-LAST:event_formMouseReleased
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
void myinit()
{
puck1 = new Puck(l1, 100, 0, globalFPS);
puck2 = new Puck(l2, 120, 80, globalFPS);
puck3 = new Puck(l3, -120, 0, globalFPS);
puck4 = new Puck(l4, 0, 0, globalFPS);
puck1.frictionEnabled = false;
puck2.frictionEnabled = false;
puck3.frictionEnabled = false;
puck4.frictionEnabled = false;
ch = new CollisionHandler(0, 0, 400, 470);
ch.add(puck1);
//ch.add(puck2);
ch.add(puck3);
ch.add(puck4); }
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel l1;
private javax.swing.JLabel l2;
private javax.swing.JLabel l3;
private javax.swing.JLabel l4;
// End of variables declaration//GEN-END:variables
Puck puck1;
Puck puck2;
Puck puck3;
Puck puck4;
CollisionHandler ch;
}