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

📄 travellercanvas.java

📁 经典的货郎担问题解决办法
💻 JAVA
字号:
/*** This code class was written by Kent Paul Dolan.  Most of its contents** were removed from Scott Robert Ladd's Traveller.java and** TravellerWorld.java source files; see those files for Scott's** copyright rules.  See accompanying file TravellerDoc.html for status** of the modifications here (an extensive refactoring and rewrite) for** your use.*/package com.well.www.user.xanthian.java.ui;import java.awt.*;import java.awt.event.*;import com.coyotegulch.ui.*;import com.well.www.user.xanthian.java.tools.*;public class TravellerCanvas    extends Canvas{  //-------------------------  // constants  //-------------------------  private static final int    NODE_SIZE              =  2;  private static final int    NODE_SIZE2             =  2 * NODE_SIZE;  public  static final int INTERNAL_CLIPPING_PADDING =  10;  public  static final int EXTERNAL_BORDER_PADDING   =   8;  private static final int WORKING_EDGE              = 320;  // dimensions  public static final Dimension WORKING_DIMENSIONS = new    Dimension( WORKING_EDGE, WORKING_EDGE );  public static final Dimension PADDED_DIMENSIONS = new    Dimension    (      WORKING_EDGE      + 2 * INTERNAL_CLIPPING_PADDING      + 2 * EXTERNAL_BORDER_PADDING,      WORKING_EDGE      + 2 * INTERNAL_CLIPPING_PADDING      + 2 * EXTERNAL_BORDER_PADDING    );  // graphics entity hooks  private Image           m_image       = null;  private TravellerFrame  m_canvasFrame = null;  private EdgedPanel      m_canvasPanel = null;  // debugging hook abbreviation  boolean DB;  public TravellerCanvas()  {    super();    // set and get size    setSize    (      WORKING_EDGE + 2 * INTERNAL_CLIPPING_PADDING,      WORKING_EDGE + 2 * INTERNAL_CLIPPING_PADDING    );  }  public void setup(String titleBarName)  {/*** Create window to contain the Traveller's route canvas.*/    m_canvasFrame = new TravellerFrame(titleBarName);    m_canvasFrame.setVisible(true);    m_canvasFrame.invalidate();/*** Create a panel to hold the canvas.*/    m_canvasPanel = new EdgedPanel();    m_canvasPanel.setBackground(TravellerColors.MAIN_COLOR);    m_canvasPanel.setForeground(TravellerColors.MAIN_TEXT);    m_canvasPanel.setLayout(new FlowLayout());    m_canvasPanel.setVisible(true);    m_canvasPanel.setSize    (      WORKING_EDGE      + 2 * INTERNAL_CLIPPING_PADDING      + 2 * EXTERNAL_BORDER_PADDING,      WORKING_EDGE      + 2 * INTERNAL_CLIPPING_PADDING      + 2 * EXTERNAL_BORDER_PADDING    );    m_image =      this.createImage      (        WORKING_EDGE + 2 * INTERNAL_CLIPPING_PADDING,        WORKING_EDGE + 2 * INTERNAL_CLIPPING_PADDING      );    m_canvasPanel.add("Center", this);    m_canvasFrame.add( "Center", m_canvasPanel );    // m_canvasPanel.repaint();    m_canvasFrame.pack();    m_canvasFrame.validate();    this.redraw();    m_canvasFrame.repaint();  }  public void clear()  {    m_image = null;    repaint();  }  public void update(Graphics g)  {    paint(g);  }  public void drawNodes  (    int   nodes[][],    int   nodesCount,    int   nodeX,    int   nodeY,    Color nodeColor  )  {    Graphics gi = m_image.getGraphics();    // draw cities    gi.setColor(nodeColor);    for (int i = 0; i < nodesCount; ++i)    {      gi.fillOval      (        nodes[i][nodeX] - NODE_SIZE + INTERNAL_CLIPPING_PADDING,        nodes[i][nodeY] - NODE_SIZE + INTERNAL_CLIPPING_PADDING,        NODE_SIZE2,        NODE_SIZE2      );    }  }  public void drawEdges  (    int nodeList[],    int numberOfNodes,    int nodeDrawAtLocations[][],    int nodeX,    int nodeY,    Color edgeColor,    boolean closePath  )  {    Graphics gi = m_image.getGraphics();    gi.setColor( edgeColor );    for (int n = 1; n < numberOfNodes; ++n)    {      gi.drawLine      (        nodeDrawAtLocations[nodeList[n-1]][nodeX] + INTERNAL_CLIPPING_PADDING,        nodeDrawAtLocations[nodeList[n-1]][nodeY] + INTERNAL_CLIPPING_PADDING,        nodeDrawAtLocations[nodeList[n]][nodeX] + INTERNAL_CLIPPING_PADDING,        nodeDrawAtLocations[nodeList[n]][nodeY] + INTERNAL_CLIPPING_PADDING      );    }    if ( closePath )    {      gi.drawLine      (        nodeDrawAtLocations[nodeList[numberOfNodes - 1]][nodeX]        + INTERNAL_CLIPPING_PADDING,        nodeDrawAtLocations[nodeList[numberOfNodes - 1]][nodeY]        + INTERNAL_CLIPPING_PADDING,        nodeDrawAtLocations[nodeList[0]][nodeX] + INTERNAL_CLIPPING_PADDING,        nodeDrawAtLocations[nodeList[0]][nodeY] + INTERNAL_CLIPPING_PADDING      );    }  }  public void drawImage()  {    try    {      getGraphics().drawImage(m_image,0,0,this);    }    catch (Exception ex)    {      // do nothing    }  }  public void paint(Graphics g)  {      if (m_image == null)          redraw();      // draw image      g.drawImage(m_image,0,0,this);  }  public void redraw()  {/*** If we don't have a world, make one.*/    if ( m_image == null )    {      m_image =        this.createImage        (          WORKING_EDGE + 2 * INTERNAL_CLIPPING_PADDING,          WORKING_EDGE + 2 * INTERNAL_CLIPPING_PADDING        );    }    Graphics gi = m_image.getGraphics();/*** Clear background.*/    gi.setColor(TravellerColors.COLOR_WORLD);    gi.fillRect    (      0,      0,      WORKING_DIMENSIONS.width + 2 * INTERNAL_CLIPPING_PADDING,      WORKING_DIMENSIONS.height + 2 * INTERNAL_CLIPPING_PADDING    );    this.drawImage();/*** This little commented-out jewel is a "how to dump a stack trace"** example, and helped me find out where I was calling a build of the** route canvas one extra time which was generating a second set of** cities and making the initial display of Traveller look buggy.  This** is really all you need to do to dump a stack trace to stderr with a** string comment attached.  Neat!*///    try//    {//      throw new Throwable( "stuck in TravellerCanvas.redraw() again!" );//    }//    catch (Throwable t)//    {//      t.printStackTrace();//    }  }  public void clearPlayfield()  {    if ( m_image != null )    {      Graphics gi = m_image.getGraphics();/*** Clear background.*/      gi.setColor(TravellerColors.COLOR_WORLD);      gi.fillRect      (        0,        0,        WORKING_DIMENSIONS.width + 2 * INTERNAL_CLIPPING_PADDING,        WORKING_DIMENSIONS.height + 2 * INTERNAL_CLIPPING_PADDING      );    }    else    {      this.redraw();    }  }  public void windowClose()  {    this.clear();    this.m_canvasPanel = null;    if ( m_canvasFrame != null )    {      this.m_canvasFrame.windowClose();      this.m_canvasFrame = null;    }  }  public void toFront()  {    m_canvasFrame.toFront();  }}

⌨️ 快捷键说明

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