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