Advertisement

05.08.2008 at 03:32AM PDT, ID: 23385537
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Locked "close window" icon
Tags: Sun, Java, JDK 6, Java JDK 6
I wrote a java code to draw a circle moving inside a frame. If I run the following code (in which I commented out the motion routine) it works fine: I can close the application by clicking on the "close window" icon. Instead, when I remove the comment, the application is locked and I'm unable to close the window (so to interrupt the motion routine). What should I add/remove from my code so that the "close window" icon can be clicked even when the circle is moving?

Thanks

Stefano

==========================================0

/** AniMain.java - Java versione 1.6 - 05 05 2008 */
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.*;

class Circle {
    int x;
    int y;
    int radius;
    Graphics g;
   
    public Circle(int x, int y, int radius, Graphics g) {
        this.x = x;
        this.y = y;
        this.radius = radius;
        this.g = g;
    }
   
    void draw() {
        g.setColor(Color.RED);
        g.drawOval(x-radius,y-radius,2*radius,2*radius);
    }
     
    void delete() {
        g.setColor(Color.white);
        g.drawOval(x-radius,y-radius,2*radius,2*radius);
    }
   
    void move(int newx, int newy) {
        delete();
        x = newx;
        y = newy;
        draw();
    }
}

//end the program when the user hits the Frame's close button
class Window extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
        System.exit(1);
    }
}

public class AniMain extends Frame {
    public void paint(Graphics g) {
        int i;
        Circle circle1 = new Circle(50, 60, 100, g);
        circle1.draw();
       
        /* //uncomment this block to lock "close window" icon
         
        i = 0;
        while(true) {
            if (i > 300)
                i = 0;
            else
                i++;

            circle1.move(50+i, 60+i);
            try {Thread.sleep(50);}
            catch(Exception e) {}
        }*/
    }

    public static void main(String args[]) {
        Frame frame = new AniMain();
        frame.setBounds(100,100,400,400);
        frame.setBackground(Color.white);
        frame.setResizable(false);
        //register event
        frame.addWindowListener(new Window());

        frame.setVisible(true);

    }
}
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: s_federici
Solution Provided By: CEHJ
Participating Experts: 3
Solution Grade: A
Views: 4
Translate:
Loading Advertisement...
05.08.2008 at 03:37AM PDT, ID: 21523582

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.08.2008 at 04:11AM PDT, ID: 21523722

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.08.2008 at 04:13AM PDT, ID: 21523730

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.08.2008 at 07:23AM PDT, ID: 21524971

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.08.2008 at 07:44AM PDT, ID: 21525171

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.08.2008 at 01:35PM PDT, ID: 21528228

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 04:00AM PDT, ID: 21531621

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 07:25AM PDT, ID: 21533129

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.13.2008 at 10:38AM PDT, ID: 21557373

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.13.2008 at 11:03AM PDT, ID: 21557595

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.13.2008 at 11:04AM PDT, ID: 21557607

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.13.2008 at 11:18AM PDT, ID: 21557738

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.13.2008 at 12:12PM PDT, ID: 21558290

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.13.2008 at 12:14PM PDT, ID: 21558323

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.13.2008 at 12:21PM PDT, ID: 21558397

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.13.2008 at 01:00PM PDT, ID: 21558742

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.13.2008 at 05:29PM PDT, ID: 21560375

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.15.2008 at 02:53PM PDT, ID: 21578284

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.16.2008 at 02:11AM PDT, ID: 21581098

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.16.2008 at 06:59AM PDT, ID: 21582691

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
05.08.2008 at 03:37AM PDT, ID: 21523582

Rank: Guru

Run it in a separate thread.
 
05.08.2008 at 04:11AM PDT, ID: 21523722

Rank: Genius

Remove the commented code from the paint() method and instad invoke it using a Swing Timer

http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html
 
05.08.2008 at 04:13AM PDT, ID: 21523730

Rank: Genius

and remove the loop, and instead have youe timer invoked repeatedly executing:

            if (i > 300)
                i = 0;
            else
                i++;

            circle1.move(50+i, 60+i);
            repaint();

You'll need to make i a member variable

>         Circle circle1 = new Circle(50, 60, 100, g);

also make your circle a member var

Let me know if you need a hand.

 
05.08.2008 at 07:23AM PDT, ID: 21524971
I'm really astonished that there is a no way out of this other thatn using threads or timers. I programmed in a lot of different languages and interacting with the "close window" icon has never been so difficult. Is this due to the fact that I used the paint method of the frame object? Is there a cleaner way that will replace the paint/main methods of my code without having to use timers or threads? Thanks for your help.

If threads or timers are the only solution I need a clear explanation as the web page suggested wasn't very explanatory to me.
 
05.08.2008 at 07:44AM PDT, ID: 21525171

Rank: Genius

problem is caused by swing being single threaded. You block that thread so all gui processing is blocked.
Swing timer will allow you to periodicly have code executed by that thread, just make sure the code executes and returns quickly.

 
05.08.2008 at 01:35PM PDT, ID: 21528228

Rank: Guru

Because of what has been said above (effectively the event dispatch thread beng used up by the hogging process), you'll get nowhere unless you implement a timer, or spawn a separate thread. You're right - it's very -very - annoying if you don't know about it. (Think of it as the equivalent of needing doEvents() in VB (if you are familiar with that at all)). It's one of the fewish things that can irk you about Java, but you've got to bite the ole bullet, and take the road that has been suggested.

 
05.09.2008 at 04:00AM PDT, ID: 21531621

Rank: Guru

You may like to read this and its associated pages :

http://www.javaworld.com/javaworld/jw-06-2003/jw-0606-swingworker.html?page=2

from which comes:

"This artificial delay demonstrates the negative effect of executing a time-consuming task, which can be a complex SQL statement execution in a more realistic situation. Remember the thread that becomes dormant is the event-dispatch thread, which is also responsible for dispatching painting events. Therefore, when the user makes a new table selection, the user interface ceases to properly repaint itself during the delay. The application will display bizarre effects when the user resizes the window during the delay, as seen in Figure 3 below. During this problematic delay, the user can also queue up additional events by clicking on the unresponsive screen, potentially causing further unintended consequences. Feel free to verify these problems by running the demo application in delay mode. "

Swingworker is probably what you need.
 
05.09.2008 at 07:25AM PDT, ID: 21533129
Ok, whatever you think is the simplest solution (thread or timer) I need to ask you for the modified version of my code that will react to the "close window" event. Thanks for your help.
 
05.13.2008 at 10:38AM PDT, ID: 21557373
Is there no one able to modify my code so that it will have the desired behaviour(with threads or timers)?
 
05.13.2008 at 11:03AM PDT, ID: 21557595

Rank: Guru

You could have a shot yourself. Just remember 1) loops need relief; like a Thread.sleep(); (open Task Manager to see the CPU burn without such relief at up to 100%; 2) you may be able to use a Daemon thread for the drawing, that must end by definition once the main class is dropped; 3) you may as well use a timer as iobject said, because you are effectively using one already when moving the circle. It's not that much more hassle enveloping that in a thread.
 
05.13.2008 at 11:04AM PDT, ID: 21557607

Rank: Guru

iobject = objects. ;)
 
05.13.2008 at 11:18AM PDT, ID: 21557738
If I had either known how to do it or had time to study this subject I wouldn't have asked :-)
 
05.13.2008 at 12:12PM PDT, ID: 21558290

Rank: Guru

I can see that. But y'know, you'd have a 'right' to be even *more* surprised if you were to find that the screen *did* die when you asked it to, when, in fact, all the time you'd just asked it to loop until it drooped, - wouldn't you?  ;) (The logic *is* in there, but its not explicit, and that's the problem.

You've got the options, and personally, I'd take up objects' offer to lend a hand on this one. :)



 
05.13.2008 at 12:14PM PDT, ID: 21558323

Rank: Guru

>> (The logic *is* in there ....

... by which of course I meant to say the logic of only having asked your code to do what you'd instructed. ;) Loop.
 
05.13.2008 at 12:21PM PDT, ID: 21558397

Rank: Guru

>>If I had either known how to do it or had time to study this subject I wouldn't have asked :-) <<

You came perilously close to succeeding, and your code, otherwise, was very nice. ;) CU later.
 
05.13.2008 at 01:00PM PDT, ID: 21558742

Rank: Genius

You need to do a bit of rearranging. But the following works:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
public class AniMain extends Frame implements Runnable {
	private Circle circle1;
 
	public AniMain() {
		circle1 = new Circle(50, 60, 100);
		addWindowListener(new WindowCloser());
		new Thread(this).start();
	}
 
	public void paint(Graphics g) {
		circle1.draw(g);
	}
 
	public static void main(String args[]) {
		Frame frame = new AniMain();
		frame.setBounds(100, 100, 400, 400);
		frame.setBackground(Color.white);
		frame.setResizable(false);
		// register event
		frame.setVisible(true);
 
	}
 
	// end the program when the user hits the Frame's close button
	private class WindowCloser extends WindowAdapter {
		public void windowClosing(WindowEvent e) {
			System.exit(1);
		}
	}
 
	public void run() {
		int i;		
		i = 0;
		while (true) {
			if (i > 300)
				i = 0;
			else
				i++;
 
			circle1.move(50 + i, 60 + i);
			try {
				Thread.sleep(50);
				repaint();
			} catch (Exception e) {
			}
		}
	}
 
}
 
//=============================================================
 
/** AniMain.java - Java versione 1.6 - 05 05 2008 */
import java.awt.Color;
import java.awt.Graphics;
 
class Circle {
	int x;
 
	int y;
 
	int radius;
 
	public Circle(int x, int y, int radius) {
		this.x = x;
		this.y = y;
		this.radius = radius;
	}
 
	void draw(Graphics graphics) {
		graphics.setColor(Color.RED);
		graphics.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);
	}
 
	void delete(Graphics graphics) {
		graphics.setColor(Color.white);
		graphics.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);
	}
 
	void move(int newx, int newy) {
		x = newx;
		y = newy;
	}
 
}
Open in New Window
Accepted Solution
 
05.13.2008 at 05:29PM PDT, ID: 21560375

Rank: Genius

I already explained above what you needed to change, which part did you not understand?
 
05.15.2008 at 02:53PM PDT, ID: 21578284
CEHJ,
I reshaped your code so that make it closer to my original code. I moved the windowCloser class back outside the main class and put the addwindowlistener back to the main method. I tested the code and verified that it does still work. Then I compared your reshaped code and my original code. What I think I understood is that the problem was that the paint method cannot be interrupted (is this true?) so what you did was:

- to move the init commands to the class constructor
- to remove the endless loop from the paint method and put in the run method (that can be interrupted)
- "new Thread(this).start();" starts the "run" method
- as the init section and the run method don't know about the Graphics object, the Circle class constructor lost the Graphics argument and, viceversa, the draw and delete methods got the new Graphics argument
- as the run method doesn't know about the Graphics object, the move method lost the draw/delete commands (that are still in the paint method)

I guess that delete is useless know as, if I correctly understand now how they work, repaint clears every time the drawing area.

Could you please confirm my guesses? Thanks a lot.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
import java.awt.*;
import java.awt.event.*;
 
class Circle {
	int x;
 	int y;
 	int radius;
 
	public Circle(int x, int y, int radius) {
		this.x = x;
		this.y = y;
		this.radius = radius;
	}
 
	void draw(Graphics g) {
		g.setColor(Color.RED);
		g.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);
	}
 
	void delete(Graphics g) {
		g.setColor(Color.white);
		g.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);
	}
 
	void move(int newx, int newy) {
		x = newx;
		y = newy;
	}
 
} 
 
class WindowCloser extends WindowAdapter {
        public void windowClosing(WindowEvent e) {
                System.exit(1);
        }
}
 
public class AniMain extends Frame implements Runnable {
	private Circle circle1;
 
	public AniMain() {
		circle1 = new Circle(50, 60, 100);
		new Thread(this).start();
	}
 
	public void paint(Graphics g) {
		circle1.draw(g);
	}
 
	public static void main(String args[]) {
		Frame frame = new AniMain();
		frame.setBounds(100, 100, 400, 400);
		frame.setBackground(Color.white);
		frame.setResizable(false);
		// register event
                frame.addWindowListener(new WindowCloser());
                
		frame.setVisible(true);
 
	}
 
	public void run() {
		int i;		
		i = 0;
		while (true) {
			if (i > 300)
				i = 0;
			else
				i++;
 
			circle1.move(50 + i, 60 + i);
			try {
				Thread.sleep(50);
				repaint();
			} catch (Exception e) {
			}
		}
	}
 
}
 
Open in New Window
 
05.16.2008 at 02:11AM PDT, ID: 21581098

Rank: Genius

The main thing is that the system 'owns' paint and you the programmer can 'hitch a ride' by overriding it and you can tell the system to call it by calling repaint. Otherwise, when paint is called is system-dependent.

You as programmer have to ensure that your sprites or drawings are in the correct position when paint is called. That's done by setting the new position in the timer thread and calling repaint.

The Graphics object is really only valid when paint is called, so shouldn't be kept as an instance variable.


>>
I guess that delete is useless know as, if I correctly understand now how they work, repaint clears every time the drawing area.
>>

That's right, although i did leave it in.

As for the window closing matters, if you're going to do much work in AWT it might be worth creating a custom class called 'CloseableFrame' and then subclassing it. See http://examples.oreilly.com/jenut/CloseableFrame.java
 
05.16.2008 at 06:59AM PDT, ID: 21582691

Rank: Genius

:-)
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628