Advertisement
codecstasy

GAme

May 5th, 2022
863
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.78 KB | None | 0 0
  1.  
  2. package circular.motion;
  3.  
  4. import java.awt.Color;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.RenderingHints;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.KeyEvent;
  11. import static java.awt.event.KeyEvent.VK_SPACE;
  12. import java.awt.event.KeyListener;
  13. import static java.lang.Thread.sleep;
  14. import java.util.logging.Level;
  15. import java.util.logging.Logger;
  16. import javax.swing.JFrame;
  17. import javax.swing.JPanel;
  18.  
  19. public class CircularMotion extends JPanel implements KeyListener, ActionListener{
  20.     double cx = 180;
  21.     double cy = 150;
  22.     double posx1 = 200, posy1 = 300, posx2 = 200, posy2 = 250;
  23.     double rad = 70;
  24.     double angle1 = 90;
  25.     double angle2 = 270;
  26.  
  27.     private void moveBall1() {
  28.         posx1 = cx + rad * Math.cos(angle1 * ((Math.PI)/180));
  29.         posy1 = cy + rad * Math.sin(angle1 * ((Math.PI)/180));
  30.         angle1++;
  31.          
  32.     }
  33.     private void moveBall2() {
  34.         posx2 = cx + rad * Math.cos(angle2 * ((Math.PI)/180));
  35.         posy2 = cy + rad * Math.sin(angle2 * ((Math.PI)/180));
  36.         angle2++;
  37.     }
  38.     @Override
  39.     public void paint(Graphics g) {
  40.         super.paint(g);
  41.         Graphics2D g2d = (Graphics2D) g;
  42.         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  43.                 RenderingHints.VALUE_ANTIALIAS_ON);
  44.         g2d.setColor(Color.RED);
  45.         g2d.fillOval((int)posx1, (int)posy1, 30, 30);//Circle 1
  46.        
  47.         g2d.setColor(Color.BLUE);
  48.         g2d.fillOval((int)posx2, (int)posy2 , 30, 30);
  49.     }
  50.     static CircularMotion game;
  51.     public static void main(String[] args) throws InterruptedException {
  52.         JFrame frame = new JFrame("Circular Motion");
  53.         game = new CircularMotion();
  54.         frame.add(game);
  55.         frame.setSize(400,400);
  56.         frame.setVisible(true);
  57.         frame.setResizable(false);
  58.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  59.        
  60.         int cnt = 0;
  61.         //game.control();
  62.     }
  63.    
  64.     public void control() throws InterruptedException {
  65.         int cnt = 0;
  66.         while(cnt <= 180) {
  67.             moveBall1();
  68.             moveBall2();
  69.             repaint();
  70.             Thread.sleep(5);
  71.             cnt++;
  72.         }
  73.     }
  74.    
  75.     @Override
  76.     public void keyTyped(KeyEvent e) {}
  77.  
  78.     @Override
  79.     public void keyPressed(KeyEvent e) {
  80.         if(e.getKeyCode() == KeyEvent.VK_SPACE) {
  81.             try {
  82.                 game.control();
  83.             } catch (InterruptedException ex) {
  84.                 Logger.getLogger(CircularMotion.class.getName()).log(Level.SEVERE, null, ex);
  85.             }
  86.         }
  87.     }
  88.  
  89.     @Override
  90.     public void keyReleased(KeyEvent e) {}
  91.  
  92.     @Override
  93.     public void actionPerformed(ActionEvent e) {}
  94.    
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement