Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package circular.motion;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.RenderingHints;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import static java.awt.event.KeyEvent.VK_SPACE;
- import java.awt.event.KeyListener;
- import static java.lang.Thread.sleep;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class CircularMotion extends JPanel implements KeyListener, ActionListener{
- double cx = 180;
- double cy = 150;
- double posx1 = 200, posy1 = 300, posx2 = 200, posy2 = 250;
- double rad = 70;
- double angle1 = 90;
- double angle2 = 270;
- private void moveBall1() {
- posx1 = cx + rad * Math.cos(angle1 * ((Math.PI)/180));
- posy1 = cy + rad * Math.sin(angle1 * ((Math.PI)/180));
- angle1++;
- }
- private void moveBall2() {
- posx2 = cx + rad * Math.cos(angle2 * ((Math.PI)/180));
- posy2 = cy + rad * Math.sin(angle2 * ((Math.PI)/180));
- angle2++;
- }
- @Override
- public void paint(Graphics g) {
- super.paint(g);
- Graphics2D g2d = (Graphics2D) g;
- g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON);
- g2d.setColor(Color.RED);
- g2d.fillOval((int)posx1, (int)posy1, 30, 30);//Circle 1
- g2d.setColor(Color.BLUE);
- g2d.fillOval((int)posx2, (int)posy2 , 30, 30);
- }
- static CircularMotion game;
- public static void main(String[] args) throws InterruptedException {
- JFrame frame = new JFrame("Circular Motion");
- game = new CircularMotion();
- frame.add(game);
- frame.setSize(400,400);
- frame.setVisible(true);
- frame.setResizable(false);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- int cnt = 0;
- //game.control();
- }
- public void control() throws InterruptedException {
- int cnt = 0;
- while(cnt <= 180) {
- moveBall1();
- moveBall2();
- repaint();
- Thread.sleep(5);
- cnt++;
- }
- }
- @Override
- public void keyTyped(KeyEvent e) {}
- @Override
- public void keyPressed(KeyEvent e) {
- if(e.getKeyCode() == KeyEvent.VK_SPACE) {
- try {
- game.control();
- } catch (InterruptedException ex) {
- Logger.getLogger(CircularMotion.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- @Override
- public void keyReleased(KeyEvent e) {}
- @Override
- public void actionPerformed(ActionEvent e) {}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement