Advertisement
fabioceep

JAVA: Uma animação Simples

Feb 21st, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.40 KB | None | 0 0
  1. package animacaosimples;
  2. import javax.swing.*;
  3. import java.awt.*;
  4.  
  5. public class AnimacaoSimples {
  6.         int x = 70;
  7.         int y = 70;
  8.  
  9.     public static void main(String[] args) throws InterruptedException{
  10.         AnimacaoSimples sistema = new AnimacaoSimples();
  11.         sistema.mostrar();
  12.        
  13.     }
  14.     public void mostrar() throws InterruptedException{
  15.         JFrame janela = new JFrame();
  16.         janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  17.        
  18.         Painel desenharPainel = new Painel();
  19.        
  20.         janela.getContentPane().add(desenharPainel);
  21.         janela.setSize(300,300);
  22.         janela.setVisible(true);
  23.        
  24.         for (int i = 0; i < 500; i ++){
  25.             x++;
  26.            
  27.             desenharPainel.repaint();
  28.            
  29.             try {
  30.                 Thread.sleep(20);
  31.             } catch(InterruptedException ex){}
  32.            
  33.         }
  34.     }
  35.    
  36.     public class Painel extends JPanel {
  37.         @Override
  38.         public void paintComponent(Graphics g){
  39.             g.setColor(Color.white);
  40.             g.fillRect(0, 0, this.getWidth(), this.getHeight());
  41.      
  42.             for(int i = 0; i < 10 ; i++){
  43.                 g.setColor(Color.red);
  44.                 g.fillOval(x+(i*30), y*2, 20, 20);
  45.                 g.setColor(Color.blue);
  46.                 g.fillOval(x+(i*30), 30+y*2, 20, 20);
  47.             }
  48.  
  49.     }
  50. }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement