PDA

View Full Version : Help, Application does not Close!



iitbpd1962
08-25-2008, 07:16 AM
import java.awt.*;
import java.awt.event.*;

public class Balls extends Frame {
Graphics gBuffer;
Image image;
public static void main(String[] args){
Balls f = new Balls();
f.setSize(200,200);
f.addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent e){
System.exit(0);
}
}
);
extracted(f);
}
private static void extracted(Balls f) {
f.show();
}
public void paint(Graphics g){
image = createImage(getSize().width, getSize().height);
gBuffer = image.getGraphics();
gBuffer.setColor(Color.black);
gBuffer.fillRect(10, 10, getSize().width-30, getSize().height-10);
g.drawImage(image, 10, 10, this);
g.setColor(Color.red);
dropBall(g);
}

public void dropBall(Graphics gr){
int i;
while(true){
for(i=20;i<getHeight()-20;i++){
gr.setColor(Color.red);
gr.drawOval(getWidth()/2, i, 10, 10);
Sleep(10);
gr.setColor(Color.black);
gr.drawOval(getWidth()/2, i, 10, 10);
}
for(;i>20;i--){
gr.setColor(Color.red);
gr.drawOval(getWidth()/2, i, 10, 10);
Sleep(10);
gr.setColor(Color.black);
gr.drawOval(getWidth()/2, i, 10, 10);
}
}
}

public void Sleep(long amount){
try{
Thread.sleep(amount);
}
catch(Exception e){}
}

public void update(){
repaint();
}
}

Moshambi
08-25-2008, 02:23 PM
I have used system.exit(); to make my Java programs close before. Just insert in wherever you want it to close the program and it should work. If it oesnt work try putting a 0 or a 1 in the parentheses. Hope that helps!

iitbpd1962
08-29-2008, 05:56 AM
Hi,

Thanks for your quick reply. However, how to detect if user clicks the cross (to close) window so as to use the System.exit() which u suggested?

I have taken to an applet instead. There are problems with the ball collision detection code. Do view it and let me know if there are any (simple) solutions.

http://www.geocities.com/iit_bpd1962/applets/balls.html

nk