Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.*;
- import java.awt.Graphics;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class Animation {
- JFrame frame;
- Figura figura;
- private int oneX = 200;
- private int oneY = 200;
- private int dX = 1;
- private int dY = 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 [360];
- int yPoints[] = new int[360];
- int y = 0;
- for(int i = 0; i < 180 ; i++ )
- {
- xPoints[i] = (int) (Math.cos(Math.toRadians(i + 180 )) * 50) + oneX;
- yPoints[i] = (int) (Math.sin(Math.toRadians(i + 180 )) * 50) + oneY;
- }
- for(int i = 359; i >= 180 ; i-- )
- {
- y++;
- xPoints[i] = (int) (Math.cos(Math.toRadians(y+ 180 )) * 40) + oneX ;
- yPoints[i] = (int) (Math.sin(Math.toRadians(y + 180 )) * 40) + oneY ;
- }
- g.setColor(Color.orange);
- g.fillPolygon(xPoints, yPoints, xPoints.length);
- int xPoints1[] = new int [360];
- int yPoints1[] = new int[360];
- int y1 = 0;
- for(int i = 0; i < 180 ; i++ )
- {
- xPoints1[i] = (int) (Math.cos(Math.toRadians(i )) * 50) + oneX - 40;
- yPoints1[i] = (int) (Math.sin(Math.toRadians(i )) * 50) + oneY - 25;
- }
- for(int i = 359; i >= 180 ; i-- )
- {
- y1++;
- xPoints1[i] = (int) (Math.cos(Math.toRadians(y1)) * 40) + oneX - 40 ;
- yPoints1[i] = (int) (Math.sin(Math.toRadians(y1 )) * 40) + oneY - 25 ;
- }
- g.setColor(Color.orange);
- g.fillPolygon(xPoints1, yPoints1, xPoints1.length);
- }
- }
- private void moveIt()
- {
- while (true)
- {
- oneX +=dX;
- if ( oneX + dX < 700) {
- oneY = (int) (50 * Math.cos(Math.toRadians((oneX +dX))))+ 200;
- }
- //TODO change magical number
- if (oneX + dX >=700 ) {
- oneY = (int) (100 * Math.cos(Math.toRadians((oneX +dX))))+ 200;
- }
- if (oneX + dX < 0 || oneX + dX > 1500) {
- dX = -dX;
- }
- try
- {
- Thread.sleep(1);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- frame.repaint();
- }
- }
- //тут можна дописати новий метод
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement