Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package AsymptoticFreedom; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.XYPlot; /** * * @author Geonmo */ public class AppletAF extends javax.swing.JApplet { /** * Initializes the applet AppletAF */ int pre_x_pos; int pre_y_pos; javax.swing.Timer timer; boolean isTimerOn = false; static double k1 = 10000; static double k2 = 15; JFreeChart chart; @Override public void init() { /* 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(AppletAF.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(AppletAF.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(AppletAF.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(AppletAF.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> //</editor-fold> /* Create and display the applet */ try { java.awt.EventQueue.invokeAndWait(new Runnable() { public void run() { chart = new JFreeChart(new XYPlot()); initComponents(); } }); } catch (Exception ex) { ex.printStackTrace(); } } public static double getK1() { return k1; } public static double getK2() { return k2; } /** * This method is called from within the init() method 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() { selectedParticle = new javax.swing.ButtonGroup(); thePotentialPanel = new GraphViewPanel(chart); theOptionPanel = new javax.swing.JPanel(); theParticleOption = new javax.swing.JLabel(); theProtonButton = new javax.swing.JRadioButton(); thePionButton = new javax.swing.JRadioButton(); theNeutronButton = new javax.swing.JRadioButton(); theKaonButton = new javax.swing.JRadioButton(); theStartButton = new javax.swing.JButton(); theQuitButton = new javax.swing.JButton(); TimerButton = new javax.swing.JToggleButton(); theViewPanel = new AsymptoticFreedom.ViewPanel(); setBackground(new java.awt.Color(240, 240, 240)); thePotentialPanel .setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)); javax.swing.GroupLayout thePotentialPanelLayout = new javax.swing.GroupLayout(thePotentialPanel); thePotentialPanel.setLayout(thePotentialPanelLayout); thePotentialPanelLayout.setHorizontalGroup(thePotentialPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 597, Short.MAX_VALUE)); thePotentialPanelLayout.setVerticalGroup(thePotentialPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 233, Short.MAX_VALUE)); theParticleOption.setText("Particle"); selectedParticle.add(theProtonButton); theProtonButton.setText("Proton"); theProtonButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { theProtonButtonActionPerformed(evt); } }); selectedParticle.add(thePionButton); thePionButton.setText("Pion+"); thePionButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { thePionButtonActionPerformed(evt); } }); selectedParticle.add(theNeutronButton); theNeutronButton.setText("Neutron"); theNeutronButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { theNeutronButtonActionPerformed(evt); } }); selectedParticle.add(theKaonButton); theKaonButton.setText("Kaon+"); theKaonButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { theKaonButtonActionPerformed(evt); } }); theStartButton.setText("Start"); theStartButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { theStartButtonActionPerformed(evt); } }); theQuitButton.setText("Quit"); theQuitButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { theQuitButtonActionPerformed(evt); } }); TimerButton.setText("Timer On"); TimerButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { TimerButtonActionPerformed(evt); } }); javax.swing.GroupLayout theOptionPanelLayout = new javax.swing.GroupLayout(theOptionPanel); theOptionPanel.setLayout(theOptionPanelLayout); theOptionPanelLayout.setHorizontalGroup(theOptionPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(theOptionPanelLayout.createSequentialGroup().addContainerGap() .addGroup(theOptionPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, theOptionPanelLayout .createSequentialGroup().addGap(0, 0, Short.MAX_VALUE) .addGroup(theOptionPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(theProtonButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(thePionButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(theOptionPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(theKaonButton, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(theNeutronButton)) .addGap(76, 76, 76)) .addGroup(theOptionPanelLayout.createSequentialGroup() .addComponent(theParticleOption, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, theOptionPanelLayout.createSequentialGroup().addGroup(theOptionPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(TimerButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(theOptionPanelLayout.createSequentialGroup() .addGap(0, 0, Short.MAX_VALUE) .addComponent(theStartButton, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(theQuitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(64, 64, 64))))); theOptionPanelLayout .setVerticalGroup( theOptionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(theOptionPanelLayout.createSequentialGroup().addGap(28, 28, 28) .addComponent(theParticleOption) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(theOptionPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(thePionButton).addComponent(theKaonButton)) .addGap(18, 18, 18) .addGroup(theOptionPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(theNeutronButton).addComponent(theProtonButton)) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(theOptionPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(theStartButton, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(theQuitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(36, 36, 36) .addComponent(TimerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(81, 81, 81))); theViewPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)); theViewPanel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() { public void mouseDragged(java.awt.event.MouseEvent evt) { theViewPanelMouseDragged(evt); } }); theViewPanel.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent evt) { theViewPanelMousePressed(evt); } }); javax.swing.GroupLayout theViewPanelLayout = new javax.swing.GroupLayout(theViewPanel); theViewPanel.setLayout(theViewPanelLayout); theViewPanelLayout.setHorizontalGroup(theViewPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE)); theViewPanelLayout.setVerticalGroup(theViewPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 347, Short.MAX_VALUE)); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(thePotentialPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(theViewPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(theOptionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(theViewPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(thePotentialPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addComponent(theOptionPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); }// </editor-fold>//GEN-END:initComponents public void resetViewPanel() { //ViewPanel. } private void thePionButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_thePionButtonActionPerformed // TODO add your handling code here: System.out.println("pion selected."); theViewPanel.setParticle(1); theViewPanel.resetPanel(); ((GraphViewPanel) thePotentialPanel).bookingSeries(); }//GEN-LAST:event_thePionButtonActionPerformed private void theKaonButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_theKaonButtonActionPerformed // TODO add your handling code here: System.out.println("Kaon selected."); theViewPanel.setParticle(2); theViewPanel.resetPanel(); ((GraphViewPanel) thePotentialPanel).bookingSeries(); }//GEN-LAST:event_theKaonButtonActionPerformed private void theProtonButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_theProtonButtonActionPerformed // TODO add your handling code here: System.out.println("Proton selected."); theViewPanel.setParticle(3); theViewPanel.resetPanel(); ((GraphViewPanel) thePotentialPanel).bookingSeries(); }//GEN-LAST:event_theProtonButtonActionPerformed private void theStartButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_theStartButtonActionPerformed // TODO add your handling code here: theStartButton.setText("Restart"); TimerButton.setText("Timer on"); TimerButton.setSelected(true); timerStart(); theViewPanel.resetPanel(); ((GraphViewPanel) thePotentialPanel).bookingSeries(); }//GEN-LAST:event_theStartButtonActionPerformed private void theNeutronButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_theNeutronButtonActionPerformed // TODO add your handling code here: System.out.println("Neutron selected."); theViewPanel.setParticle(4); theViewPanel.resetPanel(); ((GraphViewPanel) thePotentialPanel).bookingSeries(); }//GEN-LAST:event_theNeutronButtonActionPerformed private void theViewPanelMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_theViewPanelMouseDragged // TODO add your handling code here: //System.out.printf("Draggred>> x : %d, y : %d\n",evt.getX(), evt.getY()); for (int i = 0; i < theViewPanel.getQuarkSize(); i++) { if (theViewPanel.quark_list.get(i).pos.distance(new Point(evt.getX(), evt.getY())) < 25) { //System.out.printf("Selected %d quark!\n",i); theViewPanel.quark_list.get(i).translate(evt.getX() - pre_x_pos, evt.getY() - pre_y_pos); //System.out.format("%d %d\n",evt.getX()-pre_x_pos,evt.getY()-pre_y_pos); pre_x_pos = evt.getX(); pre_y_pos = evt.getY(); } } theViewPanel.repaint(); }//GEN-LAST:event_theViewPanelMouseDragged private void theViewPanelMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_theViewPanelMousePressed // TODO add your handling code here: //System.out.printf("pressed >> x : %d, y : %d\n",evt.getX(), evt.getY()); for (int i = 0; i < theViewPanel.getQuarkSize(); i++) { if (theViewPanel.quark_list.get(i).pos.distance(new Point(evt.getX(), evt.getY())) < 25) { //System.out.printf("Selected %d quark!\n",i); pre_x_pos = evt.getX(); pre_y_pos = evt.getY(); //theViewPanel.repaint(); } } theViewPanel.repaint(); }//GEN-LAST:event_theViewPanelMousePressed private void theQuitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_theQuitButtonActionPerformed // TODO add your handling code here: setVisible(false); //dispose(); System.exit(0); }//GEN-LAST:event_theQuitButtonActionPerformed private void TimerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TimerButtonActionPerformed // TODO add your handling code here: if (isTimerOn) { timerStop(); TimerButton.setText("Timer off"); TimerButton.setSelected(isTimerOn); } else { timerStart(); TimerButton.setText("Timer on"); TimerButton.setSelected(isTimerOn); } }//GEN-LAST:event_TimerButtonActionPerformed public void timerStart() { if (timer == null) { System.out.println("Start Timer!!"); timer = new javax.swing.Timer(100, new aListener()); timer.stop(); } timer.start(); isTimerOn = true; } public void timerStop() { timer.stop(); isTimerOn = false; } public class aListener implements ActionListener { public void actionPerformed(ActionEvent e) { if (isTimerOn) { //System.out.println("Continue timer!"); theViewPanel.nextStep(); theViewPanel.repaint(); } else { System.out.println("Already Stop Timer!!"); } } }; //private ViewPanel theViewPanel; // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JToggleButton TimerButton; private javax.swing.ButtonGroup selectedParticle; private javax.swing.JRadioButton theKaonButton; private javax.swing.JRadioButton theNeutronButton; private javax.swing.JPanel theOptionPanel; private javax.swing.JLabel theParticleOption; private javax.swing.JRadioButton thePionButton; private javax.swing.JPanel thePotentialPanel; private javax.swing.JRadioButton theProtonButton; private javax.swing.JButton theQuitButton; private javax.swing.JButton theStartButton; private AsymptoticFreedom.ViewPanel theViewPanel; // End of variables declaration//GEN-END:variables }