daniv1

Lab3Ira

May 8th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1. import java.awt.Color;
  2.  
  3. import java.awt.Graphics;
  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6.  
  7. public class Animation {
  8.  
  9.     JFrame frame;            
  10.     Figura figura;
  11.  
  12.     private int oneX = 100;
  13.     private int oneY = 500;
  14.     private int start = 610;
  15.     private int d = 90;
  16.  
  17.     private int dX = 1;
  18.    
  19.     public static void main(String[] args)
  20.     {
  21.         new Animation().go();
  22.     }
  23.  
  24.     private void go()
  25.     {
  26.         frame = new JFrame("Test");
  27.         figura = new Figura();
  28.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  29.         frame.setResizable(false);
  30.         frame.setSize(1900, 1000);
  31.         frame.add(figura);
  32.         frame.setVisible(true);
  33.         moveIt();
  34.        
  35.     }
  36.  
  37.     class Figura extends JPanel
  38.     {    
  39.         public void paintComponent(Graphics g)
  40.         {
  41.        
  42.             int xPoints[] = new int [362];
  43.            
  44.             int yPoints[] = new int[362];
  45.             xPoints[181] = oneX - 50;
  46.             yPoints[181] = oneY;
  47.            
  48.             for(int i = 1; i <= 180 ;  i++ )
  49.                
  50.             {    
  51.        
  52.                 xPoints[i] = (int) (Math.cos(Math.toRadians(i)) * 50) + oneX;        
  53.                 yPoints[i] = (int) (Math.sin(Math.toRadians(i )) * 50) + oneY + 100;                    
  54.  
  55.                
  56.             }
  57.             xPoints[0] = oneX + 50;
  58.             yPoints[0] = oneY + 100;
  59.            
  60. for(int i = 182; i <= 361 ;  i++ )
  61.                
  62.             {    
  63.        
  64.                 xPoints[i] = (int) (Math.cos(Math.toRadians(i + 90)) * 50) + oneX + 100;        
  65.                 yPoints[i] = (int) (Math.sin(Math.toRadians(i + 90)) * 50) + oneY + 50;                    
  66.  
  67.                
  68.             }
  69.             g.setColor(Color.orange);
  70.             g.fillPolygon(xPoints, yPoints, xPoints.length);
  71.         }
  72.     }
  73.  
  74.     private void moveIt()
  75.     {
  76.    
  77.         while (true)
  78.         {
  79.            
  80.  
  81.             d -= dX;
  82.             start += dX;
  83.             if(d > 0) {
  84.             oneX = (int) (Math.cos(Math.toRadians(d)) * 690) ;
  85.             oneY = (int) (Math.sin(Math.toRadians(d)) * 690);
  86.             try
  87.             {
  88.                 Thread.sleep(30);
  89.             }
  90.             catch (Exception e)
  91.             {
  92.                 e.printStackTrace();
  93.             }
  94.             }
  95.             else {
  96.              oneX = start;
  97.              try
  98.              {
  99.                  Thread.sleep(2);
  100.              }
  101.              catch (Exception e)
  102.              {
  103.                  e.printStackTrace();
  104.              }
  105.             }
  106.             if ( start >  1400 || start < 610) {
  107.                 dX = -dX;
  108.             }
  109.            
  110.             frame.repaint();
  111.         }
  112.     }
  113.    
  114.     //тут можна дописати новий метод
  115. }
Add Comment
Please, Sign In to add comment