Example usage for java.awt Component getWidth

List of usage examples for java.awt Component getWidth

Introduction

In this page you can find the example usage for java.awt Component getWidth.

Prototype

public int getWidth() 

Source Link

Document

Returns the current width of this component.

Usage

From source file:Main.java

public static void addMiddleButtonDragSupport(Component targetComponent) {
    MouseInputAdapter mia = new MouseInputAdapter() {
        int m_XDifference, m_YDifference;
        boolean m_dragging = false;

        public void mouseDragged(MouseEvent e) {
            if (!m_dragging)
                return;

            Component target = e.getComponent();
            Container c = target.getParent();
            if (c instanceof JViewport) {
                JViewport jv = (JViewport) c;
                Point p = jv.getViewPosition();
                int newX = p.x - (e.getX() - m_XDifference);
                int newY = p.y - (e.getY() - m_YDifference);

                int maxX = target.getWidth() - jv.getWidth();
                int maxY = target.getHeight() - jv.getHeight();
                if (newX < 0)
                    newX = 0;//from   w  w  w.  java2 s. co m
                if (newX > maxX)
                    newX = maxX;
                if (newY < 0)
                    newY = 0;
                if (newY > maxY)
                    newY = maxY;

                jv.setViewPosition(new Point(newX, newY));
            }
        }

        Cursor oldCursor;

        public void mousePressed(MouseEvent e) {
            if (SwingUtilities.isMiddleMouseButton(e)) {
                m_dragging = true;
                oldCursor = e.getComponent().getCursor();
                e.getComponent().setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
                m_XDifference = e.getX();
                m_YDifference = e.getY();
            }
        }

        public void mouseReleased(MouseEvent e) {
            if (m_dragging) {
                e.getComponent().setCursor(oldCursor);
                m_dragging = false;
            }
        }
    };
    targetComponent.addMouseMotionListener(mia);
    targetComponent.addMouseListener(mia);
}

From source file:no.geosoft.cc.io.GifEncoder.java

/**
 * Write AWT/Swing component to GIF file.
 * /*from   w  ww.j  a  v a  2  s.c om*/
 * @param image  Image to write.
 * @param file   File to erite to.
 */
public static void writeFile(Component component, File file) throws AWTException, IOException {
    Image image = component.createImage(component.getWidth(), component.getHeight());
    Graphics graphics = image.getGraphics();
    component.printAll(graphics);

    GifEncoder.writeFile(image, file);
}

From source file:Main.java

public static void centralizeComponent(Component component, Component otherComponent) {

    Dimension othersDimension = null;
    Point othersLocation = null;//  ww  w  . j  a v  a 2s .co m
    if (otherComponent == null || !otherComponent.isVisible()) {
        othersDimension = Toolkit.getDefaultToolkit().getScreenSize();
        othersLocation = new Point(0, 0);
    } else {
        othersDimension = otherComponent.getSize();
        othersLocation = otherComponent.getLocation();
    }
    Point centerPoint = new Point(
            (int) Math.round(othersDimension.width / HALF - component.getWidth() / HALF) + othersLocation.x,
            (int) Math.round(othersDimension.height / HALF - component.getHeight() / HALF) + othersLocation.y);
    component.setLocation(centerPoint);
}

From source file:com.igormaznitsa.ideamindmap.utils.IdeaUtils.java

public static void showPopup(@Nonnull final String text, @Nonnull final MessageType type) {
    SwingUtils.safeSwing(new Runnable() {
        @Override/*from   w  ww .  j  av a  2  s  .  c  om*/
        public void run() {
            final JBPopupFactory factory = JBPopupFactory.getInstance();
            final BalloonBuilder builder = factory
                    .createHtmlTextBalloonBuilder(StringEscapeUtils.escapeHtml(text), type, null);
            final Balloon balloon = builder.createBalloon();
            balloon.setAnimationEnabled(true);
            final Component frame = WindowManager.getInstance().findVisibleFrame();
            if (frame != null)
                balloon.show(new RelativePoint(frame, new Point(frame.getWidth(), frame.getHeight())),
                        Balloon.Position.below);
        }
    });
}

From source file:Main.java

public static void setCenter(Component component, Component component1) {
    Point point;//from   w ww  .j av a 2  s.  co m
    Dimension dimension;
    if (component != null) {
        point = component.getLocation();
        dimension = component.getSize();
    } else {
        dimension = Toolkit.getDefaultToolkit().getScreenSize();
        point = new Point(0, 0);
    }
    Dimension dimension1 = Toolkit.getDefaultToolkit().getScreenSize();
    point.setLocation((double) point.x + dimension.getWidth() / 2D,
            (double) point.y + dimension.getHeight() / 2D);
    Point point1 = new Point(point.x - component1.getWidth() / 2, point.y - component1.getHeight() / 2);
    if (point1.y < 0)
        point1.y = 0;
    if (point1.x < 0)
        point1.y = 0;
    if (point1.x + component1.getWidth() > dimension1.width)
        point1.x = dimension1.width - component1.getWidth();
    if (point1.y + component1.getHeight() > dimension1.height)
        point1.y = dimension1.height - component1.getHeight();
    component1.setLocation(point1);
}

From source file:Main.java

public static void centerComponent(Component relativeTo, Component toCenter) {
    if (relativeTo == null) {
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension screenSize = tk.getScreenSize();
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;
        toCenter.setLocation((screenWidth / 2) - (toCenter.getSize().width / 2),
                (screenHeight / 2) - (toCenter.getSize().height / 2));
    } else {/*w w  w .  j a  v a  2s  . c  om*/
        Point loc = relativeTo.getLocationOnScreen();
        Rectangle bounds = relativeTo.getBounds();
        toCenter.setLocation((int) (loc.x + bounds.getWidth() / 2) - (toCenter.getWidth() / 2),
                (int) (loc.y + bounds.getHeight() / 2) - (toCenter.getHeight() / 2));

    }
}

From source file:com.simiacryptus.util.Util.java

/**
 * To image buffered image./*from  ww w.ja va 2s  .c o m*/
 *
 * @param component the component
 * @return the buffered image
 */
public static BufferedImage toImage(@javax.annotation.Nonnull final Component component) {
    try {
        com.simiacryptus.util.Util.layout(component);
        @javax.annotation.Nonnull
        final BufferedImage img = new BufferedImage(component.getWidth(), component.getHeight(),
                BufferedImage.TYPE_INT_ARGB_PRE);
        final Graphics2D g = img.createGraphics();
        g.setColor(component.getForeground());
        g.setFont(component.getFont());
        component.print(g);
        return img;
    } catch (@javax.annotation.Nonnull final Exception e) {
        return null;
    }
}

From source file:pcgen.gui2.tools.Utility.java

/**
 * Centres the dialog over the component ensuring that the dialog will be
 * within the usable area of the screen (i.e. excluding native task bars,
 * menus bars etc)./*w  w w.  jav a  2  s.  c  o m*/
 *
 * @param parent The component over which the dialog should be centred.
 * @param dialog The dialog to be positioned.
 */
public static void setComponentRelativeLocation(Component parent, Component dialog) {
    // First make sure it is not too big
    resizeComponentToScreen(dialog);

    // Get the maximum window size to account for taskbars etc
    Rectangle screenBounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
    Point centreOfParent = new Point(parent.getWidth() / 2, parent.getHeight() / 2);
    SwingUtilities.convertPointToScreen(centreOfParent, parent);
    // Default to centre of parent
    Point location = new Point(centreOfParent.x - (dialog.getWidth() / 2),
            centreOfParent.y - (dialog.getHeight() / 2));
    // Adjust so it fits on the screen
    if ((location.x + dialog.getWidth()) > (screenBounds.width + screenBounds.x)) {
        location.x -= (location.x + dialog.getWidth()) - (screenBounds.width + screenBounds.x);
    }
    if (location.x < screenBounds.x) {
        location.x = screenBounds.x;
    }
    if ((location.y + dialog.getHeight()) > (screenBounds.height + screenBounds.y)) {
        location.y -= (location.y + dialog.getHeight()) - (screenBounds.height + screenBounds.y);
    }
    if (location.y < screenBounds.y) {
        location.y = screenBounds.y;
    }
    dialog.setLocation(location);
}

From source file:org.eclipse.wb.internal.swing.utils.SwingImageUtils.java

/**
 * @return the {@link java.awt.Image} of given {@link Component}. Must be called from AWT disp
 *         thread./*  www. j  av  a  2s .c  o  m*/
 */
static java.awt.Image createComponentShotAWT(final Component component) throws Exception {
    Assert.isNotNull(component);
    // prepare sizes
    final int componentWidth = component.getWidth();
    final int componentHeight = component.getHeight();
    final int imageWidth = Math.max(1, componentWidth);
    final int imageHeight = Math.max(1, componentHeight);
    // prepare empty image
    final BufferedImage componentImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
    // If actual size on component is zero, then we are done.
    if (componentWidth == 0 || componentHeight == 0) {
        return componentImage;
    }
    // some components like JLabel are transparent by default and printing it separately gives
    // bad results like invalid background color. The workaround is to set opacity property and
    // restore old value back when all done.
    ComponentShotConfigurator shotConfigurator = new ComponentShotConfigurator(component);
    try {
        // Linux only: it seems that printAll() should be invoked in AWT dispatch thread 
        // to prevent deadlocks between main thread and AWT event queue.
        // See also SwingUtils.invokeLaterAndWait().
        runInDispatchThread(new Runnable() {
            public void run() {
                component.printAll(componentImage.getGraphics());
            }
        });
    } finally {
        shotConfigurator.dispose();
    }
    // convert into SWT image
    return componentImage;
}

From source file:io.github.tavernaextras.biocatalogue.model.Util.java

/**
 * Makes sure that one component (for example, a window) is centered horizontally and vertically
 * relatively to the other component. This is achieved by aligning centers of the two components.
 * //from   w w  w . ja  va 2 s . co m
 * This method can be used even if the 'dependentComponent' is larger than the 'mainComponent'. In
 * this case it is probably only useful for centering windows against each other rather than
 * components inside a container.
 * 
 * Method also makes sure that the dependent component will not be placed above the screen's upper
 * edge and to the left of the left edge.
 */
public static void centerComponentWithinAnother(Component mainComponent, Component dependentComponent) {
    int iMainComponentCenterX = (int) Math
            .round(mainComponent.getLocationOnScreen().getX() + (mainComponent.getWidth() / 2));
    int iPosX = iMainComponentCenterX - (dependentComponent.getWidth() / 2);
    if (iPosX < 0)
        iPosX = 0;

    int iMainComponentCenterY = (int) Math
            .round(mainComponent.getLocationOnScreen().getY() + (mainComponent.getHeight() / 2));
    int iPosY = iMainComponentCenterY - (dependentComponent.getHeight() / 2);
    if (iPosY < 0)
        iPosY = 0;

    dependentComponent.setLocation(iPosX, iPosY);
}