Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Canvas;
- import java.awt.Frame;
- import java.awt.Graphics;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- public class CircleAnimation {
- public static void main(String[] args) {
- CircleFrame c = new CircleFrame();
- c.setVisible(true);
- }
- }
- class CircleFrame extends Frame {
- private static final long serialVersionUID = 1L;
- private final int width = 800, height = 800;
- public CircleFrame() {
- setSize(width, height);
- setTitle("CircleAnimation");
- addWindowListener(new WindowAdapter() {
- @Override
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- Circle canvas = new Circle();
- add(canvas, BorderLayout.CENTER);
- }
- }
- class Circle extends Canvas implements Runnable {
- private static final long serialVersionUID = 1L;
- private final int c1 = 200 , c2 = 50, c3 = 20;
- private final int width = 700, height = 700;
- private double theta1 = 0., theta2 = 0.;
- public Circle() {
- setSize(width, height);
- new Thread(this).start();
- }
- @Override
- public void paint(Graphics g) {
- g.translate(350, 350);
- g.drawOval(-c1, -c1, 2 * c1, 2 * c1);
- double x1 = (c1 + c2) * Math.cos(theta1);
- double y1 = (c1 + c2) * Math.sin(theta1);
- g.drawOval((int )(x1 - c2), (int )(y1 - c2), 2 * c2, 2 * c2);
- double x2 = x1 + (c2 + c3) * Math.cos(theta2);
- double y2 = y1 + (c2 + c3) * Math.sin(theta2);
- g.drawOval((int )(x2 - c3), (int )(y2 - c3), 2 * c3, 2 * c3);
- }
- @Override
- public void run() {
- while (true) {
- theta1 += Math.PI / 180 * 5;
- theta2 -= Math.PI / 180 * 19;
- repaint();
- try {
- Thread.sleep(100);
- }
- catch (Exception e) {
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement