Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Graphics;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class Animation {
- JFrame frame;
- Figura figura;
- private int oneX = 100;
- private int oneY = 500;
- private int start = 610;
- private int d = 90;
- private int dX = 1;
- public static void main(String[] args)
- {
- new Animation().go();
- }
- private void go()
- {
- frame = new JFrame("Test");
- figura = new Figura();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setResizable(false);
- frame.setSize(1900, 1000);
- frame.add(figura);
- frame.setVisible(true);
- moveIt();
- }
- class Figura extends JPanel
- {
- public void paintComponent(Graphics g)
- {
- int xPoints[] = new int [362];
- int yPoints[] = new int[362];
- xPoints[181] = oneX - 50;
- yPoints[181] = oneY;
- for(int i = 1; i <= 180 ; i++ )
- {
- xPoints[i] = (int) (Math.cos(Math.toRadians(i)) * 50) + oneX;
- yPoints[i] = (int) (Math.sin(Math.toRadians(i )) * 50) + oneY + 100;
- }
- xPoints[0] = oneX + 50;
- yPoints[0] = oneY + 100;
- for(int i = 182; i <= 361 ; i++ )
- {
- xPoints[i] = (int) (Math.cos(Math.toRadians(i + 90)) * 50) + oneX + 100;
- yPoints[i] = (int) (Math.sin(Math.toRadians(i + 90)) * 50) + oneY + 50;
- }
- g.setColor(Color.orange);
- g.fillPolygon(xPoints, yPoints, xPoints.length);
- }
- }
- private void moveIt()
- {
- while (true)
- {
- d -= dX;
- start += dX;
- if(d > 0) {
- oneX = (int) (Math.cos(Math.toRadians(d)) * 690) ;
- oneY = (int) (Math.sin(Math.toRadians(d)) * 690);
- try
- {
- Thread.sleep(30);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- else {
- oneX = start;
- try
- {
- Thread.sleep(2);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- if ( start > 1400 || start < 610) {
- dX = -dX;
- }
- frame.repaint();
- }
- }
- //тут можна дописати новий метод
- }
Add Comment
Please, Sign In to add comment