Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.88 KB | None | 0 0
  1.  
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.MouseEvent;
  7. import java.awt.event.MouseListener;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Random;
  11. import javax.swing.JPanel;
  12. import javax.swing.Timer;
  13.  
  14. /*
  15.  * To change this license header, choose License Headers in Project Properties.
  16.  * To change this template file, choose Tools | Templates
  17.  * and open the template in the editor.
  18.  */
  19.  
  20. /**
  21.  *
  22.  * @author Klaudia
  23.  */
  24. public class Animacja extends javax.swing.JFrame implements ActionListener {
  25.  
  26.     Random r = new Random();
  27.     final int X = 500, Y = 500;
  28.     PanelGraficzny panel;
  29.     Timer t;
  30.     double d;
  31.     double dm;
  32.     List<Obiekt> LW = new ArrayList<>();
  33.     static boolean ruch = true;
  34.  
  35.     public Animacja() {
  36.         initComponents();
  37.         setSize(550, 550);
  38.         panel = new PanelGraficzny();
  39.         panel.setBounds(25, 25, X, Y);
  40.         add(panel);
  41.         t = new Timer(10, this);
  42.         t.start();
  43.     }
  44.  
  45.  
  46.     public void actionPerformed(ActionEvent ae) {
  47.         panel.repaint();
  48.     }
  49.  
  50.     class PanelGraficzny extends JPanel implements MouseListener {
  51.  
  52.         public PanelGraficzny() {
  53.             addMouseListener(this);
  54.         }
  55.  
  56.         protected void paintComponent(Graphics g) {
  57.             g.clearRect(0, 0, X, Y);
  58.             for (Obiekt o : LW) {
  59.                 //d=r.nextInt(70)+45;
  60.                 //dm = (1/3)*d;
  61.                 o.rysuj(g);
  62.             }
  63.         }
  64.  
  65.         @Override
  66.         public void mouseClicked(MouseEvent me) {
  67.  
  68.         }
  69.  
  70.         @Override
  71.         public void mousePressed(MouseEvent e) {
  72.             if (e.isAltDown() || e.isShiftDown()) {
  73.                 ruch = false;
  74.                 return;
  75.             }
  76.             int x0 = e.getX();
  77.             int y0 = e.getY();
  78.             d = r.nextInt(70) + 45;
  79.             dm = 0.3333333 * d;
  80.             Color c = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
  81.             Obiekt o = new Obiekt(x0, y0, d, dm);
  82.             LW.add(o);
  83.             o.start();
  84.             ruch = true;
  85.             panel.repaint();
  86.         }
  87.  
  88.         @Override
  89.         public void mouseReleased(MouseEvent me) {
  90.  
  91.         }
  92.  
  93.         @Override
  94.         public void mouseEntered(MouseEvent me) {
  95.  
  96.         }
  97.  
  98.         @Override
  99.         public void mouseExited(MouseEvent me) {
  100.  
  101.         }
  102.     }
  103.  
  104.     public class Obiekt extends Thread {
  105.  
  106.         double d1, d2;
  107.         Color k, k2;
  108.  
  109.         Random r = new Random();
  110.         int dt;
  111.         int x, y;
  112.  
  113.         public Obiekt(int x, int y, double d1, double d2) {
  114.             this.x = x;
  115.             this.y = y;
  116.             this.d1 = d1;
  117.             this.d2 = d2;
  118.  
  119.             k = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)); // losowy kolor trojkata
  120.             k2 = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
  121.             dt = 550 + r.nextInt(900);
  122.         }
  123.  
  124.         void rysuj(Graphics g) { // x0, y0 - współrzędne punktu zaczepienia wahadła
  125.             g.setColor(k);
  126.             //int x = x0 + (int) (d1 * Math.sin(alfa));
  127.             //int y = y0 + (int) (d2 * Math.cos(alfa));
  128.             // g.drawLine(x0, y0, x, y);
  129.             int dx = (int) d1;
  130.             int dy = (int) d2;
  131.             g.fillOval(x, y, dx, dx);
  132.  
  133.             g.setColor(k2);
  134.             g.fillOval(x + dy, y + dy, dy, dy);
  135.         }
  136.  
  137.         public void run() {
  138.             while (true) {
  139.                 if (Animacja.ruch) {
  140.                     //d1=r.nextInt(70)+45;
  141.                     //d2=(1/3)*d1;
  142.                     k = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)); // losowy kolor trojkata
  143.                     k2 = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
  144.                 }
  145.                 try {
  146.                     Thread.sleep(dt); //uśpienie wątku na 5 milisekund
  147.                 } catch (InterruptedException e) {
  148.                 }
  149.             }
  150.         }
  151.  
  152.     }
  153.  
  154.     /**
  155.      * This method is called from within the constructor to initialize the form.
  156.      * WARNING: Do NOT modify this code. The content of this method is always
  157.      * regenerated by the Form Editor.
  158.      */
  159.     @SuppressWarnings("unchecked")
  160.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  161.     private void initComponents() {
  162.  
  163.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  164.  
  165.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  166.         getContentPane().setLayout(layout);
  167.         layout.setHorizontalGroup(
  168.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  169.             .addGap(0, 400, Short.MAX_VALUE)
  170.         );
  171.         layout.setVerticalGroup(
  172.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  173.             .addGap(0, 300, Short.MAX_VALUE)
  174.         );
  175.  
  176.         pack();
  177.     }// </editor-fold>//GEN-END:initComponents
  178.  
  179.     /**
  180.      * @param args the command line arguments
  181.      */
  182.     public static void main(String args[]) {
  183.         /* Set the Nimbus look and feel */
  184.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  185.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  186.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  187.          */
  188.         try {
  189.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  190.                 if ("Nimbus".equals(info.getName())) {
  191.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  192.                     break;
  193.                 }
  194.             }
  195.         } catch (ClassNotFoundException ex) {
  196.             java.util.logging.Logger.getLogger(Animacja.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  197.         } catch (InstantiationException ex) {
  198.             java.util.logging.Logger.getLogger(Animacja.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  199.         } catch (IllegalAccessException ex) {
  200.             java.util.logging.Logger.getLogger(Animacja.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  201.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  202.             java.util.logging.Logger.getLogger(Animacja.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  203.         }
  204.         //</editor-fold>
  205.  
  206.         /* Create and display the form */
  207.         java.awt.EventQueue.invokeLater(new Runnable() {
  208.             public void run() {
  209.                 new Animacja().setVisible(true);
  210.             }
  211.         });
  212.     }
  213.  
  214.     // Variables declaration - do not modify//GEN-BEGIN:variables
  215.     // End of variables declaration//GEN-END:variables
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement