Esempio n. 1
0
  /**
   * Move the component to its new location. The dragged Point must be in the destination
   * coordinates.
   */
  @Override
  public void mouseDragged(MouseEvent e) {
    Point dragged = e.getLocationOnScreen();
    int dragX = getDragDistance(dragged.x, pressed.x, snapSize.width);
    int dragY = getDragDistance(dragged.y, pressed.y, snapSize.height);

    int locationX = location.x + dragX;
    int locationY = location.y + dragY;

    // Mouse dragged events are not generated for every pixel the mouse
    // is moved. Adjust the location to make sure we are still on a
    // snap value.

    while (locationX < edgeInsets.left) {
      locationX += snapSize.width;
    }

    while (locationY < edgeInsets.top) {
      locationY += snapSize.height;
    }

    Dimension d = getBoundingSize(destination);

    while (locationX + destination.getSize().width + edgeInsets.right > d.width) {
      locationX -= snapSize.width;
    }

    while (locationY + destination.getSize().height + edgeInsets.bottom > d.height) {
      locationY -= snapSize.height;
    }

    // Adjustments are finished, move the component

    destination.setLocation(locationX, locationY);
  }
  /**
   * Draws a simple 3d border for the given component.
   *
   * @param c The component to draw its border.
   * @param g The graphics context.
   * @param x The x coordinate of the top left corner.
   * @param y The y coordinate of the top left corner.
   * @param w The width.
   * @param h The height.
   */
  public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
    boolean isHorizontal = ((JToolBar) c).getOrientation() == HORIZONTAL;

    g.setColor(MetouiaLookAndFeel.getControlHighlight());
    if (isHorizontal) {
      g.drawLine(0, 0, w - 1, 0);
    } else {
      g.drawLine(0, 0, 0, h - 1);
    }

    g.setColor(MetouiaLookAndFeel.getControlShadow());
    if (isHorizontal) {
      g.drawLine(0, h - 1, w - 1, h - 1);
    } else {
      g.drawLine(w - 1, 0, w - 1, h - 1);
    }

    g.translate(x, y);

    if (((JToolBar) c).isFloatable()) {
      if (((JToolBar) c).getOrientation() == HORIZONTAL) {
        dots.setDotsArea(5, c.getSize().height - 4);
        if (c.getComponentOrientation().isLeftToRight()) {
          dots.paintIcon(c, g, 2, 2);
        } else {
          dots.paintIcon(c, g, c.getBounds().width - 12, 2);
        }
      } else {
        dots.setDotsArea(c.getSize().width - 4, 5);
        dots.paintIcon(c, g, 2, 2);
      }
    }

    g.translate(-x, -y);
  }
Esempio n. 3
0
  public static void positionComponent(int p, Component c, Rectangle d) {
    if (d == null) {
      Dimension s = Toolkit.getDefaultToolkit().getScreenSize();
      d = new Rectangle(0, 0, s != null ? s.width : 800, s != null ? s.height : 600);
    }

    switch (p) {
      case NORTH_WEST:
        c.setLocation(d.x, d.y);
        break;
      case NORTH:
        c.setLocation(d.x + (d.width - c.getSize().width) / 2, d.y);
        break;
      case NORTH_EAST:
        c.setLocation(d.x + (d.width - c.getSize().width), d.y);
        break;
      case WEST:
        c.setLocation(d.x, d.y + (d.height - c.getSize().height) / 2);
        break;
      case SOUTH_WEST:
        c.setLocation(d.x, d.y + (d.height - c.getSize().height));
        break;
      case EAST:
        c.setLocation(d.x + d.width - c.getSize().width, d.y + (d.height - c.getSize().height) / 2);
        break;
      case SOUTH_EAST:
        c.setLocation(
            d.x + (d.width - c.getSize().width), d.y + (d.height - c.getSize().height) - 30);
        break;
      case CENTER:
        c.setLocation(
            d.x + (d.width - c.getSize().width) / 2, d.y + (d.height - c.getSize().height) / 2);
        break;
    }
  }
Esempio n. 4
0
  /**
   * Returns the nth point in this graph (coordinates measured in pixels with respect to the canvas
   * provided).
   *
   * @throws IndexOutOfBoundsException if the graph does not have an nth point.
   */
  public Point getPoint(Component canvas, int n) {
    // subtract 1 so that everything actually fits *within* the canvas
    int width = canvas.getSize().width - 1;
    int height = canvas.getSize().height - 1;

    int x = xAxis.rescaleDataValue(this.getXValue(n), 0, width);
    int y = yAxis.rescaleDataValue(this.getYValue(n), height, 0);

    return new Point(x, y);
  }
Esempio n. 5
0
  /**
   * Setup the variables used to control the moving of the component:
   *
   * <p>source - the source component of the mouse event destination - the component that will
   * ultimately be moved pressed - the Point where the mouse was pressed in the destination
   * component coordinates.
   */
  @Override
  public void mousePressed(MouseEvent e) {
    source = e.getComponent();
    int width = source.getSize().width - dragInsets.left - dragInsets.right;
    int height = source.getSize().height - dragInsets.top - dragInsets.bottom;
    Rectangle r = new Rectangle(dragInsets.left, dragInsets.top, width, height);

    if (r.contains(e.getPoint())) {
      setupForDragging(e);
    }
  }
Esempio n. 6
0
 public static void center(Component c, Component rel) {
   if (rel != null) {
     c.setLocation(
         rel.getLocation().x + rel.getSize().width / 2 - c.getSize().width / 2,
         rel.getLocation().y + rel.getSize().height / 2 - c.getSize().height / 2);
   } else {
     c.setLocation(
         Toolkit.getDefaultToolkit().getScreenSize().width / 2 - c.getSize().width / 2,
         +Toolkit.getDefaultToolkit().getScreenSize().height / 2 - c.getSize().height / 2);
   }
 }
Esempio n. 7
0
  protected void enforceMinimumAndMaximumWidgetSizes(java.awt.Component component) {
    java.awt.Dimension size = component.getSize();
    boolean changed = false;

    // enforce minimum
    java.awt.Dimension minimumSize = component.getMinimumSize();
    if (size.width < minimumSize.width) {
      size.width = minimumSize.width;
      changed = true;
    }
    if (size.height < minimumSize.height) {
      size.height = minimumSize.height;
      changed = true;
    }

    // enforce maximum
    java.awt.Dimension maximumSize = component.getMaximumSize();
    if (maximumSize != null) {
      if (size.width > maximumSize.width && maximumSize.width > 0) {
        size.width = maximumSize.width;
        changed = true;
      }
      if (size.height > maximumSize.height && maximumSize.height > 0) {
        size.height = maximumSize.height;
        changed = true;
      }
    }

    if (changed) {
      component.setSize(size);
    }
  }
  public void setTarget(Component paramComponent, Insets paramInsets) {
    if (this.Target != null) {
      this.Target.removeComponentListener(this);
      if (this.TargetParent != null) {
        this.TargetParent.removeContainerListener(this);
        this.TargetParent = null;
      }
    }

    if (paramInsets == null) this.Space = DEFAULTINSETS;
    else this.Space = paramInsets;
    this.Target = paramComponent;

    setLocation(
        paramComponent.getLocation().x - this.Space.left,
        paramComponent.getLocation().y - this.Space.top);
    setSize(
        paramComponent.getSize().width + this.Space.left + this.Space.right,
        paramComponent.getSize().height + this.Space.top + this.Space.bottom);

    paramComponent.addComponentListener(this);

    if (this.Target.getParent() != null) {
      this.TargetParent = this.Target.getParent();
      this.TargetParent.addContainerListener(this);
    }
  }
Esempio n. 9
0
  public int print(Graphics g, PageFormat format, int page_index) throws PrinterException {
    if (page_index > 0) {
      return Printable.NO_SUCH_PAGE;
    }

    // get the bounds of the component
    Dimension dim = comp.getSize();
    double cHeight = dim.getHeight();
    double cWidth = dim.getWidth();

    // get the bounds of the printable area
    double pHeight = format.getImageableHeight();
    double pWidth = format.getImageableWidth();

    double pXStart = format.getImageableX();
    double pYStart = format.getImageableY();

    double xRatio = pWidth / cWidth;
    double yRatio = pHeight / cHeight;

    Graphics2D g2 = (Graphics2D) g;
    g2.translate(pXStart, pYStart);
    g2.scale(xRatio, yRatio);
    comp.paint(g2);

    return Printable.PAGE_EXISTS;
  }
 /**
  * Locates the given component on the screen's center.
  *
  * @param component the component to be centered
  */
 protected final void locateOnOpticalScreenCenter(Component component) {
   Dimension paneSize = component.getSize();
   Dimension screenSize = component.getToolkit().getScreenSize();
   component.setLocation(
       (screenSize.width - paneSize.width) / 2,
       (int) ((screenSize.height - paneSize.height) * 0.45));
 }
Esempio n. 11
0
  /**
   * We generally draw lines to/from the <I>center</I> of components; this method finds the center
   * of the argument's enclosing rectangle
   *
   * @return Point at the center of <CODE>c</CODE>
   * @param c The component whose center point we wish to determine
   */
  protected Point getCenterLocation(Component c) {

    Point p1 = new Point();
    Point p2 = new Point();

    // start with the relative location...
    c.getLocation(p1);
    // get to the middle of the fractionsLabel
    Dimension d = c.getSize();
    p1.x += d.width / 2;
    p1.y += d.height / 2;

    Component parent = c.getParent();
    // System.err.println("parent=" + parent);

    while (parent != null) {
      parent.getLocation(p2);
      p1.x += p2.x;
      p1.y += p2.y;

      if (STOP.equals(parent.getName())) break;

      parent = parent.getParent();
    }
    return p1;
  }
Esempio n. 12
0
  public static void captureScreen(Component Area) {

    // Find out where the user would like to save their screen shot
    String fileName = null;
    JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("Screen Shots", "png");
    chooser.setFileFilter(filter);
    int returnVal = chooser.showSaveDialog(null);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
      File saveFile = new File(chooser.getSelectedFile().getPath() + ".png");
      fileName = saveFile.toString();

      // Check to see if we will overwrite the file
      if (saveFile.exists()) {
        int overwrite =
            JOptionPane.showConfirmDialog(null, "File already exists, do you want to overwrite?");
        if (overwrite == JOptionPane.CANCEL_OPTION
            || overwrite == JOptionPane.CLOSED_OPTION
            || overwrite == JOptionPane.NO_OPTION) {
          return;
        }
      }
    }
    // If they didn't hit approve, return
    else {
      return;
    }

    // Determine the exact coordinates of the screen that is to be captured
    Dimension screenSize = Area.getSize();
    Rectangle screenRectangle = new Rectangle();
    screenRectangle.height = screenSize.height;
    screenRectangle.width = screenSize.width;
    screenRectangle.x = Area.getLocationOnScreen().x;
    screenRectangle.y = Area.getLocationOnScreen().y;

    // Here we have to make the GUI Thread sleep for 1/4 of a second
    // just to give the save dialog enough time to close off of the
    // screen. On slower computers they were capturing the screen
    // before the dialog was out of the way.
    try {
      Thread.currentThread();
      Thread.sleep(250);
    } catch (InterruptedException e1) {
      e1.printStackTrace();
    }

    // Attempt to capture the screen at the defined location.
    try {
      Robot robot = new Robot();
      BufferedImage image = robot.createScreenCapture(screenRectangle);
      ImageIO.write(image, "png", new File(fileName));
    } catch (AWTException e) {
      e.printStackTrace();
    } catch (IOException e) {
      JOptionPane.showMessageDialog(null, "Could not save screen shoot at: " + fileName);
      e.printStackTrace();
    }
  }
Esempio n. 13
0
  public static void center(Component c, Rectangle area) {
    Dimension size = c.getSize();

    int x = area.x + (area.width - size.width >> 1);
    int y = area.y + (area.height - size.height >> 1);

    c.setLocation(Math.max(0, x), Math.max(0, y));
  }
Esempio n. 14
0
 /**
  * takes a java component and generates an image out of it.
  *
  * @param c the component for which image needs to be generated
  * @return the generated image
  */
 public static BufferedImage captureComponentViewAsBufferedImage(Component c) {
   Dimension size = c.getSize();
   BufferedImage bufferedImage =
       new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
   Graphics bufferedGraphics = bufferedImage.createGraphics();
   c.paint(bufferedGraphics);
   return bufferedImage;
 }
Esempio n. 15
0
 private void test() throws Exception {
   if (left.getSize().width == 100) {
     System.out.println("Test passed");
   } else {
     throw new RuntimeException(
         "ScrollPaneLayout sometimes improperly " + "calculates the preferred layout size. ");
   }
 }
 private Rectangle getTransformedSize() {
   if (view != null) {
     Dimension viewSize = view.getSize();
     Rectangle viewRect = new Rectangle(viewSize);
     return at.createTransformedShape(viewRect).getBounds();
   }
   return new Rectangle(super.getPreferredSize());
 }
Esempio n. 17
0
 private void toggleViewState(final Component component, final boolean visible) {
   final Dimension size = getSize();
   size.height += component.getSize().height * (visible ? -1 : 1);
   component.setVisible(!visible);
   setMinimumSize(size);
   if ((getExtendedState() & Frame.MAXIMIZED_BOTH) != Frame.MAXIMIZED_BOTH) {
     pack();
   }
 }
  /*--------------------------------------------------------------------------*/
  private void moveComponents(
      Container target,
      int x,
      int y,
      int width,
      int height,
      int rowStart,
      int rowEnd,
      boolean ltr) {
    synchronized (target.getTreeLock()) {
      switch (newAlign) {
        case LEFT:
          x += ltr ? 0 : width;
          break;
        case CENTER:
          x += width / 2;
          break;
        case RIGHT:
          x += ltr ? width : 0;
          break;
        case LEADING:
          break;
        case TRAILING:
          x += width;
          break;
      }

      for (int i = rowStart; i < rowEnd; i++) {
        Component m = target.getComponent(i);

        if (m.isVisible()) {
          if (ltr) {
            m.setLocation(x, y + (height - m.getSize().height) / 2);
          } else {
            m.setLocation(
                target.getSize().width - x - m.getSize().width,
                y + (height - m.getSize().height) / 2);
          }

          x += m.getSize().width + hgap;
        }
      }
    }
  }
Esempio n. 19
0
 public static BufferedImage componentToImage(Component component) {
   Dimension componentSize = component.getSize();
   BufferedImage image =
       new BufferedImage(
           (int) componentSize.getWidth(),
           (int) componentSize.getHeight(),
           BufferedImage.TYPE_INT_RGB);
   component.paint(image.createGraphics());
   return image;
 }
Esempio n. 20
0
 public void paintIcon(Component c, Graphics g, int x, int y) {
   Dimension size = c.getSize();
   Graphics g2 = g.create(size.width / 2 - 5, size.height / 2 - 5, 10, 10);
   g2.setColor(Color.GRAY);
   g2.drawPolygon(xPoints, yPoints, 3);
   if (c.isEnabled()) {
     g2.setColor(Color.BLACK);
     g2.fillPolygon(xPoints, yPoints, 3);
   }
   g2.dispose();
 }
  public void layoutContainer(Container target) {
    //      System.out.println("Laying out container " + target);
    super.layoutContainer(target);
    if (free != null) { // what is free??
      Point loc = free.getLocation();
      Dimension sz = free.getSize();

      // System.out.println("Laying out free component " + free);
      free.setBounds(loc.x, loc.y, sz.width, sz.height);
    }
  }
  /** Center a component in the middle of the screen. */
  public static void centerComponent(java.awt.Component component) {
    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    java.awt.Dimension size = component.getSize();

    screenSize.height = screenSize.height / 2;
    screenSize.width = screenSize.width / 2;

    size.height = size.height / 2;
    size.width = size.width / 2;

    component.setLocation(screenSize.width - size.width, screenSize.height - size.height);
  }
Esempio n. 23
0
  /**
   * Gets the border insets.
   *
   * @param c the instance of Component
   * @return the instance of Insets
   */
  public Insets getBorderInsets(Component c) {
    Dimension currentComponent = c.getSize();

    if (currentComponent.equals(lastComponentSize)) {
      return insets;
    }

    insets = new Insets(thickness, thickness, thickness, thickness);
    lastComponentSize = currentComponent;

    return insets;
  }
 private static void center(final Component wind, final Rectangle rect) {
   if ((wind == null) || (rect == null)) {
     return;
   }
   final Dimension windSize = wind.getSize();
   final int x = ((rect.width - windSize.width) / 2) + rect.x;
   int y = ((rect.height - windSize.height) / 2) + rect.y;
   if (y < rect.y) {
     y = rect.y;
   }
   wind.setLocation(x, y);
 }
 /** Computes the declared size on the EventDispatcherThread. */
 public void run() {
   retval = null;
   try {
     final Component component = getComponent();
     if (component instanceof Window == false && isOwnPeerConnected() == false) {
       peerSupply.pack();
       contentPane.add(component);
     }
     retval = component.getSize();
   } finally {
     cleanUp();
   }
 }
    private void paintFocusBorders(boolean clean) {
      if (myCurrent != null) {
        Graphics currentFocusGraphics = myCurrent.getGraphics();
        if (currentFocusGraphics != null) {
          if (clean) {
            if (myCurrent.isDisplayable()) {
              myCurrent.repaint();
            }
          } else {
            currentFocusGraphics.setColor(myTemporary ? JBColor.ORANGE : JBColor.GREEN);
            UIUtil.drawDottedRectangle(
                currentFocusGraphics,
                1,
                1,
                myCurrent.getSize().width - 2,
                myCurrent.getSize().height - 2);
          }
        }
      }

      if (myPrevious != null) {
        Graphics previousFocusGraphics = myPrevious.getGraphics();
        if (previousFocusGraphics != null) {
          if (clean) {
            if (myPrevious.isDisplayable()) {
              myPrevious.repaint();
            }
          } else {
            previousFocusGraphics.setColor(JBColor.RED);
            UIUtil.drawDottedRectangle(
                previousFocusGraphics,
                1,
                1,
                myPrevious.getSize().width - 2,
                myPrevious.getSize().height - 2);
          }
        }
      }
    }
Esempio n. 27
0
  /* Constructor */
  public MovingObject(
      int radius,
      int x,
      int y,
      double vx,
      double vy,
      double mass,
      double k,
      Planet planet,
      Color color,
      AudioClip collision,
      Component parent,
      int panelHeight) {
    this.radius = radius;
    pos_x = x;
    pos_y = y;
    x_speed = vx;
    y_speed = vy;
    ballMass = mass;

    this.k = k; // arbitrary constant used for gravity

    this.planet = planet;

    this.parent = parent;

    this.panelHeight = panelHeight;

    x_leftout = radius;
    x_rightout = parent.getSize().width - radius;
    y_upout = radius + panelHeight;
    y_downout = parent.getSize().height - radius;

    this.color = color;

    img = ((Applet) parent).getImage(((Applet) parent).getCodeBase(), "earth.gif");

    this.collision = collision;
  }
Esempio n. 28
0
  public static void wallPaper(Component component, Graphics g, Image image) {
    Dimension compsize = component.getSize();
    Util.waitForImage(component, image);

    int patchW = image.getWidth(component);
    int patchH = image.getHeight(component);

    Assert.isTrue(patchW != -1 && patchH != -1);

    for (int r = 0; r < compsize.width; r += patchW) {
      for (int c = 0; c < compsize.height; c += patchH) g.drawImage(image, r, c, component);
    }
  }
Esempio n. 29
0
 /**
  * Center on screen
  *
  * @param thisComponent component to center
  */
 public static void center(final Component thisComponent) {
   final Dimension thisScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
   final Dimension thisComponentSize = thisComponent.getSize();
   if (thisComponentSize.height > thisScreenSize.height) {
     thisComponentSize.height = thisScreenSize.height;
   }
   if (thisComponentSize.width > thisScreenSize.width) {
     thisComponentSize.width = thisScreenSize.width;
   }
   thisComponent.setLocation(
       (thisScreenSize.width - thisComponentSize.width) / 2,
       (thisScreenSize.height - thisComponentSize.height) / 2);
 }
Esempio n. 30
0
 /**
  * Method to set the mouse dragged actions
  *
  * @param e
  */
 public void mouseDragged(MouseEvent e) {
   if (dragging) {
     Point p = e.getPoint();
     int x = p.x - offset.x;
     int y = p.y - offset.y;
     Dimension d = selectedComponent.getSize();
     selectedComponent.setBounds(x, y, d.width, d.height);
     if (!selected) {
       activePanel.repaint();
       selected = false;
     }
     glassPanel.repaint();
   }
 }