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 [5];
- int yPoints[] = new int[5];
- xPoints[0] = oneX;
- yPoints[0] = oneY;
- xPoints[1] = oneX + 25;
- yPoints[1] = oneY - 25;
- xPoints[2] = oneX + 25;
- yPoints[2] = oneY ;
- xPoints[3] = oneX + 50;
- yPoints[3] = oneY;
- xPoints[4] = oneX + 25;
- yPoints[4] = oneY + 25;
- g.setColor(Color.orange);
- g.fillPolygon(xPoints, yPoints, xPoints.length);
- }
- }
- private void moveIt()
- {
- while (true)
- {
- oneX +=dX;
- if ( oneX + dX < 210) {
- oneY = (int) (200 * Math.cos(Math.toRadians((oneX))))+ 200;
- }
- //TODO change magical number
- if (oneX + dX < 0 || oneX + dX > 210) {
- dX = -dX;
- }
- try
- {
- Thread.sleep(1);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- frame.repaint();
- }
- }
- //тут можна дописати новий метод
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement