Results 1 to 3 of 3

Thread: Help, Application does not Close!

  1. #1
    Join Date
    Aug 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help, Application does not Close!

    Code:
    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();
    	}
    }

  2. #2
    Join Date
    Nov 2007
    Location
    USA
    Posts
    170
    Thanks
    8
    Thanked 22 Times in 22 Posts

    Default

    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!

  3. #3
    Join Date
    Aug 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default System.exit() won't do.

    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •