Advertisement
fel486

Movimentação controlada por Slider e Thread!

Mar 24th, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.96 KB | None | 0 0
  1. import java.awt.Graphics;
  2.  
  3. import javax.swing.JFrame;
  4. import javax.swing.JLabel;
  5. import javax.swing.JPanel;
  6. import javax.swing.JSlider;
  7. import javax.swing.UIManager;
  8.  
  9. @SuppressWarnings("serial")
  10. public class Principal extends JPanel{
  11.    
  12.     public static final int WIDTH = 500;
  13.     public static final int HEIGHT = 500;
  14.    
  15.     private static JFrame janela = new JFrame("Movimentação controlada!");
  16.     private static JFrame controlWindow = new JFrame();
  17.    
  18.     private Ball ball = new Ball(50, 50);
  19.     private JSlider sliderX = new JSlider();
  20.     private JSlider sliderY = new JSlider();
  21.    
  22.     JLabel textX = new JLabel("X: ");
  23.     JLabel textY = new JLabel("Y: ");
  24.    
  25.     // ---------------------------------------------------------------------- //
  26.    
  27.     private void sliderSensor()
  28.     {
  29.         ball.setSpeedX(sliderX.getValue());
  30.         ball.setSpeedY(sliderY.getValue());
  31.     }
  32.    
  33.     class Ball
  34.     {
  35.         private int x;
  36.         private int y;
  37.        
  38.         private int sentido_1 = 2; // Baixo = 1 // Cima = 2
  39.         private int sentido_2 = 1; // Esquerda = 1 // Direita = 2
  40.        
  41.         private int speedX = 5;
  42.         private int speedY = 5;
  43.        
  44.         Thread movimento = new Thread(new Runnable() {
  45.            
  46.             @Override
  47.             public void run() {
  48.                
  49.                 while(true)
  50.                 {
  51.                     try
  52.                     {
  53.                         Thread.sleep(50); // Intervalo de 50ms.
  54.                         controlWindow.setTitle("X: "+x+" Y: "+y+" | X-SPEED: "+speedX+" Y-SPEED: "+speedY);
  55.                     }
  56.                     catch(InterruptedException e)
  57.                     {
  58.                         e.printStackTrace();
  59.                     }
  60.                    
  61.                     // --------------------------------------------------------------- //
  62.                    
  63.                     if(sentido_1 == 1)
  64.                     {
  65.                         y += speedY;
  66.                     }
  67.                    
  68.                     else if(sentido_1 == 2)
  69.                     {
  70.                         y -= speedY;
  71.                     }
  72.                    
  73.                     if(sentido_2 == 1)
  74.                     {
  75.                         x -= speedX;
  76.                     }
  77.                    
  78.                     else if(sentido_2 == 2)
  79.                     {
  80.                         x += speedX;
  81.                     }
  82.                    
  83.                     // --------------------------------------------------------------- //
  84.                    
  85.                     if(x < 0) sentido_2 = 2;
  86.                     else if(x >= WIDTH - 30) sentido_2 = 1;
  87.                    
  88.                     if(y < 0) sentido_1 = 1;
  89.                     else if(y >= HEIGHT - 50) sentido_1 = 2;
  90.                    
  91.                 }
  92.                
  93.             }
  94.         });
  95.        
  96.         public Ball(int x, int y) {
  97.             this.x = x;
  98.             this.y = y;
  99.            
  100.             movimento.start(); // Startando o thread
  101.         }
  102.        
  103.         public void setX(int x) {
  104.             this.x = x;
  105.         }
  106.        
  107.         public void setY(int y) {
  108.             this.y = y;
  109.         }
  110.        
  111.         public int getX() {
  112.             return x;
  113.         }
  114.        
  115.         public int getY() {
  116.             return y;
  117.         }
  118.        
  119.         public void setSpeedX(int speedX) {
  120.             this.speedX = speedX;
  121.         }
  122.        
  123.         public void setSpeedY(int speedY) {
  124.             this.speedY = speedY;
  125.         }
  126.     }
  127.    
  128.     public Principal() // Construtor padrão
  129.     {
  130.         this.setLayout(null);
  131.        
  132.         JPanel painelDeControle = new JPanel();
  133.        
  134.         painelDeControle.setLayout(null);
  135.         painelDeControle.add(sliderX);
  136.         painelDeControle.add(sliderY);
  137.        
  138.         painelDeControle.add(textX);
  139.         painelDeControle.add(textY);
  140.        
  141.         sliderX.setBounds(100, 0, 100, 20);
  142.         sliderX.setValue(1);
  143.         sliderX.setMaximum(30);
  144.        
  145.         sliderY.setBounds(100, 50, 100, 20);
  146.         sliderY.setValue(1);
  147.         sliderY.setMaximum(30);
  148.        
  149.         textX.setBounds(50, 0, 100, 20);
  150.         textY.setBounds(50, 50, 100, 20);
  151.        
  152.         controlWindow.add(painelDeControle);
  153.         controlWindow.setVisible(true);
  154.         controlWindow.setSize(350, 200);
  155.         controlWindow.setAlwaysOnTop(true);
  156.         controlWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  157.         controlWindow.setResizable(false);
  158.        
  159.        
  160.     }
  161.    
  162.     public static void main(String[] args)
  163.     {
  164.         try
  165.         {
  166.             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  167.         }
  168.         catch(Exception e)
  169.         {
  170.             e.printStackTrace();
  171.         }
  172.        
  173.         janela.add(new Principal());
  174.         janela.setVisible(true);
  175.         janela.setResizable(false);
  176.         janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  177.         janela.setSize(WIDTH, HEIGHT);
  178.         janela.setLocationRelativeTo(null);
  179.    
  180.     }
  181.    
  182.     @Override
  183.     public void paint(Graphics g) {
  184.         super.paint(g);
  185.        
  186.         g.drawOval(ball.getX(), ball.getY(), 20, 20); // x, y, largura, altura.
  187.         sliderSensor();
  188.    
  189.         repaint();
  190.     }
  191.  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement