How to add balls that bounce when clicking

0

I made this program of a ball that bounces but I can not make it come out more

import javax.swing.JPanel;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;

import javax.swing.SpringLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;

public class Ej06Pelota extends JPanel implements MouseListener, 
MouseMotionListener {



private int y, ny;
private int x,  nx;

String mensaje = " ";
Thread  hilo;
/**
 * Create the panel.
 */

public Ej06Pelota() {


    addMouseListener(this);
    addMouseMotionListener(this);
    SpringLayout springLayout = new SpringLayout();
    setLayout(springLayout);

    JButton btnStop = new JButton("STOP");
    btnStop.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {



            hilo.interrupt();


        }
    });
    springLayout.putConstraint(SpringLayout.SOUTH, btnStop, -10, SpringLayout.SOUTH, this);
    springLayout.putConstraint(SpringLayout.EAST, btnStop, -180, SpringLayout.EAST, this);
    add(btnStop);

     hilo = new Thread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    try {
                        while(true) {
                            while(y < getHeight()-30 ) {
                                Thread.sleep(50);
                                y+=10;

                                repaint();
                            }

                            while(y >=1) {
                                Thread.sleep(50);
                                y-=10;
                                repaint();

                            }




                        while(y < getHeight()-30) {
                            Thread.sleep(50);
                            y+=10;
                            repaint();

                        }

                        while(y >= 1) {
                            Thread.sleep(50);
                            y-=10;
                            repaint();
                        }









                        }
                    }catch (Exception ex) {

                    }
                }
            });
}









        @Override
        protected void paintComponent(Graphics d) {

            // TODO Auto-generated method stub


            d.setColor(getBackground());
            d.fillRect(0, 0, getWidth(), getHeight());
            d.setColor(Color.BLACK);
            d.drawString(mensaje,80,80);
            d.setColor(Color.BLUE);
            d.fillOval(x, y, 30, 30);






        }


        @Override
        public void mouseDragged(MouseEvent e) {
            // TODO Auto-generated method stub

        }


        @Override
        public void mouseMoved(MouseEvent e) {
            // TODO Auto-generated method stub

        }


        @Override
        public void mouseClicked(MouseEvent e) {
            // TODO Auto-generated method s tub








        }


        @Override
        public void mouseEntered(MouseEvent e) {
            // TODO Auto-generated method stub

        }


        @Override
        public void mouseExited(MouseEvent e) {
            // TODO Auto-generated method stub

        }


        @Override
        public void mousePressed(MouseEvent e) {
            // TODO Auto-generated method stub
            hilo.start();




        }


        @Override
        public void mouseReleased(MouseEvent e) {
            // TODO Auto-generated method stub

        }

}
    
asked by juan Diaz 22.05.2018 в 02:01
source

0 answers