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 Cycloid; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import org.jfree.chart.JFreeChart; /** * * @author ?? */ public class Firstex extends javax.swing.JApplet { JFreeChart chart; float t; int CircleV; int limit; javax.swing.Timer timer; Boolean isTimerOn = false; public Firstex() { this.t = 0; this.CircleV = 50; this.limit = 50; } /** * Initializes the applet Firstex */ @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(Firstex.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Firstex.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Firstex.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Firstex.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the applet */ try { java.awt.EventQueue.invokeAndWait(new Runnable() { public void run() { initComponents(); } }); } catch (Exception ex) { ex.printStackTrace(); } } /** * 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() { bindingGroup = new org.jdesktop.beansbinding.BindingGroup(); jPanel1 = new javax.swing.JPanel(); ImgViewPanel = new ImageViewPanel(); jPanel4 = new javax.swing.JPanel(); ControlVariable1 = new javax.swing.JLabel(); Co1Spinner = new javax.swing.JSpinner(); Con1Slider = new javax.swing.JSlider(); StartButton = new javax.swing.JButton(); QuitButton = new javax.swing.JButton(); ResetButton = new javax.swing.JButton(); jPanel1.setMaximumSize(new java.awt.Dimension(800, 600)); jPanel1.setMinimumSize(new java.awt.Dimension(800, 600)); jPanel1.setPreferredSize(new java.awt.Dimension(800, 600)); ImgViewPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)); javax.swing.GroupLayout ImgViewPanelLayout = new javax.swing.GroupLayout(ImgViewPanel); ImgViewPanel.setLayout(ImgViewPanelLayout); ImgViewPanelLayout.setHorizontalGroup(ImgViewPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE)); ImgViewPanelLayout.setVerticalGroup(ImgViewPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 386, Short.MAX_VALUE)); jPanel4.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)); ControlVariable1.setText("Radius(20~80)"); Co1Spinner.setModel(new javax.swing.SpinnerNumberModel(50, 30, 80, 1)); org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding( org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, Con1Slider, org.jdesktop.beansbinding.ELProperty.create("${value}"), Co1Spinner, org.jdesktop.beansbinding.BeanProperty.create("value")); bindingGroup.addBinding(binding); Co1Spinner.addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent evt) { Co1SpinnerStateChanged(evt); } }); Con1Slider.setMaximum(80); Con1Slider.setMinimum(20); binding = org.jdesktop.beansbinding.Bindings.createAutoBinding( org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, Co1Spinner, org.jdesktop.beansbinding.ObjectProperty.create(), Con1Slider, org.jdesktop.beansbinding.BeanProperty.create("value")); bindingGroup.addBinding(binding); StartButton.setText("Start"); StartButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { StartButtonActionPerformed(evt); } }); QuitButton.setText("Quit"); QuitButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { QuitButtonActionPerformed(evt); } }); ResetButton.setText("Reset"); ResetButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ResetButtonActionPerformed(evt); } }); javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4); jPanel4.setLayout(jPanel4Layout); jPanel4Layout.setHorizontalGroup(jPanel4Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup() .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup().addGap(74, 74, 74) .addGroup(jPanel4Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(ControlVariable1).addComponent(Co1Spinner, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(jPanel4Layout.createSequentialGroup().addGap(51, 51, 51).addComponent( Con1Slider, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 221, Short.MAX_VALUE) .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(StartButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(QuitButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(30, 30, 30).addComponent(ResetButton, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(88, 88, 88))); jPanel4Layout.setVerticalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup() .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup().addGap(33, 33, 33) .addComponent(ControlVariable1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(10, 10, 10) .addComponent(Co1Spinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addGroup(jPanel4Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(Con1Slider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(QuitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(jPanel4Layout.createSequentialGroup().addGap(65, 65, 65).addComponent( StartButton, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(59, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup() .addGap(0, 0, Short.MAX_VALUE).addComponent(ResetButton, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(87, 87, 87))); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(ImgViewPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(ImgViewPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE))); bindingGroup.bind(); }// </editor-fold>//GEN-END:initComponents private void StartButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StartButtonActionPerformed // TODO add your handling code here: if (isTimerOn) { timerStop(); StartButton.setText("Start"); } else { timerStart(); StartButton.setText("Pause"); } }//GEN-LAST:event_StartButtonActionPerformed private void QuitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_QuitButtonActionPerformed // TODO add your handling code here: setVisible(false); //dispose(); System.exit(0); }//GEN-LAST:event_QuitButtonActionPerformed private void Co1SpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_Co1SpinnerStateChanged int r = (int) Co1Spinner.getValue(); ((ImageViewPanel) ImgViewPanel).setR(r); ((ImageViewPanel) ImgViewPanel).ListClear(); ((ImageViewPanel) ImgViewPanel).repaint(); }//GEN-LAST:event_Co1SpinnerStateChanged private void ResetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ResetButtonActionPerformed timerStop(); StartButton.setText("Start"); t = 0; ((ImageViewPanel) ImgViewPanel).setTime(t); ((ImageViewPanel) ImgViewPanel).ListClear(); ((ImageViewPanel) ImgViewPanel).repaint(); }//GEN-LAST:event_ResetButtonActionPerformed void paintComponent(Graphics circle) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } public class aListener implements ActionListener { public void actionPerformed(ActionEvent e) { //((ImpactViewPanel)ImpactViewPanel).goStep(); repaint(); limit = ((ImageViewPanel) ImgViewPanel).getCircleV(); if (limit < 600) { System.out.println(limit); t = t + 0.15f; ((ImageViewPanel) ImgViewPanel).setTime(t); ((ImageViewPanel) ImgViewPanel).repaint(); } else { timerStop(); StartButton.setText("Start"); } } }; 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; } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JSpinner Co1Spinner; private javax.swing.JSlider Con1Slider; private javax.swing.JLabel ControlVariable1; private javax.swing.JPanel ImgViewPanel; private javax.swing.JButton QuitButton; private javax.swing.JButton ResetButton; private javax.swing.JButton StartButton; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel4; private org.jdesktop.beansbinding.BindingGroup bindingGroup; // End of variables declaration//GEN-END:variables }