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

📄 imagelabel.java

📁 ErGo是一个很早的Java通用围棋服务器(IGS/NNGS)客户端程序。有全部源码和文档
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    else {
      if (explicitSize)
        g.drawImage(image, border, border,
                    width-2*border, height-2*border,
                    this);
      else
        g.drawImage(image, border, border, this);
      drawRect(g, 0, 0, width-1, height-1,
               border, borderColor);
    }
  }

  //----------------------------------------------------
  /** Used by layout managers to calculate the usual
   *  size allocated for the Component. Since some
   *  layout managers (e.g. BorderLayout) may
   *  call this before paint is called, you need to
   *  make sure that the image is done loading, which
   *  will force a resize, which determines the values
   *  returned.
   */
  public Dimension getPreferredSize() {
    if (!doneLoading)
      waitForImage(false);
    return(super.getPreferredSize());
  }

  //----------------------------------------------------
  /** Used by layout managers to calculate the smallest
   *  size allocated for the Component. Since some
   *  layout managers (e.g. BorderLayout) may
   *  call this before paint is called, you need to
   *  make sure that the image is done loading, which
   *  will force a resize, which determines the values
   *  returned.
   */
   public Dimension getMinimumSize() {
     if (!doneLoading)
       waitForImage(false);
     return(super.getMinimumSize());
   }
  
  //----------------------------------------------------
  // LayoutManagers (such as BorderLayout) might call
  // resize or reshape with only 1 dimension of
  // width/height non-zero. In such a case, you still
  // want the other dimension to come from the image
  // itself.

  /** Resizes the ImageLabel. If you don't resize the
   *  label explicitly, then what happens depends on
   *  the layout manager. With FlowLayout, as with
   *  FlowLayout for Labels, the ImageLabel takes its
   *  minimum size, just enclosing the image. With
   *  BorderLayout, as with BorderLayout for Labels,
   *  the ImageLabel is expanded to fill the
   *  section. Stretching GIF/JPG files does not always
   *  result in clear looking images. <B>So just as
   *  with builtin Labels and Buttons, don't
   *  use FlowLayout if you don't want the Buttons to
   *  get resized.</B> If you don't use any
   *  LayoutManager, then the ImageLabel will also
   *  just fit the image.
   *  <P>
   *  Note that if you resize explicitly, you must do
   *  it <B>before</B> the ImageLabel is added to the
   *  Container. In such a case, the explicit size
   *  overrides the image dimensions.
   *
   * @see #setBounds
   */
  public void setSize(int width, int height) {
    if (!doneLoading) {
      explicitSize=true;
      if (width > 0)
        explicitWidth=width;
      if (height > 0)
        explicitHeight=height;
    }
    super.setSize(width, height);
  }

  /** Resizes the ImageLabel. If you don't resize the
   *  label explicitly, then what happens depends on
   *  the layout manager. With FlowLayout, as with
   *  FlowLayout for Labels, the ImageLabel takes its
   *  minimum size, just enclosing the image. With
   *  BorderLayout, as with BorderLayout for Labels,
   *  the ImageLabel is expanded to fill the
   *  section. Stretching GIF/JPG files does not always
   *  result in clear looking images. <B>So just as
   *  with builtin Labels and Buttons, don't
   *  use FlowLayout if you don't want the Buttons to
   *  get resized.</B> If you don't use any
   *  LayoutManager, then the ImageLabel will also
   *  just fit the image.
   *  <P>
   *  Note that if you resize explicitly, you must do
   *  it <B>before</B> the ImageLabel is added to the
   *  Container. In such a case, the explicit size
   *  overrides the image dimensions.
   *
   * @see #setSize
   */
  public void setBounds (int x, int y, int width, int height) {
    if (!doneLoading) {
      explicitSize=true;
      if (width > 0)
        explicitWidth=width;
      if (height > 0)
        explicitHeight=height;
    }
    super.setBounds(x, y, width, height);
  }
  
  //----------------------------------------------------
  // You can't just set the background color to
  // the borderColor and skip drawing the border,
  // since it messes up transparent gifs. You
  // need the background color to be the same as
  // the container.
  
  /** Draws a rectangle with the specified OUTSIDE
   *  left, top, width, and height.
   *  Used to draw the border.
   */
  protected void drawRect(Graphics g,
                          int left, int top,
                          int width, int height,
                          int lineThickness,
                          Color rectangleColor) {
    g.setColor(rectangleColor);
    for(int i=0; i<lineThickness; i++) {
      g.drawRect(left, top, width, height);
      if (i < lineThickness-1) {  // Skip last iteration
        left = left + 1;
        top = top + 1;
        width = width - 2;
        height = height - 2;
      }
    }
  }
  
  //----------------------------------------------------
  /** Calls System.out.println if the debug variable
   *  is true; does nothing otherwise.
   *
   * @param message The String to be printed.
   */
  protected void debug(String message) {
    if (debug)
      System.out.println(message);
  }
  
  //----------------------------------------------------
  // Creates the URL with some error checking.
  
  private static URL makeURL(String s) {
    URL u = null;
    try { u = new URL(s); }
    catch (MalformedURLException mue) {
      System.out.println("Bad URL " + s + ": " + mue);
      mue.printStackTrace();
    }
    return(u);
  }

  private static URL makeURL(URL directory,
                             String file) {
    URL u = null;
    try { u = new URL(directory, file); }
    catch (MalformedURLException mue) {
      System.out.println("Bad URL " +
                         directory.toExternalForm() +
                         ", " + file + ": " + mue);
      mue.printStackTrace();
    }
    return(u);
  }

  //----------------------------------------------------
  // Loads the image. Needs to be static since it is
  // called by the constructor.
  
  private static Image loadImage(URL url) {
    if (debug)
      System.err.println("loadImage(" + url + ")");
    return(Toolkit.getDefaultToolkit().getImage(url));
  }

  //----------------------------------------------------
  /** The Image associated with the ImageLabel. */

  public Image getImage() {
    return(image);
  }
  
  //----------------------------------------------------
  /** Gets the border width. */

  public int getBorder() {
    return(border);
  }

  /** Sets the border thickness. */
  
  public void setBorder(int border) {
    this.border = border;
  }

  //----------------------------------------------------
  /** Gets the border color. */

  public Color getBorderColor() {
    return(borderColor);
  }

  /** Sets the border color. */
  
  public void setBorderColor(Color borderColor) {
    this.borderColor = borderColor;
  }
  
  //----------------------------------------------------
  // You could just call size().width and size().height,
  // but since we've overridden resize to record
  // this, we might as well use it.
  
  /** Gets the width (image width plus twice border). */

  public int getWidth() {
    return(width);
  }

  /** Gets the height (image height plus 2x border). */

  public int getHeight() {
    return(height);
  }
  
  //----------------------------------------------------
  /** Has the ImageLabel been given an explicit size?
   *  This is used to decide if the image should be
   *  stretched or not. This will be true if you
   *  call resize or reshape on the ImageLabel before
   *  adding it to a Container. It will be false
   *  otherwise.
   */
  protected boolean hasExplicitSize() {
    return(explicitSize);
  }

  //----------------------------------------------------
  /** Returns the string representing the URL that
   *  will be used if none is supplied in the
   *  constructor.
   */
  public static String getDefaultImageString() {
    return(defaultImageString);
  }

  /** Sets the string representing the URL that
   *  will be used if none is supplied in the
   *  constructor. Note that this is static,
   *  so is shared by all ImageLabels. Using this
   *  might be convenient in testing, but "real"
   *  applications should avoid it.
   */
  public static void setDefaultImageString(String file) {
    defaultImageString = file;
  }

  //----------------------------------------------------
  /** Returns the string representing the URL
   *  of image.
   */
  protected String getImageString() {
    return(imageString);
  }

  //----------------------------------------------------
  /** Is the debugging flag set? */
   
  public boolean isDebugging() {
    return(debug);
  }

  /** Set the debugging flag. Verbose messages
   *  will be printed to System.out if this is true.
   */
  public void setIsDebugging(boolean debug) {
    this.debug = debug;
  }

  //----------------------------------------------------
}

⌨️ 快捷键说明

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