private final void printComp(final Component comp, final int deep) { final Rectangle rect = comp.getBounds(); if (L.isInWorkshop) { System.out.println( comp.getClass().getName() + deep + ", x : " + rect.x + ", y : " + rect.y + ", w : " + rect.width + ", h : " + rect.height); } if (comp instanceof Container) { final Container container = (Container) comp; final int count = container.getComponentCount(); for (int i = 0; i < count; i++) { printComp(container.getComponent(i), deep + 1); } } else if (comp instanceof JScrollPane) { if (L.isInWorkshop) { System.out.println("------This is JScrollPane-----"); } printComp(((JScrollPane) comp).getViewport().getView(), deep); } }
public void focusGained(FocusEvent evt) { Component focusedComponent = evt.getComponent(); Component parent = focusedComponent.getParent(); if (parent instanceof JPanel) { ((JPanel) parent).scrollRectToVisible(focusedComponent.getBounds(null)); } }
// TODO replace SwingConstant with enum other non-int // TODO this method places the box outside of the origin component // that continues the implementation approach that Amy used. I // think it would be a useful poll to determine whether the box // should be place inside or outside of the origin (by default). public void showOnGlassPane( Container glassPane, Component origin, int offsetX, int offsetY, int positionHint) { Rectangle r = SwingUtilities.convertRectangle(origin, origin.getBounds(), glassPane); Dimension d = getPreferredSize(); int originX = offsetX + r.x; int originY = offsetY + r.y; switch (positionHint) { case SwingConstants.TOP: originX += (r.width - d.width) / 2; originY -= d.height; break; case SwingConstants.BOTTOM: originX += (r.width - d.width) / 2; originY += r.height; break; case SwingConstants.LEFT: originX -= d.width; originY += (r.height - d.height) / 2; break; case SwingConstants.RIGHT: originX += r.width; originY += (r.height - d.height) / 2; break; case SwingConstants.CENTER: originX += (r.width - d.width) / 2; originY += (r.height - d.height) / 2; break; default: throw new IllegalArgumentException("inavlid position hint"); } showOnGlassPane(glassPane, originX, originY); }
/** * method to negotiate draganddrop when mouse is pressed * * @param e */ public void mousePressed(MouseEvent e) { /** Set up click point and set ActivePanel */ Point p = e.getPoint(); System.out.println(p); if (!setActivePanel(p)) return; /** record where the initial click occurred */ OriP = activePanel; /** Check whether click was over an image label */ selectedComponent = getImageLabel(p); if (selectedComponent == null) return; /** Check whether click was over waste box */ if (selectedComponent.getName().equals("WasteBox")) return; /** * remove selected component from active panel add it to the glass panel set the offset and * original position */ Rectangle labelR = selectedComponent.getBounds(); Rectangle panelR = activePanel.getBounds(); // if(labelR.contains(p.x - panelR.x, p.y - panelR.y)) // { activePanel.remove(selectedComponent); selected = true; glassPanel.add(selectedComponent); offset.x = p.x - labelR.x - panelR.x; offset.y = p.y - labelR.y - panelR.y; dragging = true; Original = labelR.getLocation(); // } }
@Override public void mousePressed(MouseEvent e) { // The mouseMoved event continually updates this variable if (direction == 0) return; // Setup for resizing. All future dragging calculations are done based // on the original bounds of the component and mouse pressed location. resizing = true; Component source = e.getComponent(); pressed = e.getPoint(); SwingUtilities.convertPointToScreen(pressed, source); bounds = source.getBounds(); // Making sure autoscrolls is false will allow for smoother resizing // of components if (source instanceof JComponent) { JComponent jc = (JComponent) source; autoscrolls = jc.getAutoscrolls(); jc.setAutoscrolls(false); } }
private boolean is(boolean first) { Container parent = getParent(); if (parent == null) return false; int max = first ? Integer.MAX_VALUE : 0; ToolWindowAnchor anchor = getAnchor(); Component c = null; int count = parent.getComponentCount(); for (int i = 0; i < count; i++) { Component component = parent.getComponent(i); if (!component.isVisible()) continue; Rectangle r = component.getBounds(); if (anchor == ToolWindowAnchor.LEFT || anchor == ToolWindowAnchor.RIGHT) { if (first && (max > r.y) || (!first && max < r.y)) { max = r.y; c = component; } } else { if (first && (max > r.x) || (!first && max < r.x)) { max = r.x; c = component; } } } return c == this; }
public void actionPerformed(ActionEvent e) { int selIndexBefore = getSelectedIndex(); myDefaultAction.actionPerformed(e); int selIndexCurrent = getSelectedIndex(); if (selIndexBefore != selIndexCurrent) { return; } if (myFocusNext && selIndexCurrent == 0) { return; } KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager(); Container container = kfm.getCurrentFocusCycleRoot(); FocusTraversalPolicy policy = container.getFocusTraversalPolicy(); if (policy == null) { policy = kfm.getDefaultFocusTraversalPolicy(); } Component next = myFocusNext ? policy.getComponentAfter(container, PaletteItemsComponent.this) : policy.getComponentBefore(container, PaletteItemsComponent.this); if (next instanceof PaletteGroupComponent) { clearSelection(); next.requestFocus(); ((PaletteGroupComponent) next).scrollRectToVisible(next.getBounds()); } }
@Override public void run(Component c) { final Rectangle r = c.getBounds(); r.width -= 64; r.height -= 64; c.setBounds(r); }
/** * 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); }
/** * Paint to an offscreen graphic, e.g. a graphic for an image or svg file. * * @param g * @param rect */ public void paintOffscreen(Graphics2D g, Rectangle rect) { // Get the components of the sort by X position. Component[] components = getComponents(); Arrays.sort( components, new Comparator<Component>() { public int compare(Component component, Component component1) { return component.getX() - component1.getX(); } }); for (Component c : this.getComponents()) { if (c instanceof DataPanel) { Graphics2D g2d = (Graphics2D) g.create(); Rectangle clipRect = new Rectangle(c.getBounds()); clipRect.height = rect.height; g2d.setClip(clipRect); g2d.translate(c.getX(), 0); ((DataPanel) c).paintOffscreen(g2d, rect); } } // super.paintBorder(g); }
/** @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; }
public CreInvChecker(Component parent) { typeButtons = new JCheckBox[CHECKTYPES.length]; JPanel boxPanel = new JPanel(new GridLayout(0, 1)); for (int i = 0; i < typeButtons.length; i++) { typeButtons[i] = new JCheckBox(CHECKTYPES[i], true); boxPanel.add(typeButtons[i]); } bstart.setMnemonic('s'); bcancel.setMnemonic('c'); bstart.addActionListener(this); bcancel.addActionListener(this); selectframe.getRootPane().setDefaultButton(bstart); selectframe.setIconImage(Icons.getIcon("Find16.gif").getImage()); boxPanel.setBorder(BorderFactory.createTitledBorder("Select test to check:")); JPanel bpanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); bpanel.add(bstart); bpanel.add(bcancel); JPanel mainpanel = new JPanel(new BorderLayout()); mainpanel.add(boxPanel, BorderLayout.CENTER); mainpanel.add(bpanel, BorderLayout.SOUTH); mainpanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); JPanel pane = (JPanel) selectframe.getContentPane(); pane.setLayout(new BorderLayout()); pane.add(mainpanel, BorderLayout.CENTER); selectframe.pack(); Center.center(selectframe, parent.getBounds()); selectframe.setVisible(true); }
private static void choosePaint(Component target, Graphics2D g, boolean paintOffscreen) { log.debug("Painting to target " + target + " , offscreen " + paintOffscreen); if (paintOffscreen) { ((Paintable) target).paintOffscreen(g, target.getBounds()); } else { target.paintAll(g); } }
public void mousePressed(MouseEvent e) { if (e.getModifiers() == modifiers) { Rectangle rect = view.getBounds(); viewOrigin = new Point2D.Double(rect.x + rect.width / 2d, rect.y + rect.height / 2d); pStartScale = e.getPoint(); } }
protected Rectangle getTextComponentBound() { Rectangle ownerRec = myOwner == null ? new Rectangle(0, 0, 0, 0) : myOwner.getBounds(); Dimension size = myComponent.getPreferredSize(); int x = (ownerRec.width - size.width) / 2; int y = (ownerRec.height - size.height) / 3; return new Rectangle(x, y, size.width, size.height); }
/* * Create a BufferedImage for AWT components. * * @param component AWT component to create image from * @param fileName name of file to be created or null * @return image the image for the given region * @exception AWTException see Robot class constructors * @exception IOException if an error occurs during writing */ public static BufferedImage createImage(Component component, String fileName) throws AWTException, IOException { Point p = new Point(0, 0); SwingUtilities.convertPointToScreen(p, component); Rectangle region = component.getBounds(); region.x = p.x; region.y = p.y; return ScreenCapture.createImage(region, fileName); }
@Override public void doLayout() { int x = 0; for (int i = 0; i < getComponentCount(); i++) { final Component each = getComponent(i); final Dimension eachSize = each.getPreferredSize(); each.setBounds(x, 0, eachSize.width, getHeight()); x += each.getBounds().width; } }
@Override protected void paintComponent(Graphics g) { int dy = 0; for (int i = 0; i < getComponentCount(); i++) { Component child = getComponent(i); if (child instanceof AbstractRibbonBand) { dy = child.getBounds().y; break; } } SubstanceRibbonBandUI.paintRibbonBandBackground(g, this, 0.0f, dy); }
@Override public void doLayout() { super.doLayout(); Component child = getComponentCount() == 1 ? getComponent(0) : null; if (child instanceof JBTabsPresentation) { if (!((JBTabsPresentation) child).isHideTabs()) { Rectangle bounds = child.getBounds(); bounds.y--; bounds.height++; child.setBounds(bounds); } } }
public void setWindowBounds(Rectangle bounds, boolean screenCoordinates) { Rectangle valid = getStation().getBoundaryRestriction().check(this, bounds); if (valid != null) { bounds = valid; } if (!window.getBounds().equals(bounds)) { window.setBounds(bounds); invalidate(); validate(); } }
public void paintComponent(Graphics gg) { Graphics2D g = (Graphics2D) gg; super.paintComponent(g); if (zoom != 0) { // Paint page borders for (int i = 0; i < getComponentCount(); i++) { Component c = getComponent(i); if (c.isVisible()) { paintPageBorder(g, c.getBounds()); } } } }
/** * Returns the component in the currently selected path which contains sourcePoint. * * @param source The component in whose coordinate space sourcePoint is given * @param sourcePoint The point which is being tested * @return The component in the currently selected path which contains sourcePoint (relative to * the source component's coordinate space. If sourcePoint is not inside a component on the * currently selected path, null is returned. */ public Component componentForPoint(Component source, Point sourcePoint) { int screenX, screenY; Point p = sourcePoint; int i, c, j, d; Component mc; Rectangle r2; int cWidth, cHeight; MenuElement menuElement; MenuElement subElements[]; Vector<MenuElement> tmp; int selectionSize; SwingUtilities.convertPointToScreen(p, source); screenX = p.x; screenY = p.y; tmp = (Vector<MenuElement>) selection.clone(); selectionSize = tmp.size(); for (i = selectionSize - 1; i >= 0; i--) { menuElement = (MenuElement) tmp.elementAt(i); subElements = menuElement.getSubElements(); for (j = 0, d = subElements.length; j < d; j++) { if (subElements[j] == null) continue; mc = subElements[j].getComponent(); if (!mc.isShowing()) continue; if (mc instanceof JComponent) { cWidth = mc.getWidth(); cHeight = mc.getHeight(); } else { r2 = mc.getBounds(); cWidth = r2.width; cHeight = r2.height; } p.x = screenX; p.y = screenY; SwingUtilities.convertPointFromScreen(p, mc); /** * Return the deepest component on the selection path in whose bounds the event's point * occurs */ if (p.x >= 0 && p.x < cWidth && p.y >= 0 && p.y < cHeight) { return mc; } } } return null; }
public void hide() { Component component = getComponent(); if (component != null) { Container parent = component.getParent(); if (parent != null) { Rectangle bounds = component.getBounds(); parent.remove(component); parent.repaint(bounds.x, bounds.y, bounds.width, bounds.height); } } owner = null; }
/** @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent) */ public void propertyChange(PropertyChangeEvent evt) { if (!isEditable()) { return; } Component focusedValueEntryPanel = null; Component newFocusOwner = (Component) evt.getNewValue(); while (newFocusOwner != null) { if (newFocusOwner instanceof ValueEntryPanel) { focusedValueEntryPanel = newFocusOwner; } if (newFocusOwner instanceof DataConstructorEditorPanel && ((DataConstructorEditorPanel) newFocusOwner).getParentEditor() == AlgebraicValueEditor.this) { if (newFocusOwner == currentFocusedPanel) { // If there is a newly focused value entry panel, then scroll into view. if (focusedValueEntryPanel != null) { Rectangle bounds = SwingUtilities.convertRectangle( focusedValueEntryPanel.getParent(), focusedValueEntryPanel.getBounds(), currentFocusedPanel); currentFocusedPanel.scrollRectToVisible(bounds); } return; } if (currentFocusedPanel != null) { currentFocusedPanel.setFocusedLook(false); } currentFocusedPanel = (DataConstructorEditorPanel) newFocusOwner; currentFocusedPanel.setFocusedLook(true); return; } newFocusOwner = newFocusOwner.getParent(); } }
private void resizeArea() { Dimension area = new Dimension(0, 0); Dimension size = getPreferredSize(); for (Component comp : getComponents()) { Rectangle r = comp.getBounds(); if (r.x + r.width > area.width) { area.width = r.x + r.width; } if (r.y + r.height > area.height) { area.height = r.y + r.height; } } if (size.height != area.height || size.width != area.width) { setPreferredSize(area); } }
/** * Calculates the area in which this panel actually paints stuff. * * @param g the graphics context that will paint the panel, not <code>null</code> * @return the boundaries, never <code>null</code> */ public Rectangle getVisibleBoundaries(Graphics g) { Rectangle result = null; for (GraphPaintable paintable : paintables) { Rectangle boundaries = paintable.getVisibleBoundaries(g); result = union(result, boundaries); } for (Component child : canvas.getComponents()) { Rectangle boundaries = child.getBounds(); result = union(result, boundaries); } if (result == null) { result = new Rectangle(0, 0, 50, 50); } return result; }
// implements javax.swing.DesktopManager public void beginDraggingFrame(JComponent f) { setupDragMode(f); if (dragMode == FASTER_DRAG_MODE) { Component desktop = f.getParent(); floatingItems = findFloatingItems(f); currentBounds = f.getBounds(); if (desktop instanceof JComponent) { desktopBounds = ((JComponent) desktop).getVisibleRect(); } else { desktopBounds = desktop.getBounds(); desktopBounds.x = desktopBounds.y = 0; } desktopGraphics = JComponent.safelyGetGraphics(desktop); ((JInternalFrame) f).isDragging = true; didDrag = false; } }
/** Processes the data and renders it to a component */ public synchronized int process(Buffer buffer) { if (component == null) return BUFFER_PROCESSED_FAILED; Format inf = buffer.getFormat(); if (inf == null) return BUFFER_PROCESSED_FAILED; if (inf != inputFormat || !buffer.getFormat().equals(inputFormat)) { if (setInputFormat(inf) != null) return BUFFER_PROCESSED_FAILED; } Object data = buffer.getData(); if (!(data instanceof int[])) return BUFFER_PROCESSED_FAILED; if (lastBuffer != buffer) { lastBuffer = buffer; newImage(buffer); } sourceImage.newPixels(0, 0, inWidth, inHeight); Graphics g = component.getGraphics(); if (g != null) { if (reqBounds == null) { bounds = component.getBounds(); bounds.x = 0; bounds.y = 0; } else bounds = reqBounds; g.drawImage( destImage, bounds.x, bounds.y, bounds.width, bounds.height, 0, 0, inWidth, inHeight, component); } return BUFFER_PROCESSED_OK; }
@Override public void mouseDragged(MouseEvent e) { // System.out.println(e.getLocationOnScreen()); if (dragging) { Rectangle r = selectedComponent.getBounds(); r.x += e.getX() - offset.x; r.y += e.getY() - offset.y; selectedComponent.setBounds(r); /*if(selectedComponent.getLocationOnScreen() != e.getLocationOnScreen()) { //System.out.println("not"); selectedComponent.setLocation(MouseInfo.getPointerInfo().getLocation().x-38, MouseInfo.getPointerInfo().getLocation().y-100); }*/ // System.out.println(selectedComponent.getParent()); } }
boolean overlappedByOwnedWindow() { Component component = getComponent(); if (owner != null && component != null) { Window w = SwingUtilities.getWindowAncestor(owner); if (w == null) { return false; } Window[] ownedWindows = w.getOwnedWindows(); if (ownedWindows != null) { Rectangle bnd = component.getBounds(); for (Window window : ownedWindows) { if (window.isVisible() && bnd.intersects(window.getBounds())) { return true; } } } } return false; }