/**
   * Method to check that the current position is in the viewing area and if not scroll to center
   * the current position if possible
   */
  public void checkScroll() {
    // get the x and y position in pixels
    int xPos = (int) (colIndex * zoomFactor);
    int yPos = (int) (rowIndex * zoomFactor);

    // only do this if the image is larger than normal
    if (zoomFactor > 1) {

      // get the rectangle that defines the current view
      JViewport viewport = scrollPane.getViewport();
      Rectangle rect = viewport.getViewRect();
      int rectMinX = (int) rect.getX();
      int rectWidth = (int) rect.getWidth();
      int rectMaxX = rectMinX + rectWidth - 1;
      int rectMinY = (int) rect.getY();
      int rectHeight = (int) rect.getHeight();
      int rectMaxY = rectMinY + rectHeight - 1;

      // get the maximum possible x and y index
      int macolIndexX = (int) (picture.getWidth() * zoomFactor) - rectWidth - 1;
      int macolIndexY = (int) (picture.getHeight() * zoomFactor) - rectHeight - 1;

      // calculate how to position the current position in the middle of the viewing
      // area
      int viewX = xPos - (int) (rectWidth / 2);
      int viewY = yPos - (int) (rectHeight / 2);

      // reposition the viewX and viewY if outside allowed values
      if (viewX < 0) viewX = 0;
      else if (viewX > macolIndexX) viewX = macolIndexX;
      if (viewY < 0) viewY = 0;
      else if (viewY > macolIndexY) viewY = macolIndexY;

      // move the viewport upper left point
      viewport.scrollRectToVisible(new Rectangle(viewX, viewY, rectWidth, rectHeight));
    }
  }
Пример #2
0
 public void drawMe(Graphics g) {
   g.setColor(getColor());
   Rectangle r =
       new Rectangle(10 + (width) * getIndex() + 40, height - getNum(), width - 1, getNum());
   g.fillRect(r.x, r.y, (int) r.getWidth(), (int) r.getHeight());
 }