Question’s all in the title :). I don’t know what’s wrong with my code and why it won’t draw the circle onto the Japplet. Can u help me?
Here’s my code:
import javax.swing.*;
import java.awt.Graphics;
import java.awt.Event;
public class BouncingBall extends JApplet {
private static final long serialVersionUID = 1L;
boolean b = true;
long speed = 50;
int pos = 250;
public void init(){
setSize(500,500);
}
public boolean mouseDown(Event e, int x, int y)
{
if(y>250)
{
speed = speed - 10;
}
else
{
speed = speed + 10;
}
repaint();
return true;
}
public void paintComponents(Graphics g)
{
g.drawOval(250,pos,100,100);
if(speed <= 20)
{
speed++;
repaint();
}
try
{
Thread.sleep(speed);
}
catch(InterruptedException e){e.printStackTrace();}
if(pos>=400)
{
b = false;
}
if(pos<=100)
{
b = true;
}
if(b==true)
{
pos = pos +5;
}
else
{
pos = pos -5;
}
repaint();
}
}
Imulsion
While I prepare my response, please have a read through
Okay. About the only thing you did right, was extend from
JAppletYour “paint” method is a complete mess…
As has already been pointed out, don’t use
mouseDownmethod, use aMouseListenerinsteadAs has already been pointed out, don’t paint on to the top level containers (
JAppletor any type of window or frame), use a custom component instead.Please, make an effort to read through all the above links, they will highlight the problem areas in you code