Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.23 KB | None | 0 0
  1.  
  2. import static java.awt.Color.white;
  3. import java.awt.Graphics;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import javax.swing.JPanel;
  7. import javax.swing.Timer;
  8.  
  9. /*
  10.  * To change this license header, choose License Headers in Project Properties.
  11.  * To change this template file, choose Tools | Templates
  12.  * and open the template in the editor.
  13.  */
  14.  
  15. /**
  16.  *
  17.  * @author janek
  18.  */
  19. public class animacja extends javax.swing.JFrame implements ActionListener {
  20. PanelGraficzny panel;
  21.   Prostokąt[] tp = new Prostokąt[10];  
  22.     Timer t;
  23.     static boolean ruch = false;
  24.     public animacja() {
  25.         initComponents();
  26.         setSize(500,500);
  27.         setBackground( white);
  28.         panel = new PanelGraficzny();
  29.         add(panel);
  30.         panel.setSize(600,400);
  31.        panel.setBounds(20, 20, 450, 200);
  32.        t = new Timer(10, this);
  33. t.start();
  34. for (int i = 0; i < 10; i++) {
  35.  tp[i]=new Prostokąt(0, 0, 0);
  36.     }}
  37.  
  38.     @Override
  39.     public void actionPerformed(ActionEvent e) {
  40.         panel.repaint();
  41.     }
  42. class PanelGraficzny extends JPanel {
  43.     protected void paintComponent(Graphics g) {
  44. g.clearRect(0, 0, getWidth(),getHeight());
  45.  
  46.  for (int i = 0; i < 10; i++) { tp[i].rysuj(g); }
  47.  }
  48.     }
  49.  
  50.  
  51.     /**
  52.      * This method is called from within the constructor to initialize the form.
  53.      * WARNING: Do NOT modify this code. The content of this method is always
  54.      * regenerated by the Form Editor.
  55.      */
  56.     @SuppressWarnings("unchecked")
  57.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  58.     private void initComponents() {
  59.  
  60.         buttonGroup1 = new javax.swing.ButtonGroup();
  61.         jRadioButton1 = new javax.swing.JRadioButton();
  62.         jRadioButton2 = new javax.swing.JRadioButton();
  63.  
  64.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  65.  
  66.         buttonGroup1.add(jRadioButton1);
  67.         jRadioButton1.setText("start");
  68.         jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
  69.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  70.                 jRadioButton1ActionPerformed(evt);
  71.             }
  72.         });
  73.  
  74.         buttonGroup1.add(jRadioButton2);
  75.         jRadioButton2.setText("stop");
  76.         jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
  77.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  78.                 jRadioButton2ActionPerformed(evt);
  79.             }
  80.         });
  81.  
  82.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  83.         getContentPane().setLayout(layout);
  84.         layout.setHorizontalGroup(
  85.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  86.             .addGroup(layout.createSequentialGroup()
  87.                 .addContainerGap()
  88.                 .addComponent(jRadioButton1)
  89.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  90.                 .addComponent(jRadioButton2)
  91.                 .addContainerGap(296, Short.MAX_VALUE))
  92.         );
  93.         layout.setVerticalGroup(
  94.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  95.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  96.                 .addGap(0, 277, Short.MAX_VALUE)
  97.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  98.                     .addComponent(jRadioButton1)
  99.                     .addComponent(jRadioButton2)))
  100.         );
  101.  
  102.         pack();
  103.     }// </editor-fold>//GEN-END:initComponents
  104.  
  105.     private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
  106.      ruch = false;
  107.    // TODO add your handling code here:
  108.     }//GEN-LAST:event_jRadioButton2ActionPerformed
  109.  
  110.     private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
  111.       for (int i = 0; i < 10; i++) {
  112.  int d = panel.getWidth() / 10;
  113.  tp[i] = new Prostokąt(i * d, d, panel.getHeight()); tp[i].start();// nowe wątki
  114.  }
  115.  panel.repaint();
  116.  ruch = true;// TODO add your handling code here:
  117.     }//GEN-LAST:event_jRadioButton1ActionPerformed
  118.  
  119.     /**
  120.      * @param args the command line arguments
  121.      */
  122.     public static void main(String args[]) {
  123.         /* Set the Nimbus look and feel */
  124.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  125.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  126.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  127.          */
  128.         try {
  129.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  130.                 if ("Nimbus".equals(info.getName())) {
  131.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  132.                     break;
  133.                 }
  134.             }
  135.         } catch (ClassNotFoundException ex) {
  136.             java.util.logging.Logger.getLogger(animacja.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  137.         } catch (InstantiationException ex) {
  138.             java.util.logging.Logger.getLogger(animacja.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  139.         } catch (IllegalAccessException ex) {
  140.             java.util.logging.Logger.getLogger(animacja.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  141.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  142.             java.util.logging.Logger.getLogger(animacja.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  143.         }
  144.         //</editor-fold>
  145.  
  146.         /* Create and display the form */
  147.         java.awt.EventQueue.invokeLater(new Runnable() {
  148.             public void run() {
  149.                 new animacja().setVisible(true);
  150.             }
  151.         });
  152.     }
  153.  
  154.     // Variables declaration - do not modify//GEN-BEGIN:variables
  155.     private javax.swing.ButtonGroup buttonGroup1;
  156.     private javax.swing.JRadioButton jRadioButton1;
  157.     private javax.swing.JRadioButton jRadioButton2;
  158.     // End of variables declaration//GEN-END:variables
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement