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[] = {oneX , oneX + 50 , oneX + 100 , oneX + 50};
- int yPoints[] = {oneY , oneY -75 , oneY , oneY + 75};
- g.setColor(Color.orange);
- g.fillPolygon(xPoints, yPoints, xPoints.length);
- }
- }
- private void moveIt()
- {
- while (true)
- {
- oneX +=dX;
- 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