I am presently planning to write some code for collision detection. However, I came across a problem. I want to draw multiple spheres on the JFrame window but the following code isn't working... Kindly help me out... This is my code :-
import javax.swing.*;
import java.awt.*;
class Draw extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
for(int i=0;i<20;i++)
drawing(g,new Sphere());
}
public void drawing(Graphics g,Sphere s)
{
g.setColor(s.color);
g.fillOval(s.x,s.y,s.radius*2,s.radius*2);
}
public static void main(String args[])
{
JFrame jf = new JFrame("Renderer");
jf.getContentPane().add(new Draw(),BorderLayout.CENTER);
jf.setBounds(100,100,400,300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
class Sphere
{
int x;
int y;
int radius;
Color color;
public Sphere()
{
this.x = (int)Math.random()*100;
this.y = (int)Math.random()*100;
this.radius = (int)Math.random()*20;
this.color = new Color((int)(Math.random()*255),
(int)(Math.random()*255),(int)(Math.random()*255));
}
}