Java Code Examples for java.awt.Component#getX()

The following examples show how to use java.awt.Component#getX() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WindowsPopupMenuUI.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns offset for the text.
 * BasicMenuItemUI sets max text offset on the JPopupMenuUI.
 * @param c PopupMenu to return text offset for.
 * @return text offset for the component
 */
static int getTextOffset(JComponent c) {
    int rv = -1;
    Object maxTextOffset =
        c.getClientProperty(BASICMENUITEMUI_MAX_TEXT_OFFSET);
    if (maxTextOffset instanceof Integer) {
        /*
         * this is in JMenuItem coordinates.
         * Let's assume all the JMenuItem have the same offset along X.
         */
        rv = (Integer) maxTextOffset;
        int menuItemOffset = 0;
        Component component = c.getComponent(0);
        if (component != null) {
            menuItemOffset = component.getX();
        }
        rv += menuItemOffset;
    }
    return rv;
}
 
Example 2
Source File: WindowsPopupMenuUI.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns offset for the text.
 * BasicMenuItemUI sets max text offset on the JPopupMenuUI.
 * @param c PopupMenu to return text offset for.
 * @return text offset for the component
 */
static int getTextOffset(JComponent c) {
    int rv = -1;
    Object maxTextOffset =
        c.getClientProperty(BASICMENUITEMUI_MAX_TEXT_OFFSET);
    if (maxTextOffset instanceof Integer) {
        /*
         * this is in JMenuItem coordinates.
         * Let's assume all the JMenuItem have the same offset along X.
         */
        rv = (Integer) maxTextOffset;
        int menuItemOffset = 0;
        Component component = c.getComponent(0);
        if (component != null) {
            menuItemOffset = component.getX();
        }
        rv += menuItemOffset;
    }
    return rv;
}
 
Example 3
Source File: GuiHelper.java    From binnavi with Apache License 2.0 6 votes vote down vote up
/**
 * Centers the child component relative to it's parent component
 * 
 * @param parent
 * @param child
 * @param bStayOnScreen
 */
public static final void centerChildToParent(final Component parent, final Component child,
    final boolean bStayOnScreen) {
  int x = (parent.getX() + (parent.getWidth() / 2)) - (child.getWidth() / 2);
  int y = (parent.getY() + (parent.getHeight() / 2)) - (child.getHeight() / 2);
  if (bStayOnScreen) {
    final Toolkit tk = Toolkit.getDefaultToolkit();
    final Dimension ss = new Dimension(tk.getScreenSize());
    if ((x + child.getWidth()) > ss.getWidth()) {
      x = (int) (ss.getWidth() - child.getWidth());
    }
    if ((y + child.getHeight()) > ss.getHeight()) {
      y = (int) (ss.getHeight() - child.getHeight());
    }
    if (x < 0) {
      x = 0;
    }
    if (y < 0) {
      y = 0;
    }
  }
  child.setLocation(x, y);
}
 
Example 4
Source File: WindowsPopupMenuUI.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns offset for the text.
 * BasicMenuItemUI sets max text offset on the JPopupMenuUI.
 * @param c PopupMenu to return text offset for.
 * @return text offset for the component
 */
static int getTextOffset(JComponent c) {
    int rv = -1;
    Object maxTextOffset =
        c.getClientProperty(BASICMENUITEMUI_MAX_TEXT_OFFSET);
    if (maxTextOffset instanceof Integer) {
        /*
         * this is in JMenuItem coordinates.
         * Let's assume all the JMenuItem have the same offset along X.
         */
        rv = (Integer) maxTextOffset;
        int menuItemOffset = 0;
        Component component = c.getComponent(0);
        if (component != null) {
            menuItemOffset = component.getX();
        }
        rv += menuItemOffset;
    }
    return rv;
}
 
Example 5
Source File: FieldConfigBase.java    From sldeditor with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Adds the UI component to the field panel.
 *
 * @param component the component
 * @param buffer the buffer
 * @param width the width
 * @param height the height
 */
public void addUI(Component component, int buffer, int width, int height) {
    if (fieldPanel != null) {
        int lastX = -1;

        for (Component c : fieldPanel.getComponents()) {
            int x = c.getX() + c.getWidth();

            if (x > lastX) {
                lastX = x;
            }
        }
        component.setBounds(lastX + buffer, 0, width, height);
        fieldPanel.add(component);
    }
}
 
Example 6
Source File: WindowsPopupMenuUI.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns offset for the text.
 * BasicMenuItemUI sets max text offset on the JPopupMenuUI.
 * @param c PopupMenu to return text offset for.
 * @return text offset for the component
 */
static int getTextOffset(JComponent c) {
    int rv = -1;
    Object maxTextOffset =
        c.getClientProperty(BASICMENUITEMUI_MAX_TEXT_OFFSET);
    if (maxTextOffset instanceof Integer) {
        /*
         * this is in JMenuItem coordinates.
         * Let's assume all the JMenuItem have the same offset along X.
         */
        rv = (Integer) maxTextOffset;
        int menuItemOffset = 0;
        Component component = c.getComponent(0);
        if (component != null) {
            menuItemOffset = component.getX();
        }
        rv += menuItemOffset;
    }
    return rv;
}
 
Example 7
Source File: WindowsPopupMenuUI.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns offset for the text.
 * BasicMenuItemUI sets max text offset on the JPopupMenuUI.
 * @param c PopupMenu to return text offset for.
 * @return text offset for the component
 */
static int getTextOffset(JComponent c) {
    int rv = -1;
    Object maxTextOffset =
        c.getClientProperty(BASICMENUITEMUI_MAX_TEXT_OFFSET);
    if (maxTextOffset instanceof Integer) {
        /*
         * this is in JMenuItem coordinates.
         * Let's assume all the JMenuItem have the same offset along X.
         */
        rv = (Integer) maxTextOffset;
        int menuItemOffset = 0;
        Component component = c.getComponent(0);
        if (component != null) {
            menuItemOffset = component.getX();
        }
        rv += menuItemOffset;
    }
    return rv;
}
 
Example 8
Source File: WindowsPopupMenuUI.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns offset for the text.
 * BasicMenuItemUI sets max text offset on the JPopupMenuUI.
 * @param c PopupMenu to return text offset for.
 * @return text offset for the component
 */
static int getTextOffset(JComponent c) {
    int rv = -1;
    Object maxTextOffset =
        c.getClientProperty(BASICMENUITEMUI_MAX_TEXT_OFFSET);
    if (maxTextOffset instanceof Integer) {
        /*
         * this is in JMenuItem coordinates.
         * Let's assume all the JMenuItem have the same offset along X.
         */
        rv = (Integer) maxTextOffset;
        int menuItemOffset = 0;
        Component component = c.getComponent(0);
        if (component != null) {
            menuItemOffset = component.getX();
        }
        rv += menuItemOffset;
    }
    return rv;
}
 
Example 9
Source File: DropTargetLayer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private  void drawOpenSpotAtEndOfMenuBar(Graphics2D g2, JComponent mb) {
    Point mblocation = SwingUtilities.convertPoint(mb, new Point(0,0), this);
    if(mb.getComponentCount() > 0) {
        Component lastComp = mb.getComponent(mb.getComponentCount()-1);
        mblocation.x += lastComp.getX() + lastComp.getWidth();
    }
    g2.drawRect(mblocation.x+2, mblocation.y+2, mb.getHeight()-4, mb.getHeight()-4);
}
 
Example 10
Source File: UIUtilities.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Converts a {@link Rectangle} from on the screen to a position within the component.
 *
 * @param bounds    The rectangle to convert.
 * @param component The component the rectangle should be translated to.
 */
public static void convertRectangleFromScreen(Rectangle bounds, Component component) {
    while (component != null) {
        bounds.x -= component.getX();
        bounds.y -= component.getY();
        if (component instanceof Window) {
            break;
        }
        component = component.getParent();
    }
}
 
Example 11
Source File: UIUtilities.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Converts a {@link Point} in a component to its position on the screen.
 *
 * @param pt        The point to convert.
 * @param component The component the point originated in.
 */
public static void convertPointToScreen(Point pt, Component component) {
    while (component != null) {
        pt.x += component.getX();
        pt.y += component.getY();
        if (component instanceof Window) {
            break;
        }
        component = component.getParent();
    }
}
 
Example 12
Source File: ContextMenuHandler.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
public void showPopup(KeyEvent keyEvent) {
    if (isContextMenuOn()) {
        return;
    }
    Component component = keyEvent.getComponent();
    if (component instanceof JMenuItem && (!(component instanceof JMenu) || ((JMenu) component).isSelected())) {
        return;
    }
    Point point = new Point(component.getX() + component.getWidth() / 2, component.getY() + component.getHeight() / 2);
    showPopup(component, point);
}
 
Example 13
Source File: FlatInspector.java    From FlatLaf with Apache License 2.0 5 votes vote down vote up
private Component getDeepestComponentAt( Component parent, int x, int y ) {
	if( !parent.contains( x, y ) )
		return null;

	if( parent instanceof Container ) {
		for( Component child : ((Container)parent).getComponents() ) {
			if( child == null || !child.isVisible() )
				continue;

			int cx = x - child.getX();
			int cy = y - child.getY();
			Component c = (child instanceof Container)
				? getDeepestComponentAt( child, cx, cy )
				: child.getComponentAt( cx, cy );
			if( c == null || !c.isVisible() )
				continue;

			// ignore highlight figure and tooltip
			if( c == highlightFigure || c == tip )
				continue;

			// ignore glass pane
			if( c.getParent() instanceof JRootPane && c == ((JRootPane)c.getParent()).getGlassPane() )
				continue;

			if( "com.formdev.flatlaf.ui.FlatWindowResizer".equals( c.getClass().getName() ) )
				continue;

			return c;
		}
	}

	return parent;
}
 
Example 14
Source File: CompMover.java    From Math-Game with Apache License 2.0 4 votes vote down vote up
/**
 * If a card is selected, it can be dragged
 */
@Override
public void mousePressed(MouseEvent e) {

	selectedComponent = (Component) (e.getSource());
	// System.out.println(selectedComponent.getParent());
	// Point tempPoint = selectedComponent.getLocation();
	offset = e.getPoint();
	draggingCard = true;

	try {
		if (selectedComponent.getParent().equals(mathGame.getWorkspacePanel())) {

			mathGame.getWorkspacePanel().remove(selectedComponent);
			mathGame.getWorkspacePanel().revalidate();
			mathGame.getMasterPane().add(selectedComponent, new Integer(1));
			mathGame.getMasterPane().revalidate();
			mathGame.getMasterPane().repaint();

			// offset = selectedComponent.getLocationOnScreen();
			// selectedComponent.setBounds(MouseInfo.getPointerInfo().getLocation().x,
			// MouseInfo.getPointerInfo().getLocation().y,
			// cardHomes[1].getSize().width, cardHomes[1].getSize().height);
			// selectedComponent.setLocation(MouseInfo.getPointerInfo().getLocation());
			
			/*
			System.out.println(MouseInfo.getPointerInfo().getLocation());
			System.out.println(selectedComponent.getLocation());
			System.out.println(selectedComponent.getLocationOnScreen());
			System.out.println(tempPoint);
			*/
			selectedComponent.setLocation(-200, -200);

			// selectedComponent.setSize(cardHomes[1].getSize().width,
			// cardHomes[1].getSize().height);

		} else if (selectedComponent.getParent().equals(mathGame.getHoldPanel())) {
			int tempX = selectedComponent.getX();
			int tempY = selectedComponent.getLocationOnScreen().y;
			mathGame.getHoldPanel().remove(selectedComponent);
			mathGame.getHoldPanel().revalidate();
			mathGame.getMasterPane().add(selectedComponent, new Integer(1));
			mathGame.getMasterPane().revalidate();
			mathGame.getMasterPane().repaint();

			selectedComponent.setLocation(tempX, tempY);
		}
		/*
		else { System.out.println("normal workpanel:"+workPanel);
		System.out.println("parent:"+selectedComponent.getParent()); }
		*/

	} catch (Exception ex) {
		System.out.println("error removing from panel");
		ex.printStackTrace();
	}

}
 
Example 15
Source File: GraphNodePort.java    From pdfxtk with Apache License 2.0 4 votes vote down vote up
public void paint(Component c, Graphics g, boolean selected) {
  int ix = (int)(x * c.getWidth())  + c.getX();
  int iy = (int)(y * c.getHeight()) + c.getY();
  (selected ? SELECTED : DESELECTED).paintIcon(c, g, ix - 2, iy - 2);
}
 
Example 16
Source File: FlatSpinnerUI.java    From FlatLaf with Apache License 2.0 4 votes vote down vote up
@Override
public void update( Graphics g, JComponent c ) {
	float focusWidth = FlatUIUtils.getBorderFocusWidth( c );
	float arc = FlatUIUtils.getBorderArc( c );

	// fill background if opaque to avoid garbage if user sets opaque to true
	if( c.isOpaque() && (focusWidth > 0 || arc > 0) )
		FlatUIUtils.paintParentBackground( g, c );

	Graphics2D g2 = (Graphics2D) g;
	FlatUIUtils.setRenderingHints( g2 );

	int width = c.getWidth();
	int height = c.getHeight();
	Component nextButton = getHandler().nextButton;
	int arrowX = nextButton.getX();
	int arrowWidth = nextButton.getWidth();
	boolean paintButton = !"none".equals( buttonStyle );
	boolean enabled = spinner.isEnabled();
	boolean isLeftToRight = spinner.getComponentOrientation().isLeftToRight();

	// paint background
	g2.setColor( getBackground( enabled ) );
	FlatUIUtils.paintComponentBackground( g2, 0, 0, width, height, focusWidth, arc );

	// paint arrow buttons background
	if( paintButton && enabled ) {
		g2.setColor( buttonBackground );
		Shape oldClip = g2.getClip();
		if( isLeftToRight )
			g2.clipRect( arrowX, 0, width - arrowX, height );
		else
			g2.clipRect( 0, 0, arrowX + arrowWidth, height );
		FlatUIUtils.paintComponentBackground( g2, 0, 0, width, height, focusWidth, arc );
		g2.setClip( oldClip );
	}

	// paint vertical line between value and arrow buttons
	if( paintButton ) {
		g2.setColor( enabled ? borderColor : disabledBorderColor );
		float lw = scale( 1f );
		float lx = isLeftToRight ? arrowX : arrowX + arrowWidth - lw;
		g2.fill( new Rectangle2D.Float( lx, focusWidth, lw, height - 1 - (focusWidth * 2) ) );
	}

	paint( g, c );
}
 
Example 17
Source File: GraphNodePort.java    From pdfxtk with Apache License 2.0 4 votes vote down vote up
public Point getLocation(Component c) {
  return new Point((int)(x * c.getWidth())  + c.getX(), (int)(y * c.getHeight()) + c.getY());
}
 
Example 18
Source File: FlatInspector.java    From FlatLaf with Apache License 2.0 4 votes vote down vote up
private String buildToolTipText( Component c ) {
	String name = c.getClass().getName();
	name = name.substring( name.lastIndexOf( '.' ) + 1 );

	String text =
		"Class: " + name + " (" + c.getClass().getPackage().getName() + ")\n" +
		"Size: " + c.getWidth() + ',' + c.getHeight() + "  @ " + c.getX() + ',' + c.getY() + '\n';

	if( c instanceof Container )
		text += "Insets: " + toString( ((Container)c).getInsets() ) + '\n';

	Insets margin = null;
	if( c instanceof AbstractButton )
		margin = ((AbstractButton) c).getMargin();
	else if( c instanceof JTextComponent )
		margin = ((JTextComponent) c).getMargin();
	else if( c instanceof JMenuBar )
		margin = ((JMenuBar) c).getMargin();
	else if( c instanceof JToolBar )
		margin = ((JToolBar) c).getMargin();

	if( margin != null )
		text += "Margin: " + toString( margin ) + '\n';

	Dimension prefSize = c.getPreferredSize();
	Dimension minSize = c.getMinimumSize();
	Dimension maxSize = c.getMaximumSize();
	text += "Pref size: " + prefSize.width + ',' + prefSize.height + '\n' +
		"Min size: " + minSize.width + ',' + minSize.height + '\n' +
		"Max size: " + maxSize.width + ',' + maxSize.height + '\n';

	if( c instanceof JComponent )
		text += "Border: " + toString( ((JComponent)c).getBorder() ) + '\n';

	text += "Background: " + toString( c.getBackground() ) + '\n' +
		"Foreground: " + toString( c.getForeground() ) + '\n' +
		"Font: " + toString( c.getFont() ) + '\n';

	if( c instanceof JComponent ) {
		try {
			Field f = JComponent.class.getDeclaredField( "ui" );
			f.setAccessible( true );
			Object ui = f.get( c );
			text += "UI: " + (ui != null ? ui.getClass().getName() : "null") + '\n';
		} catch( NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex ) {
			// ignore
		}
	}

	if( c instanceof Container ) {
		LayoutManager layout = ((Container)c).getLayout();
		if( layout != null )
			text += "Layout: " + layout.getClass().getName() + '\n';
	}

	text += "Enabled: " + c.isEnabled() + '\n';
	text += "Opaque: " + c.isOpaque() + (c instanceof JComponent &&
		FlatUIUtils.hasOpaqueBeenExplicitlySet( (JComponent) c ) ? " EXPLICIT" : "") + '\n';
	if( c instanceof AbstractButton )
		text += "ContentAreaFilled: " + ((AbstractButton)c).isContentAreaFilled() + '\n';
	text += "Focusable: " + c.isFocusable() + '\n';
	text += "Left-to-right: " + c.getComponentOrientation().isLeftToRight() + '\n';
	text += "Parent: " + (c.getParent() != null ? c.getParent().getClass().getName() : "null");

	if( inspectParentLevel > 0 )
		text += "\n\nParent level: " + inspectParentLevel;

	if( inspectParentLevel > 0 )
		text += "\n(press Ctrl/Shift to increase/decrease level)";
	else
		text += "\n\n(press Ctrl key to inspect parent)";

	return text;
}
 
Example 19
Source File: GraphPanelEditor.java    From pdfxtk with Apache License 2.0 votes vote down vote up
int card(Component c) {return c.getX() + c.getWidth();} 
Example 20
Source File: GraphPanelEditor.java    From pdfxtk with Apache License 2.0 votes vote down vote up
int card(Component c) {return c.getX();}