/** * 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(); // } }
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 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; }
@SuppressWarnings("deprecation") public final void runOneComponent( Component comp, Rectangle bounds, Graphics g, Shape clip, int weightFlags) { if (comp == null || comp.getPeer() == null || !comp.isVisible()) { return; } boolean lightweight = comp.isLightweight(); if ((lightweight && (weightFlags & LIGHTWEIGHTS) == 0) || (!lightweight && (weightFlags & HEAVYWEIGHTS) == 0)) { return; } if (bounds == null) { bounds = comp.getBounds(); } if (clip == null || clip.intersects(bounds)) { Graphics cg = g.create(); try { constrainGraphics(cg, bounds); cg.setFont(comp.getFont()); cg.setColor(comp.getForeground()); if (cg instanceof Graphics2D) { ((Graphics2D) cg).setBackground(comp.getBackground()); } else if (cg instanceof Graphics2Delegate) { ((Graphics2Delegate) cg).setBackground(comp.getBackground()); } run(comp, cg); } finally { cg.dispose(); } } }
@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); } }
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()); } }
/** * 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); }
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 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); } } }
/** * 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; }
// 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; }
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; }
/** * A very nice trick is to center dialog with their parent. * * @param parent The parent <code>Component</code> * @param child The <code>Component</code> to center */ public static void centerComponentChild(Component parent, Component child) { Rectangle par = parent.getBounds(); Rectangle chi = child.getBounds(); child.setLocation( new Point(par.x + (par.width - chi.width) / 2, par.y + (par.height - chi.height) / 2)); }
public void layoutContainer(Container parent) { Insets insets = parent.insets(); int ncomponents = parent.countComponents(); int nrows = getRows(); int ncols = getColumns(); int hgap = getHgap(); int vgap = getVgap(); if (nrows > 0) { ncols = (ncomponents + nrows - 1) / nrows; } else { nrows = (ncomponents + ncols - 1) / ncols; } // Set heights int x; int y; int nFills = 0; boolean[] fills = new boolean[nrows]; int lastFillRow = -1; int nComps = parent.getComponentCount(); y = insets.top; for (int row = 0; row < nrows; row++) { // Find largest minimum height for this row int h = 0; for (int col = 0; col < ncols; col++) { if (row * ncols + col < nComps) { Component c = parent.getComponent(row * ncols + col); h = Math.max(h, c.getMinimumSize().height); } } // Set heights for this row x = insets.left; for (int col = 0; col < ncols; col++) { if (row * ncols + col < nComps) { JComponent c = (JComponent) parent.getComponent(row * ncols + col); int w = c.getWidth(); c.setBounds(x, y, w, h); x += w + hgap; if (col == 0 && getFillRow(c)) { fills[row] = true; } } } y += h + vgap; if (fills[row]) { nFills++; lastFillRow = row; } } // Fill heights if (nFills > 0 && y < parent.getHeight()) { // How much height to add int hAdd = (parent.getHeight() - y) / nFills; int hAdded = 0; for (int row = 0; row < nrows; row++) { if (fills[row]) { if (row == lastFillRow) { // Compensate for rounding error hAdd = parent.getHeight() - (y + hAdded); } for (int col = 0; col < ncols; col++) { if (row * ncols + col < nComps) { Component c = parent.getComponent(row * ncols + col); Rectangle b = c.getBounds(); c.setBounds(b.x, b.y + hAdded, b.width, b.height + hAdd); } } hAdded += hAdd; } } } // Set widths nFills = 0; fills = new boolean[ncols]; int lastFillCol = -1; x = insets.left; for (int col = 0; col < ncols; col++) { // Find largest minimum width for this column int w = 0; for (int row = 0; row < nrows; row++) { if (row * ncols + col < nComps) { Component c = parent.getComponent(row * ncols + col); w = Math.max(w, c.getMinimumSize().width); } } // Set widths for this column y = insets.top; for (int row = 0; row < nrows; row++) { if (row * ncols + col < nComps) { JComponent c = (JComponent) parent.getComponent(row * ncols + col); int h = c.getHeight(); c.setBounds(x, y, w, h); y += h + vgap; if (row == 0 && getFillColumn(c)) { fills[col] = true; } } } x += w + hgap; if (fills[col]) { nFills++; lastFillCol = col; } } // Fill widths if (nFills > 0 && x < parent.getWidth()) { // How much width to add int wAdd = (parent.getWidth() - x) / nFills; int wAdded = 0; for (int col = 0; col < ncols; col++) { if (fills[col]) { if (col == lastFillCol) { wAdd = parent.getWidth() - (x + wAdded); } for (int row = 0; row < nrows; row++) { if (row * ncols + col < nComps) { Component c = parent.getComponent(row * ncols + col); Rectangle b = c.getBounds(); c.setBounds(b.x + wAdded, b.y, b.width + wAdd, b.height); } } wAdded += wAdd; } } } }
public static String doComponentSnapshot( Component component, File file, SnapshotFileChooser.SnapshotFileType type, boolean paintOffscreen) throws IOException { // TODO Should really make this work for more components if (paintOffscreen && !(component instanceof Paintable)) { log.error("Component cannot be painted offscreen. Performing onscreen paint"); paintOffscreen = false; } if (paintOffscreen) { Rectangle rect = component.getBounds(); if (component instanceof MainPanel) { rect.height = ((MainPanel) component).getOffscreenImageHeight(); } else { rect.height = Math.min(component.getHeight(), getMaxPanelHeight()); } // translate to (0, 0) if necessary int dx = rect.x; int dy = rect.y; rect.x = 0; rect.y = 0; rect.width -= dx; rect.height -= dy; component.setBounds(rect); } int width = component.getWidth(); int height = component.getHeight(); // Call appropriate converter String format = null; String[] exts = null; switch (type) { case SVG: // log.debug("Exporting svg screenshot"); exportScreenshotSVG(component, file, paintOffscreen); // exportScreenshotVector2D(component, file, paintOffscreen); break; case JPEG: format = "jpeg"; exts = new String[] {".jpg", ".jpeg"}; break; case PNG: format = "png"; exts = new String[] {"." + format}; break; case EPS: exportScreenshotEpsGraphics(component, file, paintOffscreen); // exportScreenshotEpsGraphicsNoRef(component, file, paintOffscreen); break; } if (format != null && exts != null) { exportScreenShotBufferedImage(component, file, width, height, exts, format, paintOffscreen); } return "OK"; }
/** * When a MenuElement receives an event from a MouseListener, it should never process the event * directly. Instead all MenuElements should call this method with the event. * * @param event a MouseEvent object */ public void processMouseEvent(MouseEvent event) { int screenX, screenY; Point p; int i, c, j, d; Component mc; Rectangle r2; int cWidth, cHeight; MenuElement menuElement; MenuElement subElements[]; MenuElement path[]; Vector<MenuElement> tmp; int selectionSize; p = event.getPoint(); Component source = event.getComponent(); if ((source != null) && !source.isShowing()) { // This can happen if a mouseReleased removes the // containing component -- bug 4146684 return; } int type = event.getID(); int modifiers = event.getModifiers(); // 4188027: drag enter/exit added in JDK 1.1.7A, JDK1.2 if ((type == MouseEvent.MOUSE_ENTERED || type == MouseEvent.MOUSE_EXITED) && ((modifiers & (InputEvent.BUTTON1_MASK | InputEvent.BUTTON2_MASK | InputEvent.BUTTON3_MASK)) != 0)) { return; } if (source != null) { SwingUtilities.convertPointToScreen(p, source); } screenX = p.x; screenY = p.y; tmp = (Vector<MenuElement>) selection.clone(); selectionSize = tmp.size(); boolean success = false; for (i = selectionSize - 1; i >= 0 && success == false; i--) { menuElement = (MenuElement) tmp.elementAt(i); subElements = menuElement.getSubElements(); path = null; for (j = 0, d = subElements.length; j < d && success == false; 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); /** * Send the event to visible menu element if menu element currently in the selected path or * contains the event location */ if ((p.x >= 0 && p.x < cWidth && p.y >= 0 && p.y < cHeight)) { int k; if (path == null) { path = new MenuElement[i + 2]; for (k = 0; k <= i; k++) path[k] = (MenuElement) tmp.elementAt(k); } path[i + 1] = subElements[j]; MenuElement currentSelection[] = getSelectedPath(); // Enter/exit detection -- needs tuning... if (currentSelection[currentSelection.length - 1] != path[i + 1] && (currentSelection.length < 2 || currentSelection[currentSelection.length - 2] != path[i + 1])) { Component oldMC = currentSelection[currentSelection.length - 1].getComponent(); MouseEvent exitEvent = new MouseEvent( oldMC, MouseEvent.MOUSE_EXITED, event.getWhen(), event.getModifiers(), p.x, p.y, event.getXOnScreen(), event.getYOnScreen(), event.getClickCount(), event.isPopupTrigger(), MouseEvent.NOBUTTON); currentSelection[currentSelection.length - 1].processMouseEvent(exitEvent, path, this); MouseEvent enterEvent = new MouseEvent( mc, MouseEvent.MOUSE_ENTERED, event.getWhen(), event.getModifiers(), p.x, p.y, event.getXOnScreen(), event.getYOnScreen(), event.getClickCount(), event.isPopupTrigger(), MouseEvent.NOBUTTON); subElements[j].processMouseEvent(enterEvent, path, this); } MouseEvent mouseEvent = new MouseEvent( mc, event.getID(), event.getWhen(), event.getModifiers(), p.x, p.y, event.getXOnScreen(), event.getYOnScreen(), event.getClickCount(), event.isPopupTrigger(), MouseEvent.NOBUTTON); subElements[j].processMouseEvent(mouseEvent, path, this); success = true; event.consume(); } } } }
public static void setToplevelLocation( Window toplevel, Component component, int relativePosition) { Rectangle compBounds = component.getBounds(); // Convert component location to screen coordinates Point p = new Point(); SwingUtilities.convertPointToScreen(p, component); int x; int y; // Set frame location to be centered on panel switch (relativePosition) { case SwingConstants.NORTH: { x = (p.x + (compBounds.width / 2)) - (toplevel.getWidth() / 2); y = p.y - toplevel.getHeight(); break; } case SwingConstants.EAST: { x = p.x + compBounds.width; y = (p.y + (compBounds.height / 2)) - (toplevel.getHeight() / 2); break; } case SwingConstants.SOUTH: { x = (p.x + (compBounds.width / 2)) - (toplevel.getWidth() / 2); y = p.y + compBounds.height; break; } case SwingConstants.WEST: { x = p.x - toplevel.getWidth(); y = (p.y + (compBounds.height / 2)) - (toplevel.getHeight() / 2); break; } case SwingConstants.NORTH_EAST: { x = p.x + compBounds.width; y = p.y - toplevel.getHeight(); break; } case SwingConstants.NORTH_WEST: { x = p.x - toplevel.getWidth(); y = p.y - toplevel.getHeight(); break; } case SwingConstants.SOUTH_EAST: { x = p.x + compBounds.width; y = p.y + compBounds.height; break; } case SwingConstants.SOUTH_WEST: { x = p.x - toplevel.getWidth(); y = p.y + compBounds.height; break; } default: case SwingConstants.CENTER: { x = (p.x + (compBounds.width / 2)) - (toplevel.getWidth() / 2); y = (p.y + (compBounds.height / 2)) - (toplevel.getHeight() / 2); } } toplevel.setLocation(x, y); }