⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 graphicsdirector.java

📁 Vyger offers a D & D and Rogue-like environment in a graphical online roleplay game.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Light And Shadow. A Persistent Universe based on Robert Jordan's Wheel of Time Books.
 * Copyright (C) 2001-2002 WOTLAS Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package wotlas.libs.graphics2D;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;

/** A GraphicsDirector is the root class of this graphics2D engine. It manages
 *  Drawables and has a WindowPolicy for scrollings.
 *
 *  the only synchronized methods in GraphicsDirector are paint() and tick()
 *  so if you happen to handle events or change parameters do it with care !
 *
 * @author MasterBob, Aldiss, Petrus, Diego
 * @see wotlas.libs.graphics2D.ImageLibrary
 * @see wotlas.libs.graphics2D.Drawable
 * @see wotlas.libs.graphics2D.DrawableIterator
 */

public class GraphicsDirector extends JPanel {

 /*------------------------------------------------------------------------------------*/

  /** Represents the visible part of the JPanel (it has the JPanel's size)
   *  and is expressed in the background's coordinate (we use the background as a
   *  reference here, because all Drawables should be expressed in background
   *  coordinates).
   */
    protected Rectangle screen;
 
  /** Background's Dimension. The background can be any Drawable,
   *  we don't have to possess a handle it, we only need its dimension.
   */
    protected Dimension background;

  /** Our Drawable reference, we need to know which drawable our windowPolicy
   *  is going to refer to center the screen.
   */
    protected Drawable refDrawable;

  /** Our drawables. They are sorted by priority.
   */
    protected DrawableIterator drawables;

  /** The image library from which will take our images.
   */
    protected ImageLibrary imageLib;

  /** Our WindowPolicy. It tells us how to move the screen on the background.
   */
    protected WindowPolicy windowPolicy;

 /*------------------------------------------------------------------------------------*/

  /** Can we display our drawables ?
   */
    protected boolean display;

  /** Lock for repaint...
   */
    protected Object lockPaint = new Object();

  /** OffScreen image for the GraphicsDirector. 
   */
    protected Image backBufferImage;

  /** FOR REPAINT SOLUTION 2
   ** To repaint the screen.
   * 
    private Thread paintThread;
  
   ** Is there already a Thread waiting to repaint the screen ?
   *
    private boolean isLocked;

   ** END OF REPAINT SOLUTION 2 */

 /*------------------------------------------------------------------------------------*/

  /** Constructor. The window policy is not supposed to change during the life of the
   *  GraphicsDirector, but you can still change it by invoking the setWindowPolicy()
   *  method. The ImageLibrary is set to the default one : ImageLibrary.getDefault...
   *
   * @param windowPolicy a policy that manages window scrolling.
   * @exception ImageLibraryException if no ImageLibrary is found.
   */
    public GraphicsDirector( WindowPolicy windowPolicy )
    throws ImageLibraryException {
    	this( windowPolicy, null );
    }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** Constructor. The window policy is not supposed to change during the life of the
   *  GraphicsDirector, but you can still change it by invoking the setWindowPolicy()
   *  method. If the imageLibrary is set to null we seek for a default one.
   *
   * @param windowPolicy a policy that manages window scrolling.
   * @param imageLib ImageLibrary to use for this GraphicsDirector.
   * @exception ImageLibraryException if no ImageLibrary is found.
   */
    public GraphicsDirector( WindowPolicy windowPolicy, ImageLibrary imageLib )
    throws ImageLibraryException {
      super(false); // we don't use the default JPanel double-buffering

      if(imageLib==null) {
         this.imageLib = ImageLibrary.getDefaultImageLibrary();
         
         if(this.imageLib==null)
            throw new ImageLibraryException("No Image Library Found !");
      }
      else
         this.imageLib = imageLib;

      display = false;
      drawables = new DrawableIterator();
      setWindowPolicy( windowPolicy );
      setBackground( Color.white );
      // isLocked=false;  // remove this comment for REPAINT solution 2
    }
    
 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** To initialize the GraphicsDirector when using TileMap. A call to this method suppresses all the
   *  previously possessed Drawable Objects.
   *  <p><b>IMPORTANT:</b> The backDrawable & refDrawable are automatically added to
   *  the GraphicsDirector's Drawable list.
   *
   * @param backDrawable the drawable that you will use as a reference for your 2D cordinates.
   * @param refDrawable reference Drawable for screen movements. The way the screen moves
   *        is dictated by the WindowPolicy and refers to this drawable.
   * @param screen initial dimension for this JPanel
   */
   public void preTileMapInitWithPlayer( Drawable refDrawable, Dimension screen ) {

      // we reset the GraphicsDirector's drawables
         drawables.clear();
         addDrawable( refDrawable );
      // Background dims
         display = false;
      // Screen defaults
         this.screen = new Rectangle( screen );
         setPreferredSize( screen );
         setMinimumSize( new Dimension(10,10) );

      // We set the new drawable reference an tick our WindowPolicy.
         this.refDrawable = refDrawable;
   }

    /** To initialize the GraphicsDirector when using TileMap. A call to this method suppresses all the
    *  previously possessed Drawable Objects.
    *  <p><b>IMPORTANT:</b> The backDrawable & refDrawable are automatically added to
    *  the GraphicsDirector's Drawable list.
    *
    * @param backDrawable the drawable that you will use as a reference for your 2D cordinates.
    * @param refDrawable reference Drawable for screen movements. The way the screen moves
    *        is dictated by the WindowPolicy and refers to this drawable.
    * @param screen initial dimension for this JPanel
    */
    public void preTileMapInit( Dimension screen ) {

      // we reset the GraphicsDirector's drawables
         drawables.clear();
      // Background dims
         display = false;
      // Screen defaults
         this.screen = new Rectangle( screen );
         setPreferredSize( screen );
         setMinimumSize( new Dimension(50,50) );
    }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

    public void tileMapInit( Dimension background ) {
        this.background = background;
        setMaximumSize( background );
        windowPolicy.tick();
        display = true;
    }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** To initialize the GraphicsDirector. A call to this method suppresses all the
   *  previously possessed Drawable Objects.
   *  <p><b>IMPORTANT:</b> The backDrawable & refDrawable are automatically added to
   *  the GraphicsDirector's Drawable list.
   *
   * @param backDrawable the drawable that you will use as a reference for your 2D cordinates.
   * @param refDrawable reference Drawable for screen movements. The way the screen moves
   *        is dictated by the WindowPolicy and refers to this drawable.
   * @param screen initial dimension for this JPanel
   */
    public void init( Drawable backDrawable,  Drawable refDrawable, Dimension screen) {

      // we reset the GraphicsDirector's drawables
         drawables.clear();
         addDrawable( backDrawable );
         addDrawable( refDrawable );

      // Background dims
         display = false;
         background = new Dimension( backDrawable.getWidth(), backDrawable.getHeight() );

      // Screen defaults
         this.screen = new Rectangle( screen );
         setPreferredSize( screen );
         setMaximumSize( background );
         setMinimumSize( new Dimension(10,10) );

      // We set the new drawable reference an tick our WindowPolicy.
         this.refDrawable = refDrawable;
         windowPolicy.tick();
         display = true;
   }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** To set the window policy.
   */
    public void setWindowPolicy( WindowPolicy windowPolicy ) {
       this.windowPolicy = windowPolicy;
       windowPolicy.init( this );
    }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** Our customized repaint method
   */
    public void repaint() {      
       if(lockPaint==null) return; // to prevent repaint() calls during the constructor call.

       paint( getGraphics() );

// SOLUTION 2 : (gives better performance but is not stable on slow computers)
/*
       synchronized( lockPaint ) {

         if(paintThread==null) {
           isLocked=false;

           paintThread =new Thread() {
             public void run() {
              while( true )
                try{
                    GraphicsDirector.this.paint( GraphicsDirector.this.getGraphics() );

                    synchronized( lockPaint ) {
                        isLocked=true;
                        lockPaint.notifyAll();
                    	lockPaint.wait(400);
                    	isLocked=false;
                    }
                }catch( Exception e ) {
                   System.out.println("Exception in repaint() : "+e);
                   isLocked=false;
                }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -