Thread: Making Pendulum Waves [Java]

Results 1 to 4 of 4
  1. #1 Making Pendulum Waves [Java] 
    The internet's reject
    Andy's Avatar
    Join Date
    Jun 2007
    Age
    33
    Posts
    4,371
    Thanks given
    148
    Thanks received
    1,522
    Rep Power
    3133
    So this is something on reddit I saw and thought I'd make in Java for the hell of it




    Example output

    Attached image

    Code:
    package me.aurous.ui.frames;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    final public class Test {
    
        private int timeStep = 0;
        private final int ballYTravel = 100;
        private final int BALL_NUM = 64;
    
        public static void main(String... args) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Waves");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new DrawPanel());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        class DrawPanel extends JPanel {
    
            private static final long serialVersionUID = 1L;
    
            public DrawPanel() {
            	setBackground(new Color(3, 2, 12));
                new Timer(25, new ActionListener() {//25 seconds should be smooth enough
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        timeStep++;
                        repaint();
                    }
                }).start();
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(1354, 270);
            }
    
            public double getY(int i, int t) {
                return 100 + ballYTravel / 2 * (Math.sin(t * (i / 200d + 0.08)));
            }
    
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                for (int i = 0; i < BALL_NUM; ++i) {
                   g.fillOval(10 + 20 * i, (int) getY(i, timeStep), 12, 12);
                   g.setColor(new Color(29, 82, 112));
                }
    
            }
        }
    }
    It will be a second vislation for Aurous but will move based on Sounds.
    What am I watching now?

    Reply With Quote  
     

  2. Thankful users:


  3. #2  
    ಠ_ಠ

    Joshua's Avatar
    Join Date
    May 2010
    Posts
    1,903
    Thanks given
    397
    Thanks received
    708
    Rep Power
    803
    I love this thing! I too saw it on reddit the other day and was passing it around in the sb here. Thanks for the port.
    Reply With Quote  
     

  4. Thankful user:


  5. #3  
    Extreme Donator

    Jilic-Matt's Avatar
    Join Date
    May 2011
    Age
    29
    Posts
    1,612
    Thanks given
    799
    Thanks received
    494
    Rep Power
    540
    A nice little anim, cheers
    Reply With Quote  
     

  6. #4  
    Banned

    Join Date
    May 2011
    Posts
    1,773
    Thanks given
    854
    Thanks received
    853
    Rep Power
    0
    Pretty cool i thought there was more code to it lol

    I need to get my math together.
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Perfect Fight Caves Waves (Java format)
    By Martin in forum Snippets
    Replies: 19
    Last Post: 01-31-2016, 12:24 AM
  2. Replies: 28
    Last Post: 06-30-2012, 09:02 PM
  3. Replies: 8
    Last Post: 11-17-2007, 05:44 AM
  4. Replies: 12
    Last Post: 08-15-2007, 05:13 PM
  5. Replies: 5
    Last Post: 06-16-2007, 08:57 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •