home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-04-23 | 1.6 KB | 78 lines |
- import java.awt.*;
-
- class MyCanvas extends Canvas implements Runnable {
- Thread thread = null;
- int Refresh = 50;
-
- protected Image img = null;
- protected Graphics grps = null;
-
- Class c = null;
- MyWidget mw = null;
-
- MyCanvas( String classname ) {
- try{
- c = Class.forName( classname );
- }
- catch ( ClassNotFoundException e) {
- System.err.println("Sorry - but i cannot find a class named \"" + classname + "\"");
- System.exit(-1);
- }
-
- try {
- mw = (MyWidget) c.newInstance();
- }
- catch ( Exception e) {
- System.err.println("Creating an instance of \"" + classname + "\" have failed.");
- System.exit(-1);
- }
- }
-
- public String getWidgetName() {
- return mw.getClass().getName();
- }
-
- // start/stop animation
-
- public void start() {
- if(thread==null) {
- thread = new Thread( this, ""); // thread starten, falls noch nicht getan
- thread.start();
- } }
-
- public void stop() { thread = null; }
-
- public void run() {
- while( thread != null )
- {
- repaint();
-
- try { thread.sleep(Refresh); }
- catch( InterruptedException e ) {}
- } }
-
- public void paint(Graphics g) {
- if(img!=null) {
- g.drawImage( img, 0, 0, this);
- } }
-
- public void update(Graphics g) {
- if( grps==null )
- {
- img = createImage( MyWidget.LEN, MyWidget.LEN );
- grps = img.getGraphics();
- }
-
- mw.repaint( grps );
-
- g.drawImage( img, 0, 0, this);
- }
-
- public Dimension minimumSize() { return new Dimension( MyWidget.LEN, MyWidget.LEN ); }
-
- public Dimension preferredSize() { return minimumSize(); }
- }
-
-
-
-