Advertisement
daniv1

Untitled

Apr 4th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import javax.swing.JFrame;
  4. import javax.swing.JPanel;
  5.  
  6. public class Animation {
  7.  
  8.     JFrame frame;            
  9.     Figura figura;
  10.  
  11.     private int oneX = 200;
  12.     private int oneY = 200;
  13.  
  14.     private int dX = 2;
  15.     private int dY = 2;
  16.  
  17.     public static void main(String[] args)
  18.     {
  19.         new Animation().go();
  20.     }
  21.  
  22.     private void go()
  23.     {
  24.         frame = new JFrame("Test");
  25.         figura = new Figura();
  26.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27.         frame.setResizable(false);
  28.         frame.setSize(1500, 1000);
  29.         frame.add(figura);
  30.         frame.setVisible(true);
  31.         moveIt();
  32.         //тут можна викликати новий метод, а попередній закоментувати
  33.     }
  34.  
  35.     class Figura extends JPanel
  36.     {    
  37.         public void paintComponent(Graphics g)
  38.         {
  39.           //  g.setColor(Color.BLUE);
  40.           //  g.fillRect(0, 0, this.getWidth(), this.getHeight());
  41.          //   g.setColor(Color.RED);
  42.          //   g.fillRect(3, 3, this.getWidth() - 6, this.getHeight() - 6);
  43.             //   g.setColor(Color.WHITE);
  44.             //  g.fillRect(6, 6, this.getWidth() - 12, this.getHeight() - 12);
  45.               g.setColor(Color.BLACK);
  46.               g.fillOval(oneX, oneY, 30, 30);
  47.               g.setColor(Color.white);
  48.               g.fillOval(oneX+5, oneY+5, 20, 20);
  49.           }
  50.       }
  51.  
  52.       private void moveIt()
  53.       {
  54.           while (true)
  55.           {
  56.            //   oneX = oneX + dX;
  57.               oneY = oneY + dY;
  58.              
  59.               //TODO change magical number
  60.               if (oneX + dX < 0 || oneX + dX > 280) {
  61.                   dX = -dX;
  62.               }
  63.               if (oneY + dY < 0 || oneY + dY > 850) {
  64.                   dY = -dY;
  65.               }
  66.               try
  67.               {
  68.                   Thread.sleep(3);
  69.               }
  70.               catch (Exception e)
  71.               {
  72.                   e.printStackTrace();
  73.               }
  74.               frame.repaint();
  75.           }
  76.       }
  77.      
  78.       //тут можна дописати новий метод
  79.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement