/**
  * {@inheritDoc}
  *
  * <p>Paints a diagonal cross over the text if the comp is of type JLabel, does nothing otherwise.
  */
 @Override
 protected void doPaint(Graphics2D g, JComponent comp, int width, int height) {
   if (!(comp instanceof JLabel)) return;
   JLabel label = (JLabel) comp;
   Insets insets = label.getInsets(insetss);
   paintViewR.x = insets.left;
   paintViewR.y = insets.top;
   paintViewR.width = width - (insets.left + insets.right);
   paintViewR.height = height - (insets.top + insets.bottom);
   paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
   paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;
   SwingUtilities.layoutCompoundLabel(
       label,
       label.getFontMetrics(label.getFont()),
       label.getText(),
       null,
       label.getVerticalAlignment(),
       label.getHorizontalAlignment(),
       label.getVerticalTextPosition(),
       label.getHorizontalTextPosition(),
       paintViewR,
       paintIconR,
       paintTextR,
       label.getIconTextGap());
   doPaint(g, paintTextR);
 }
Exemplo n.º 2
0
 private void updatePointerRect() {
   if (seq == null || seq.getLength() == 0) {
     ptrRect.x = 0;
   } else {
     ptrRect.x = (int) (getWidth() * seq.getThumbPosition() / seq.getLength() - PTR_WIDTH / 2);
   }
 }
Exemplo n.º 3
0
  @Override
  public void show(
      final Component owner,
      final int aScreenX,
      final int aScreenY,
      final boolean considerForcedXY) {
    LOG.assertTrue(!isDisposed());

    Rectangle targetBounds =
        new Rectangle(new Point(aScreenX, aScreenY), getContent().getPreferredSize());
    ScreenUtil.moveRectangleToFitTheScreen(targetBounds);

    if (getParent() != null) {
      final Rectangle parentBounds = getParent().getBounds();
      parentBounds.x += STEP_X_PADDING;
      parentBounds.width -= STEP_X_PADDING * 2;
      if (parentBounds.intersects(targetBounds)) {
        targetBounds.x = getParent().getBounds().x - targetBounds.width - STEP_X_PADDING;
      }
    }

    if (getParent() == null) {
      PopupDispatcher.setActiveRoot(this);
    } else {
      PopupDispatcher.setShowing(this);
    }

    LOG.assertTrue(!isDisposed(), "Disposed popup, parent=" + getParent());
    super.show(owner, targetBounds.x, targetBounds.y, true);
  }
Exemplo n.º 4
0
  /** Returns the dimensions required to display the icon and label. */
  private Dimension getContentSize(AbstractButton button) {
    Rectangle scratchIconRect = new Rectangle();
    Rectangle scratchTextRect = new Rectangle();
    Rectangle scratchViewRect = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE);

    FontMetrics fm = button.getFontMetrics(button.getFont());
    SwingUtilities.layoutCompoundLabel(
        fm,
        button.getText(),
        button.getIcon(),
        button.getVerticalAlignment(),
        button.getHorizontalAlignment(),
        button.getVerticalTextPosition(),
        button.getHorizontalTextPosition(),
        scratchViewRect,
        scratchIconRect,
        scratchTextRect,
        button.getIconTextGap());

    Insets textInsets = getTextPadding();
    scratchTextRect.y -= textInsets.top;
    scratchTextRect.x -= textInsets.left;
    scratchTextRect.width += textInsets.left + textInsets.right;
    scratchTextRect.height += textInsets.top + textInsets.bottom;

    Insets iconInsets = getIconPadding();
    scratchIconRect.y -= iconInsets.top;
    scratchIconRect.x -= iconInsets.left;
    scratchIconRect.width += iconInsets.left + iconInsets.right;
    scratchIconRect.height += iconInsets.top + iconInsets.bottom;

    Rectangle sum = getSum(new Rectangle[] {scratchIconRect, scratchTextRect});
    return new Dimension(sum.width, sum.height);
  }
Exemplo n.º 5
0
    /**
     * Returns true if popup can fit the screen and the owner's top parent. It determines can popup
     * be lightweight or mediumweight.
     */
    boolean fitsOnScreen() {
      boolean result = false;
      Component component = getComponent();
      if (owner != null && component != null) {
        int popupWidth = component.getWidth();
        int popupHeight = component.getHeight();

        Container parent = (Container) SwingUtilities.getRoot(owner);
        if (parent instanceof JFrame || parent instanceof JDialog || parent instanceof JWindow) {

          Rectangle parentBounds = parent.getBounds();
          Insets i = parent.getInsets();
          parentBounds.x += i.left;
          parentBounds.y += i.top;
          parentBounds.width -= i.left + i.right;
          parentBounds.height -= i.top + i.bottom;

          if (JPopupMenu.canPopupOverlapTaskBar()) {
            GraphicsConfiguration gc = parent.getGraphicsConfiguration();
            Rectangle popupArea = getContainerPopupArea(gc);
            result = parentBounds.intersection(popupArea).contains(x, y, popupWidth, popupHeight);
          } else {
            result = parentBounds.contains(x, y, popupWidth, popupHeight);
          }
        } else if (parent instanceof JApplet) {
          Rectangle parentBounds = parent.getBounds();
          Point p = parent.getLocationOnScreen();
          parentBounds.x = p.x;
          parentBounds.y = p.y;
          result = parentBounds.contains(x, y, popupWidth, popupHeight);
        }
      }
      return result;
    }
Exemplo n.º 6
0
        /** @see java.awt.event.ComponentAdapter#componentResized(java.awt.event.ComponentEvent) */
        public void componentResized(ComponentEvent evt) {
          /* 获取目标组件 */
          Component component = evt.getComponent();

          /* 获取目标组件设置的边界和最小最大尺寸 */
          Rectangle bounds = component.getBounds();
          Dimension minSize = component.getMinimumSize();
          Dimension maxSize = component.getMaximumSize();

          /* 确定目标组件新的x轴坐标及宽度 */
          if (bounds.width < minSize.width) {
            bounds.x -= (bounds.x == m_oldBounds.x ? 0 : minSize.width - bounds.width);
            bounds.width = minSize.width;

          } else if (bounds.width > maxSize.width) {
            bounds.x += (bounds.x == m_oldBounds.x ? 0 : bounds.width - maxSize.width);
            bounds.width = maxSize.width;
          }

          /* 确定目标组件新的y轴坐标及高度 */
          if (bounds.height < minSize.height) {
            bounds.y -= (bounds.y == m_oldBounds.y ? 0 : minSize.height - bounds.height);
            bounds.height = minSize.height;

          } else if (bounds.height > maxSize.height) {
            bounds.y += (bounds.y == m_oldBounds.y ? 0 : bounds.height - maxSize.height);
            bounds.height = maxSize.height;
          }

          /* 设置目标组件的新边界 */
          component.setBounds(bounds);

          /* 保存新的边界 */
          m_oldBounds = bounds;
        }
Exemplo n.º 7
0
 private void eventFinish() {
   eventTick++;
   if (eventTick == 1) {
     boxes.clear();
     for (int i = 0; i < 9; i++) {
       if (i % 2 == 0) boxes.add(new Rectangle(-128, i * 16, GamePanel.WIDTH, 16));
       else boxes.add(new Rectangle(128, i * 16, GamePanel.WIDTH, 16));
     }
     JukeBox.stop("music1");
     JukeBox.play("finish");
   }
   if (eventTick > 1) {
     for (int i = 0; i < boxes.size(); i++) {
       Rectangle r = boxes.get(i);
       if (i % 2 == 0) {
         if (r.x < 0) r.x += 4;
       } else {
         if (r.x > 0) r.x -= 4;
       }
     }
   }
   if (eventTick > 33) {
     if (!JukeBox.isPlaying("finish")) {
       Data.setTime(player.getTicks());
       gsm.setState(GameStateManager.GAMEOVER);
     }
   }
 }
Exemplo n.º 8
0
  public static void center(JComponent c, Rectangle r, boolean withInsets) {
    Rectangle visible = c.getVisibleRect();

    visible.x = r.x - (visible.width - r.width) / 2;
    visible.y = r.y - (visible.height - r.height) / 2;

    Rectangle bounds = c.getBounds();
    Insets i = withInsets ? new Insets(0, 0, 0, 0) : c.getInsets();
    bounds.x = i.left;
    bounds.y = i.top;
    bounds.width -= i.left + i.right;
    bounds.height -= i.top + i.bottom;

    if (visible.x < bounds.x) visible.x = bounds.x;

    if (visible.x + visible.width > bounds.x + bounds.width)
      visible.x = bounds.x + bounds.width - visible.width;

    if (visible.y < bounds.y) visible.y = bounds.y;

    if (visible.y + visible.height > bounds.y + bounds.height)
      visible.y = bounds.y + bounds.height - visible.height;

    c.scrollRectToVisible(visible);
  }
Exemplo n.º 9
0
  /**
   * Get the vertical drop line rectangle.
   *
   * @param loc the drop location.
   * @return the rectangle.
   */
  private Rectangle getVDropLineRect(JTable.DropLocation loc) {
    if (!loc.isInsertColumn()) {
      return null;
    }

    boolean ltr = table.getComponentOrientation().isLeftToRight();
    int col = loc.getColumn();
    Rectangle rect = table.getCellRect(loc.getRow(), col, true);

    if (col >= table.getColumnCount()) {
      col--;
      rect = table.getCellRect(loc.getRow(), col, true);
      if (ltr) {
        rect.x = rect.x + rect.width;
      }
    } else if (!ltr) {
      rect.x = rect.x + rect.width;
    }

    if (rect.x == 0) {
      rect.x = -1;
    } else {
      rect.x -= 2;
    }

    rect.width = 3;

    return rect;
  }
Exemplo n.º 10
0
  /**
   * Damages the area surrounding the caret to cause it to be repainted in a new location. If
   * paint() is reimplemented, this method should also be reimplemented. This method should update
   * the caret bounds (x, y, width, and height).
   *
   * @param r the current location of the caret
   * @see #paint
   */
  @Override
  protected synchronized void damage(final Rectangle r) {
    if (r == null || fPainting) return;

    x = r.x - 4;
    y = r.y;
    width = 10;
    height = r.height;

    // Don't damage the border area.  We can't paint a partial border, so get the
    // intersection of the caret rectangle and the component less the border, if any.
    final Rectangle caretRect = new Rectangle(x, y, width, height);
    final Border border = getComponent().getBorder();
    if (border != null) {
      final Rectangle alloc = getComponent().getBounds();
      alloc.x = alloc.y = 0;
      final Insets borderInsets = border.getBorderInsets(getComponent());
      alloc.x += borderInsets.left;
      alloc.y += borderInsets.top;
      alloc.width -= borderInsets.left + borderInsets.right;
      alloc.height -= borderInsets.top + borderInsets.bottom;
      Rectangle2D.intersect(caretRect, alloc, caretRect);
    }
    x = caretRect.x;
    y = caretRect.y;
    width = Math.max(caretRect.width, 1);
    height = Math.max(caretRect.height, 1);
    repaint();
  }
  /**
   * Stores the position and size of the bouncing box that would be painted for the current
   * animation index in <code>r</code> and returns <code>r</code>. Subclasses that add to the
   * painting performed in this class's implementation of <code>paintIndeterminate</code> -- to draw
   * an outline around the bouncing box, for example -- can use this method to get the location of
   * the bouncing box that was just painted. By overriding this method, you have complete control
   * over the size and position of the bouncing box, without having to reimplement <code>
   * paintIndeterminate</code>.
   *
   * @param r the Rectangle instance to be modified; may be <code>null</code>
   * @return <code>null</code> if no box should be drawn; otherwise, returns the passed-in rectangle
   *     (if non-null) or a new rectangle
   * @see #setAnimationIndex
   * @since 1.4
   */
  protected Rectangle getBox(Rectangle r) {
    int currentFrame = getAnimationIndex();
    int middleFrame = numFrames / 2;

    if (sizeChanged() || delta == 0.0 || maxPosition == 0.0) {
      updateSizes();
    }

    r = getGenericBox(r);

    if (r == null) {
      return null;
    }
    if (middleFrame <= 0) {
      return null;
    }

    // assert currentFrame >= 0 && currentFrame < numFrames
    if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
      if (currentFrame < middleFrame) {
        r.x = componentInnards.x + (int) Math.round(delta * (double) currentFrame);
      } else {
        r.x = maxPosition - (int) Math.round(delta * (currentFrame - middleFrame));
      }
    } else { // VERTICAL indeterminate progress bar
      if (currentFrame < middleFrame) {
        r.y = componentInnards.y + (int) Math.round(delta * currentFrame);
      } else {
        r.y = maxPosition - (int) Math.round(delta * (currentFrame - middleFrame));
      }
    }
    return r;
  }
  protected Rectangle checkBounds(Rectangle source, Rectangle target) {
    if (target.contains(source)) return new Rectangle(target);
    else {
      Rectangle trimmedSource = new Rectangle(source);
      // is source outter target left
      if (trimmedSource.x < target.x) {
        trimmedSource.x = target.x;
        if (trimmedSource.width > target.width) trimmedSource.width = target.width;
      }
      // is source outter target top
      if (trimmedSource.y < target.y) {
        trimmedSource.y = target.y;
        if (trimmedSource.height > target.height) trimmedSource.height = target.height;
      }
      // is source outter target right
      if (trimmedSource.getMaxX() > target.getMaxX()) {
        trimmedSource.x = (int) target.getMaxX() - trimmedSource.width;
        //				if (trimmedSource.width > target.width)
        //					trimmedSource.width = target.width;
      }
      // is source outter target bottom
      if (trimmedSource.getMaxY() > target.getMaxY()) {
        trimmedSource.y = (int) target.getMaxY() - trimmedSource.height;
        //				if (trimmedSource.height > target.height)
        //					trimmedSource.height = target.height;
      }

      return trimmedSource;
    }
  }
Exemplo n.º 13
0
 private void ensureRectIsInBounds(final Rectangle rectangle, final Dimension bounds) {
   if (rectangle.x < 0) {
     rectangle.x = 0;
   }
   if (rectangle.x + rectangle.width > bounds.width) {
     final JScrollBar verticalScrollBar = scrollPane.getVerticalScrollBar();
     final int scrollBarWidth;
     if (verticalScrollBar != null) {
       scrollBarWidth = verticalScrollBar.getWidth();
     } else {
       scrollBarWidth = 0;
     }
     rectangle.x = bounds.width - rectangle.width - scrollBarWidth;
   }
   if (rectangle.y < 0) {
     rectangle.y = 0;
   }
   if (rectangle.y + rectangle.height > bounds.height) {
     final JScrollBar horizontalScrollBar = scrollPane.getHorizontalScrollBar();
     final int scrollBarHeight;
     if (horizontalScrollBar != null) {
       scrollBarHeight = horizontalScrollBar.getHeight();
     } else {
       scrollBarHeight = 0;
     }
     rectangle.y = bounds.height - rectangle.height - scrollBarHeight;
   }
 }
 @Override
 public void keyPressed(KeyEvent e) {
   Rectangle rect;
   switch (e.getKeyCode()) {
     case KeyEvent.VK_EQUALS:
     case KeyEvent.VK_PLUS:
     case KeyEvent.VK_ADD:
       FontSize++;
       textArea.setFont(new Font("monospaced", Font.PLAIN, FontSize));
       rect = textArea.getBounds();
       rect.x = 0;
       rect.y = 0;
       textArea.paintImmediately(rect);
       break;
     case KeyEvent.VK_MINUS:
     case KeyEvent.VK_SUBTRACT:
       if (FontSize > 8) {
         FontSize--;
         textArea.setFont(new Font("monospaced", Font.PLAIN, FontSize));
         rect = textArea.getBounds();
         rect.x = 0;
         rect.y = 0;
         textArea.paintImmediately(rect);
       }
       break;
   }
 }
Exemplo n.º 15
0
 public Dimension getPreferredSize() {
   Dimension ret = new Dimension();
   if (leftPage != null) {
     leftPageRect.x = 0;
     leftPageRect.y = 0;
     leftPageRect.width = pointsToPixels(leftFullPageRect.width);
     leftPageRect.height = pointsToPixels(leftFullPageRect.height);
     ret.height = leftPageRect.height;
     ret.width = leftPageRect.width;
   }
   if (rightPageRect != null) {
     rightPageRect.width = pointsToPixels(rightFullPageRect.width);
     rightPageRect.height = pointsToPixels(rightFullPageRect.height);
     if (rightPageRect.height > ret.height) {
       ret.height = rightPageRect.height;
     }
     if (leftPageRect != null) {
       rightPageRect.x = leftPageRect.width;
       ret.width += rightPageRect.width;
     } else {
       rightPageRect.x = rightPageRect.width;
       ret.width = rightPageRect.width * 2;
     }
     rightPageRect.y = 0;
   } else {
     ret.width *= 2;
   }
   ret.height += margin * 2;
   ret.width += margin * 2 + interpagemargin;
   return ret;
 }
  public Rectangle getVisualBounds(JComponent c, int type, int width, int height) {
    Rectangle bounds = new Rectangle(0, 0, width, height);
    if (type == VisuallyLayoutable.CLIP_BOUNDS) {
      return bounds;
    }

    AbstractButton b = (AbstractButton) c;

    if (type == VisuallyLayoutable.COMPONENT_BOUNDS
        && b.getBorder() != null
        && b.isBorderPainted()) {
      Border border = b.getBorder();
      if (border instanceof BackgroundBorder) {
        border = ((BackgroundBorder) border).getBackgroundBorder();
        if (border instanceof VisualMargin) {
          InsetsUtil.subtractInto(((VisualMargin) border).getVisualMargin(c), bounds);
        } else if (border instanceof QuaquaButtonBorder) {
          InsetsUtil.subtractInto(((QuaquaButtonBorder) border).getVisualMargin(c), bounds);
        }
      }
      return bounds;
    }

    String text = b.getText();
    boolean isEmpty = (text == null || text.length() == 0);
    if (isEmpty) {
      text = " ";
    }
    Icon icon = (b.isEnabled()) ? b.getIcon() : b.getDisabledIcon();

    if ((icon == null) && (text == null)) {
      return null;
    }

    FontMetrics fm = c.getFontMetrics(c.getFont());
    Insets insets = c.getInsets(viewInsets);

    viewR.x = insets.left;
    viewR.y = insets.top;
    viewR.width = width - (insets.left + insets.right);
    viewR.height = height - (insets.top + insets.bottom);

    iconR.x = iconR.y = iconR.width = iconR.height = 0;
    textR.x = textR.y = textR.width = textR.height = 0;

    String clippedText = layoutCL(b, fm, text, icon, viewR, iconR, textR);

    Rectangle textBounds = Fonts.getPerceivedBounds(text, c.getFont(), c);
    if (isEmpty) {
      textBounds.width = 0;
    }
    int ascent = fm.getAscent();
    textR.x += textBounds.x;
    textR.width = textBounds.width;
    textR.y += ascent + textBounds.y;
    textR.height -= fm.getHeight() - textBounds.height;

    bounds.setBounds(textR);
    return bounds;
  }
Exemplo n.º 17
0
  public static void scroll(
      JComponent c, Rectangle r, ScrollBias horizontalBias, ScrollBias verticalBias) {
    Rectangle visible = c.getVisibleRect();
    Rectangle dest = new Rectangle(r);

    if (dest.width > visible.width) {
      if (horizontalBias == ScrollBias.VIEWPORT) {
        // leave as is
      } else if (horizontalBias == ScrollBias.UNCHANGED) {
        if (dest.x <= visible.x && dest.x + dest.width >= visible.x + visible.width) {
          dest.width = visible.width;
        }
      } else {
        if (horizontalBias == ScrollBias.CENTER) {
          dest.x += (dest.width - visible.width) / 2;
        } else if (horizontalBias == ScrollBias.LAST) dest.x += dest.width - visible.width;

        dest.width = visible.width;
      }
    }

    if (dest.height > visible.height) {
      // same code as above in the other direction
    }

    if (!visible.contains(dest)) c.scrollRectToVisible(dest);
  }
Exemplo n.º 18
0
  void selected(boolean selected, boolean temporary) {
    boolean changed = this.selected != selected;

    if (changed) {
      this.selected = selected;
      java.awt.Rectangle bounds = getBounds();
      if (selected) {
        bounds.x -= BORDER_E;
        bounds.width += BORDER_E + BORDER_W;
        bounds.y -= BORDER_N;
        bounds.height += BORDER_N + BORDER_S;
      } else {
        isForeground(false);
        bounds.x += BORDER_E;
        bounds.width -= BORDER_E + BORDER_W;
        bounds.y += BORDER_N;
        bounds.height -= BORDER_N + BORDER_S;
      }

      setBounds(bounds);
      if (!temporary) {
        new Events.WidgetSelectedEvent(widget, selected).raise(this);
      }
    }
  }
Exemplo n.º 19
0
  private String layout(AbstractButton b, FontMetrics fm, int width, int height) {
    Insets i = b.getInsets();
    viewRect.x = i.left;
    viewRect.y = i.top;
    viewRect.width = width - (i.right + viewRect.x);
    viewRect.height = height - (i.bottom + viewRect.y);

    textRect.x = textRect.y = textRect.width = textRect.height = 0;
    iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;

    // layout the text and icon
    return SwingUtilities.layoutCompoundLabel(
        b,
        fm,
        b.getText(),
        b.getIcon(),
        b.getVerticalAlignment(),
        b.getHorizontalAlignment(),
        b.getVerticalTextPosition(),
        b.getHorizontalTextPosition(),
        viewRect,
        iconRect,
        textRect,
        b.getText() == null ? 0 : b.getIconTextGap());
  }
Exemplo n.º 20
0
  /**
   * Selects a range of text in a text component. If the new selection is outside of the previous
   * viewable rectangle, then the view is centered around the new selection.
   *
   * @param textArea The text component whose selection is to be centered.
   * @param start The start of the range to select.
   * @param end The end of the range to select.
   */
  private static void selectAndPossiblyCenter(JTextArea textArea, int start, int end) {

    textArea.setSelectionStart(start);
    textArea.setSelectionEnd(end);

    Rectangle r = null;
    try {
      r = textArea.modelToView(start);
      if (r == null) { // Not yet visible; i.e. JUnit tests
        return;
      }
      if (end != start) {
        r = r.union(textArea.modelToView(end));
      }
    } catch (BadLocationException ble) { // Never happens
      ble.printStackTrace();
      textArea.setSelectionStart(start);
      textArea.setSelectionEnd(end);
      return;
    }

    Rectangle visible = textArea.getVisibleRect();

    // If the new selection is already in the view, don't scroll,
    // as that is visually jarring.
    if (visible.contains(r)) {
      textArea.setSelectionStart(start);
      textArea.setSelectionEnd(end);
      return;
    }

    visible.x = r.x - (visible.width - r.width) / 2;
    visible.y = r.y - (visible.height - r.height) / 2;

    Rectangle bounds = textArea.getBounds();
    Insets i = textArea.getInsets();
    bounds.x = i.left;
    bounds.y = i.top;
    bounds.width -= i.left + i.right;
    bounds.height -= i.top + i.bottom;

    if (visible.x < bounds.x) {
      visible.x = bounds.x;
    }

    if (visible.x + visible.width > bounds.x + bounds.width) {
      visible.x = bounds.x + bounds.width - visible.width;
    }

    if (visible.y < bounds.y) {
      visible.y = bounds.y;
    }

    if (visible.y + visible.height > bounds.y + bounds.height) {
      visible.y = bounds.y + bounds.height - visible.height;
    }

    textArea.scrollRectToVisible(visible);
  }
Exemplo n.º 21
0
 private static void resetRects(JComponent c, int height) {
   Insets insets = c.getInsets();
   viewRect.x = insets.left;
   viewRect.y = insets.top;
   viewRect.width = c.getWidth() - (insets.right + viewRect.x);
   viewRect.height = height - (insets.bottom + viewRect.y);
   textRect.x = textRect.y = textRect.width = textRect.height = 0;
   iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;
 }
Exemplo n.º 22
0
 /**
  * Zooms out by making the source rectangle (srcRect) larger and centering it on (x,y). If we
  * can't make it larger, then make the window smaller.
  */
 public void zoomOut(int x, int y) {
   if (magnification <= 0.03125) return;
   double oldMag = magnification;
   double newMag = getLowerZoomLevel(magnification);
   double srcRatio = (double) srcRect.width / srcRect.height;
   double imageRatio = (double) imageWidth / imageHeight;
   double initialMag = imp.getWindow().getInitialMagnification();
   if (Math.abs(srcRatio - imageRatio) > 0.05) {
     double scale = oldMag / newMag;
     int newSrcWidth = (int) Math.round(srcRect.width * scale);
     int newSrcHeight = (int) Math.round(srcRect.height * scale);
     if (newSrcWidth > imageWidth) newSrcWidth = imageWidth;
     if (newSrcHeight > imageHeight) newSrcHeight = imageHeight;
     int newSrcX = srcRect.x - (newSrcWidth - srcRect.width) / 2;
     int newSrcY = srcRect.y - (newSrcHeight - srcRect.height) / 2;
     if (newSrcX < 0) newSrcX = 0;
     if (newSrcY < 0) newSrcY = 0;
     srcRect = new Rectangle(newSrcX, newSrcY, newSrcWidth, newSrcHeight);
     // IJ.log(newMag+" "+srcRect+" "+dstWidth+" "+dstHeight);
     int newDstWidth = (int) (srcRect.width * newMag);
     int newDstHeight = (int) (srcRect.height * newMag);
     setMagnification(newMag);
     setMaxBounds();
     // IJ.log(newDstWidth+" "+dstWidth+" "+newDstHeight+" "+dstHeight);
     if (newDstWidth < dstWidth || newDstHeight < dstHeight) {
       // IJ.log("pack");
       setDrawingSize(newDstWidth, newDstHeight);
       imp.getWindow().pack();
     } else repaint();
     return;
   }
   if (imageWidth * newMag > dstWidth) {
     int w = (int) Math.round(dstWidth / newMag);
     if (w * newMag < dstWidth) w++;
     int h = (int) Math.round(dstHeight / newMag);
     if (h * newMag < dstHeight) h++;
     x = offScreenX(x);
     y = offScreenY(y);
     Rectangle r = new Rectangle(x - w / 2, y - h / 2, w, h);
     if (r.x < 0) r.x = 0;
     if (r.y < 0) r.y = 0;
     if (r.x + w > imageWidth) r.x = imageWidth - w;
     if (r.y + h > imageHeight) r.y = imageHeight - h;
     srcRect = r;
   } else {
     srcRect = new Rectangle(0, 0, imageWidth, imageHeight);
     setDrawingSize((int) (imageWidth * newMag), (int) (imageHeight * newMag));
     // setDrawingSize(dstWidth/2, dstHeight/2);
     imp.getWindow().pack();
   }
   // IJ.write(newMag + " " + srcRect.x+" "+srcRect.y+" "+srcRect.width+" "+srcRect.height+"
   // "+dstWidth + " " + dstHeight);
   setMagnification(newMag);
   // IJ.write(srcRect.x + " " + srcRect.width + " " + dstWidth);
   setMaxBounds();
   repaint();
 }
Exemplo n.º 23
0
  /**
   * Paint cells.
   *
   * @param context DOCUMENT ME!
   * @param g DOCUMENT ME!
   * @param rMin DOCUMENT ME!
   * @param rMax DOCUMENT ME!
   * @param cMin DOCUMENT ME!
   * @param cMax DOCUMENT ME!
   */
  private void paintCells(
      SeaGlassContext context, Graphics g, int rMin, int rMax, int cMin, int cMax) {
    JTableHeader header = table.getTableHeader();
    TableColumn draggedColumn = (header == null) ? null : header.getDraggedColumn();

    TableColumnModel cm = table.getColumnModel();
    int columnMargin = cm.getColumnMargin();

    Rectangle cellRect;
    TableColumn aColumn;
    int columnWidth;

    if (table.getComponentOrientation().isLeftToRight()) {
      for (int row = rMin; row <= rMax; row++) {
        cellRect = table.getCellRect(row, cMin, false);
        for (int column = cMin; column <= cMax; column++) {
          aColumn = cm.getColumn(column);
          columnWidth = aColumn.getWidth();
          cellRect.width = columnWidth - columnMargin;
          if (aColumn != draggedColumn) {
            paintCell(context, g, cellRect, row, column);
          }

          cellRect.x += columnWidth;
        }
      }
    } else {
      for (int row = rMin; row <= rMax; row++) {
        cellRect = table.getCellRect(row, cMin, false);
        aColumn = cm.getColumn(cMin);
        if (aColumn != draggedColumn) {
          columnWidth = aColumn.getWidth();
          cellRect.width = columnWidth - columnMargin;
          paintCell(context, g, cellRect, row, cMin);
        }

        for (int column = cMin + 1; column <= cMax; column++) {
          aColumn = cm.getColumn(column);
          columnWidth = aColumn.getWidth();
          cellRect.width = columnWidth - columnMargin;
          cellRect.x -= columnWidth;
          if (aColumn != draggedColumn) {
            paintCell(context, g, cellRect, row, column);
          }
        }
      }
    }

    // Paint the dragged column if we are dragging.
    if (draggedColumn != null) {
      paintDraggedArea(context, g, rMin, rMax, draggedColumn, header.getDraggedDistance());
    }

    // Remove any renderers that may be left in the rendererPane.
    rendererPane.removeAll();
  }
Exemplo n.º 24
0
  @Override
  public void paint(Graphics g, JComponent c) {
    JButton button = (JButton) c;
    String text = button.getText();
    Icon icon = (button.isEnabled()) ? button.getIcon() : button.getDisabledIcon();

    if ((icon == null) && (text == null)) {
      return;
    }

    FontMetrics fm = g.getFontMetrics();
    paintViewInsets = c.getInsets(paintViewInsets);

    paintViewR.x = paintViewInsets.left;
    paintViewR.y = paintViewInsets.top;

    // Use inverted height &amp; width
    paintViewR.height = c.getWidth() - (paintViewInsets.left + paintViewInsets.right);
    paintViewR.width = c.getHeight() - (paintViewInsets.top + paintViewInsets.bottom);

    paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
    paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;

    Graphics2D g2 = (Graphics2D) g;
    AffineTransform tr = g2.getTransform();

    if (angle == 90) {
      g2.rotate(Math.PI / 2);
      g2.translate(0, -c.getWidth());
      paintViewR.x = c.getHeight() / 2 - (int) fm.getStringBounds(text, g).getWidth() / 2;
      paintViewR.y = c.getWidth() / 2 - (int) fm.getStringBounds(text, g).getHeight() / 2;
    } else if (angle == 270) {
      g2.rotate(-Math.PI / 2);
      g2.translate(-c.getHeight(), 0);
      paintViewR.x = c.getHeight() / 2 - (int) fm.getStringBounds(text, g).getWidth() / 2;
      paintViewR.y = c.getWidth() / 2 - (int) fm.getStringBounds(text, g).getHeight() / 2;
    }

    if (icon != null) {
      icon.paintIcon(c, g, paintIconR.x, paintIconR.y);
    }

    if (text != null) {
      int textX = paintTextR.x;
      int textY = paintTextR.y + fm.getAscent();

      if (button.isEnabled()) {
        paintText(g, c, new Rectangle(paintViewR.x, paintViewR.y, textX, textY), text);
      } else {
        paintText(g, c, new Rectangle(paintViewR.x, paintViewR.y, textX, textY), text);
      }
    }

    g2.setTransform(tr);
  }
Exemplo n.º 25
0
  /**
   * This paints the entire plot. It calls the draw methods of all the attached axis and data sets.
   * The order of drawing is - Axis first, data legends next, data last.
   *
   * @params g Graphics state.
   */
  public void paint(Graphics g) {
    int i;
    Graphics lg = g.create();
    Rectangle r = bounds();

    /* The r.x and r.y returned from bounds is relative to the
     ** parents space so set them equal to zero.
     */
    r.x = 0;
    r.y = 0;

    if (DefaultBackground == null) DefaultBackground = this.getBackground();
    if (DataBackground == null) DataBackground = this.getBackground();

    //        System.out.println("Graph2D paint method called!");

    if (!paintAll) return;

    r.x += borderLeft;
    r.y += borderTop;
    r.width -= borderLeft + borderRight;
    r.height -= borderBottom + borderTop;

    paintFirst(lg, r);

    if (!axis.isEmpty()) r = drawAxis(lg, r);
    else {
      if (clearAll) {
        Color c = g.getColor();
        g.setColor(DataBackground);
        g.fillRect(r.x, r.y, r.width, r.height);
        g.setColor(c);
      }
      drawFrame(lg, r.x, r.y, r.width, r.height);
    }

    paintBeforeData(lg, r);

    if (!dataset.isEmpty()) {

      datarect.x = r.x;
      datarect.y = r.y;
      datarect.width = r.width;
      datarect.height = r.height;

      for (i = 0; i < dataset.size(); i++) {
        ((DataSet) dataset.elementAt(i)).draw_data(lg, r);
      }
    }

    paintLast(lg, r);

    lg.dispose();
  }
    protected void calculateTabRects(int tabPlacement, int tabCount) {
      FontMetrics metrics = getFontMetrics();
      Dimension size = tabPane.getSize();
      Insets insets = tabPane.getInsets();
      Insets tabAreaInsets = getTabAreaInsets(tabPlacement);
      int fontHeight = metrics.getHeight();
      int selectedIndex = tabPane.getSelectedIndex();
      int i, j;

      int x = tabAreaInsets.left - 2;
      int y = tabAreaInsets.top;
      int totalWidth = 0;
      int totalHeight = 0;

      //
      // Calculate bounds within which a tab run must fit
      //

      maxTabHeight = calculateMaxTabHeight(tabPlacement);

      runCount = 0;
      selectedRun = -1;

      if (tabCount == 0) {
        return;
      }

      selectedRun = 0;
      runCount = 1;

      // Run through tabs and lay them out in a single run
      Rectangle rect;
      for (i = 0; i < tabCount; i++) {
        rect = rects[i];

        if (i > 0) {
          rect.x = rects[i - 1].x + rects[i - 1].width - 1;
        } else {
          tabRuns[0] = 0;
          maxTabWidth = 0;
          totalHeight += maxTabHeight;
          rect.x = x;
        }
        rect.width = calculateTabWidth(tabPlacement, i, metrics);
        totalWidth = rect.x + rect.width;
        maxTabWidth = Math.max(maxTabWidth, rect.width);

        rect.y = y;
        rect.height = maxTabHeight /* - 2 */;
      }

      // tabPanel.setSize(totalWidth, totalHeight);
      tabScroller.tabPanel.setPreferredSize(new Dimension(totalWidth, totalHeight));
    }
Exemplo n.º 27
0
  /**
   * Paints the icon.
   *
   * @param g the instance of Graphics
   * @param c the instance of JComponent
   * @param iconRect the instance of Rectangle
   */
  protected void paintIcon(Graphics g, JComponent c, Rectangle iconRect) {
    if (c.getWidth() % 2 != 0) {
      iconRect.x += 1;
    }
    if (c.getHeight() % 2 != 0) {
      iconRect.y += 1;
    }

    iconRect.x += getWidthOffset();
    iconRect.y += getHeightOffset();

    super.paintIcon(g, c, iconRect);
  }
Exemplo n.º 28
0
  /**
   * Paints the text.
   *
   * @param g the instance of Graphics
   * @param c the instance of JComponent
   * @param textRect the instance of Rectangle
   * @param text the text string
   */
  protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
    if (c.getWidth() % 2 != 0) {
      textRect.x += 1;
    }
    if (c.getHeight() % 2 != 0) {
      textRect.y += 1;
    }

    textRect.x += getWidthOffset();
    textRect.y += getHeightOffset();

    super.paintText(g, c, textRect, text);
  }
Exemplo n.º 29
0
 XContentWindow createContent(WindowDimensions dims) {
   Rectangle rec = dims.getBounds();
   // Fix for  - set the location of the content window to the (-left inset, -top inset)
   Insets ins = dims.getInsets();
   if (ins != null) {
     rec.x = -ins.left;
     rec.y = -ins.top;
   } else {
     rec.x = 0;
     rec.y = 0;
   }
   return new XContentWindow(this, rec);
 }
Exemplo n.º 30
0
 protected Rectangle getVisibleEditorRect() {
   Rectangle alloc = getBounds();
   if ((alloc.width > 0) && (alloc.height > 0)) {
     alloc.x = alloc.y = 0;
     Insets insets = getInsets();
     alloc.x += insets.left;
     alloc.y += insets.top;
     alloc.width -= insets.left + insets.right;
     alloc.height -= insets.top + insets.bottom;
     return alloc;
   }
   return null;
 }