I want my Jpanel to do a bouncing ball. So I decided to use a thread to update the coordinates regularly. How do I set the panel to visible and at the same time run run()? As you will see, it doesn't work (Code for drawing I haven't placed yet. Instead I placed System.out.println("hello"); to see if it runs):
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
public class bouncingBall extends JPanel implements Runnable
{
private int balls;
private int ballCount;
public bouncingBall()
{
setSize(500,500);
addMouseListener(
new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
}
);
ExecutorService threadExecutor = Executors.newFixedThreadPool( 1 );
threadExecutor.execute( Thread.currentThread() );
}
public void run()
{
while(true)
{
System.out.println("Hello");
}
}
public void paintComponent(Graphics g)
{
}
public static void main(String args[])
{
JFrame cage = new JFrame("Bouncing Balls");
bouncingBall app = new bouncingBall();
cage.setSize(app.getSize());
cage.add(app);
cage.setVisible(true);
}