Advertisement
cd62131

Java Circle Animation

Feb 1st, 2014
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.80 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Canvas;
  3. import java.awt.Frame;
  4. import java.awt.Graphics;
  5. import java.awt.event.WindowAdapter;
  6. import java.awt.event.WindowEvent;
  7.  
  8. public class CircleAnimation {
  9.   public static void main(String[] args) {
  10.     CircleFrame c = new CircleFrame();
  11.     c.setVisible(true);
  12.   }
  13. }
  14.  
  15. class CircleFrame extends Frame {
  16.   private static final long serialVersionUID = 1L;
  17.   private final int width = 800, height = 800;
  18.  
  19.   public CircleFrame() {
  20.     setSize(width, height);
  21.     setTitle("CircleAnimation");
  22.     addWindowListener(new WindowAdapter() {
  23.       @Override
  24.       public void windowClosing(WindowEvent e) {
  25.         System.exit(0);
  26.       }
  27.     });
  28.     Circle canvas = new Circle();
  29.     add(canvas, BorderLayout.CENTER);
  30.   }
  31. }
  32.  
  33. class Circle extends Canvas implements Runnable {
  34.   private static final long serialVersionUID = 1L;
  35.   private final int c1 = 200 , c2 = 50, c3 = 20;
  36.   private final int width = 700, height = 700;
  37.   private double theta1 = 0., theta2 = 0.;
  38.  
  39.   public Circle() {
  40.     setSize(width, height);
  41.     new Thread(this).start();
  42.   }
  43.  
  44.   @Override
  45.   public void paint(Graphics g) {
  46.     g.translate(350, 350);
  47.     g.drawOval(-c1, -c1, 2 * c1, 2 * c1);
  48.     double x1 = (c1 + c2) * Math.cos(theta1);
  49.     double y1 = (c1 + c2) * Math.sin(theta1);
  50.     g.drawOval((int )(x1 - c2), (int )(y1 - c2), 2 * c2, 2 * c2);
  51.     double x2 = x1 + (c2 + c3) * Math.cos(theta2);
  52.     double y2 = y1 + (c2 + c3) * Math.sin(theta2);
  53.     g.drawOval((int )(x2 - c3), (int )(y2 - c3), 2 * c3, 2 * c3);
  54.   }
  55.  
  56.   @Override
  57.   public void run() {
  58.     while (true) {
  59.       theta1 += Math.PI / 180 * 5;
  60.       theta2 -= Math.PI / 180 * 19;
  61.       repaint();
  62.       try {
  63.         Thread.sleep(100);
  64.       }
  65.       catch (Exception e) {
  66.       }
  67.     }
  68.   }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement