Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Graphics;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JSlider;
- import javax.swing.UIManager;
- @SuppressWarnings("serial")
- public class Principal extends JPanel{
- public static final int WIDTH = 500;
- public static final int HEIGHT = 500;
- private static JFrame janela = new JFrame("Movimentação controlada!");
- private static JFrame controlWindow = new JFrame();
- private Ball ball = new Ball(50, 50);
- private JSlider sliderX = new JSlider();
- private JSlider sliderY = new JSlider();
- JLabel textX = new JLabel("X: ");
- JLabel textY = new JLabel("Y: ");
- // ---------------------------------------------------------------------- //
- private void sliderSensor()
- {
- ball.setSpeedX(sliderX.getValue());
- ball.setSpeedY(sliderY.getValue());
- }
- class Ball
- {
- private int x;
- private int y;
- private int sentido_1 = 2; // Baixo = 1 // Cima = 2
- private int sentido_2 = 1; // Esquerda = 1 // Direita = 2
- private int speedX = 5;
- private int speedY = 5;
- Thread movimento = new Thread(new Runnable() {
- @Override
- public void run() {
- while(true)
- {
- try
- {
- Thread.sleep(50); // Intervalo de 50ms.
- controlWindow.setTitle("X: "+x+" Y: "+y+" | X-SPEED: "+speedX+" Y-SPEED: "+speedY);
- }
- catch(InterruptedException e)
- {
- e.printStackTrace();
- }
- // --------------------------------------------------------------- //
- if(sentido_1 == 1)
- {
- y += speedY;
- }
- else if(sentido_1 == 2)
- {
- y -= speedY;
- }
- if(sentido_2 == 1)
- {
- x -= speedX;
- }
- else if(sentido_2 == 2)
- {
- x += speedX;
- }
- // --------------------------------------------------------------- //
- if(x < 0) sentido_2 = 2;
- else if(x >= WIDTH - 30) sentido_2 = 1;
- if(y < 0) sentido_1 = 1;
- else if(y >= HEIGHT - 50) sentido_1 = 2;
- }
- }
- });
- public Ball(int x, int y) {
- this.x = x;
- this.y = y;
- movimento.start(); // Startando o thread
- }
- public void setX(int x) {
- this.x = x;
- }
- public void setY(int y) {
- this.y = y;
- }
- public int getX() {
- return x;
- }
- public int getY() {
- return y;
- }
- public void setSpeedX(int speedX) {
- this.speedX = speedX;
- }
- public void setSpeedY(int speedY) {
- this.speedY = speedY;
- }
- }
- public Principal() // Construtor padrão
- {
- this.setLayout(null);
- JPanel painelDeControle = new JPanel();
- painelDeControle.setLayout(null);
- painelDeControle.add(sliderX);
- painelDeControle.add(sliderY);
- painelDeControle.add(textX);
- painelDeControle.add(textY);
- sliderX.setBounds(100, 0, 100, 20);
- sliderX.setValue(1);
- sliderX.setMaximum(30);
- sliderY.setBounds(100, 50, 100, 20);
- sliderY.setValue(1);
- sliderY.setMaximum(30);
- textX.setBounds(50, 0, 100, 20);
- textY.setBounds(50, 50, 100, 20);
- controlWindow.add(painelDeControle);
- controlWindow.setVisible(true);
- controlWindow.setSize(350, 200);
- controlWindow.setAlwaysOnTop(true);
- controlWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
- controlWindow.setResizable(false);
- }
- public static void main(String[] args)
- {
- try
- {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- janela.add(new Principal());
- janela.setVisible(true);
- janela.setResizable(false);
- janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- janela.setSize(WIDTH, HEIGHT);
- janela.setLocationRelativeTo(null);
- }
- @Override
- public void paint(Graphics g) {
- super.paint(g);
- g.drawOval(ball.getX(), ball.getY(), 20, 20); // x, y, largura, altura.
- sliderSensor();
- repaint();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement