// Many of the following methods have been added purely // so InternalFunctions can work. Originally, the code in // that class was inline here, so its functions had direct // access. I removed it so that students do not need to wade // through all the functions! But, that leaves the question as // to what to do with the following methods, but I don't think // there's much you can do ... public void changeSize(int width, int height) { setSize(width, height); Component top = splitPane.getTopComponent(); Component bottom = splitPane.getBottomComponent(); int totalHeight = top.getHeight() + bottom.getHeight(); int topHeight = (totalHeight * topProportion) / 100; int bottomHeight = (totalHeight * bottomProportion) / 100; top.setPreferredSize(new Dimension(width - 10, topHeight)); bottom.setPreferredSize(new Dimension(width - 10, bottomHeight)); splitPane.resetToPreferredSizes(); pack(); }
private static Pair<Image, Point> createDragImage( final Tree tree, final Component c, Point dragOrigin, boolean adjustToPathUnderDragOrigin) { if (c instanceof JComponent) { ((JComponent) c).setOpaque(true); } c.setForeground(tree.getForeground()); c.setBackground(tree.getBackground()); c.setFont(tree.getFont()); c.setSize(c.getPreferredSize()); final BufferedImage image = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = (Graphics2D) image.getGraphics(); g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f)); c.paint(g2); g2.dispose(); Point point = new Point(-image.getWidth(null) / 2, -image.getHeight(null) / 2); if (adjustToPathUnderDragOrigin) { TreePath path = tree.getPathForLocation(dragOrigin.x, dragOrigin.y); if (path != null) { Rectangle bounds = tree.getPathBounds(path); point = new Point(bounds.x - dragOrigin.x, bounds.y - dragOrigin.y); } } return new Pair<Image, Point>(image, point); }
public void componentMoved(ComponentEvent evt) { Component component = evt.getComponent(); Point point = component.getLocation(); if (point.y < 0) { component.setBounds(point.x, 0, component.getWidth(), component.getHeight()); } }
public synchronized void paintIcon(Component c, Graphics g, int x, int y) { g.setColor(Color.white); g.fillRect(0, 0, c.getWidth(), c.getHeight()); if (getImageObserver() == null) { g.drawImage( getImage(), c.getWidth() / 2 - getIconWidth() / 2, c.getHeight() / 2 - getIconHeight() / 2, c); } else { g.drawImage( getImage(), c.getWidth() / 2 - getIconWidth() / 2, c.getHeight() / 2 - getIconHeight() / 2, getImageObserver()); } }
/** * Centers the given component in relation to its owner. * * @param component the component to center * @param owner the parent frame */ public static void centerComponent(Component component, Component owner) { // find the difference in width to see the offsets int widthDifference = owner.getWidth() - component.getWidth(); int heightDifference = owner.getHeight() - component.getHeight(); // we can divide the differences by 2 and add that to the owner's top left // and then make that the top left of the component // to center the frame int leftOffset = widthDifference / 2; int topOffset = heightDifference / 2; // these are the new locations int left = owner.getX() + leftOffset; int top = owner.getY() + topOffset; Utils.changeFrameLocation(component, left, top); }
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { if (getComponentCount() > 0) { Component c = getComponent(0); if (orientation == SwingConstants.HORIZONTAL) return c.getWidth(); else return c.getHeight(); } return 50; }
public void show(Component invoker, int x, int y) { if (invoker != null) { y += invoker.getHeight(); // add the height of the invoker to // locate the popup at the bottom of // this invoker } super.show(invoker, x, y); }
@Override public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { final Graphics2D g2d = (Graphics2D) g; final Insets ins = getBorderInsets(c); final int yOff = (ins.top + ins.bottom) / 4; final boolean square = DarculaButtonUI.isSquare(c); int offset = square ? 1 : getOffset(); if (c.hasFocus()) { if (DarculaButtonUI.isHelpButton((JComponent) c)) { int w = c.getWidth(); int h = c.getHeight(); DarculaUIUtil.paintFocusOval(g2d, (w - 22) / 2, (h - 22) / 2, 22, 22); } else { DarculaUIUtil.paintFocusRing(g2d, offset, yOff, width - 2 * offset, height - 2 * yOff); } } else { final GraphicsConfig config = new GraphicsConfig(g); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT); g2d.setPaint( UIUtil.getGradientPaint( width / 2, y + yOff + 1, Gray._80.withAlpha(90), width / 2, height - 2 * yOff, Gray._90.withAlpha(90))); // g.drawRoundRect(x + offset + 1, y + yOff + 1, width - 2 * offset, height - 2*yOff, 5, 5); ((Graphics2D) g).setPaint(Gray._100.withAlpha(180)); if (DarculaButtonUI.isHelpButton((JComponent) c)) { int w = c.getWidth(); int h = c.getHeight(); g.drawOval((w - 22) / 2, (h - 22) / 2, 22, 22); } else { g.drawRoundRect( x + offset, y + yOff, width - 2 * offset, height - 2 * yOff, square ? 3 : 5, square ? 3 : 5); } config.restore(); } }
/** {@inheritDoc} */ @Override public void mouseDragged(final MouseEvent aEvent) { final MouseEvent event = convertEvent(aEvent); final Point point = event.getPoint(); // Update the selected channel while dragging... this.controller.setSelectedChannel(point); if (getModel().isCursorMode() && (this.movingCursor >= 0)) { this.controller.moveCursor(this.movingCursor, getCursorDropPoint(point)); aEvent.consume(); } else { if ((this.lastClickPosition == null) && ((aEvent.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK) != 0)) { this.lastClickPosition = new Point(point); } final JScrollPane scrollPane = getAncestorOfClass(JScrollPane.class, (Component) aEvent.getSource()); if ((scrollPane != null) && (this.lastClickPosition != null)) { final JViewport viewPort = scrollPane.getViewport(); final Component signalView = this.controller.getSignalDiagram().getSignalView(); boolean horizontalOnly = (aEvent.getModifiersEx() & InputEvent.ALT_DOWN_MASK) != 0; boolean verticalOnly = horizontalOnly && ((aEvent.getModifiersEx() & InputEvent.SHIFT_DOWN_MASK) != 0); int dx = aEvent.getX() - this.lastClickPosition.x; int dy = aEvent.getY() - this.lastClickPosition.y; Point scrollPosition = viewPort.getViewPosition(); int newX = scrollPosition.x; if (!verticalOnly) { newX -= dx; } int newY = scrollPosition.y; if (verticalOnly || !horizontalOnly) { newY -= dy; } int diagramWidth = signalView.getWidth(); int viewportWidth = viewPort.getWidth(); int maxX = diagramWidth - viewportWidth - 1; scrollPosition.x = Math.max(0, Math.min(maxX, newX)); int diagramHeight = signalView.getHeight(); int viewportHeight = viewPort.getHeight(); int maxY = diagramHeight - viewportHeight; scrollPosition.y = Math.max(0, Math.min(maxY, newY)); viewPort.setViewPosition(scrollPosition); } // Use UNCONVERTED/ORIGINAL mouse event! handleZoomRegion(aEvent, this.lastClickPosition); } }
public void paintIcon(Component c, Graphics g, int x, int y) { Color oldColor = g.getColor(); boolean enabled = c.isEnabled(); boolean pressed = false; if (c instanceof JButton) { pressed = ((JButton) c).getModel().isPressed(); } int i = 0; int j = 0; int w = Math.min(width, c.getWidth()); int h = c.getHeight(); if (h < 5 || w < 5) { // not enough space for the arrow g.setColor(oldColor); return; } int size = Math.min(h / 2, w / 2); size = Math.max(size, 2); int mid = size / 2; x = ((w - size) / 2); // center arrow y = (h - size) / 2; // center arrow if (pressed) { x++; y++; } g.translate(x, y); // move the x,y origin in the graphic if (enabled) g.setColor(UIManager.getColor("controlDkShadow")); // NOT // LOCALIZABLE else g.setColor(UIManager.getColor("controlShadow")); // NOT // LOCALIZABLE if (!enabled) { g.translate(1, 1); g.setColor(UIManager.getColor("controlLtHighlight")); // NOT // LOCALIZABLE for (i = size - 1; i >= 0; i--) { g.drawLine(mid - i, j, mid + i, j); j++; } g.translate(-1, -1); g.setColor(UIManager.getColor("controlShadow")); // NOT // LOCALIZABLE } j = 0; for (i = size - 1; i >= 0; i--) { g.drawLine(mid - i, j, mid + i, j); j++; } g.translate(-x, -y); g.setColor(oldColor); }
/** * Extracts the buffered image of some component * * @param myComponent the component for which the BufferedImage is required * @param region region to be captured * @return the BufferedImage which has the current condition of component * @throws IOException */ public static BufferedImage Get_Component_Image(Component myComponent, Rectangle region) throws IOException { BufferedImage img = new BufferedImage( myComponent.getWidth(), myComponent.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics g = img.getGraphics(); myComponent.paint(g); g.dispose(); return img; }
public void paint(Graphics g) { if (comp != null) { width = comp.getWidth() / 6; height = comp.getHeight() * 2 / 3; } g.setColor(bgColor); g.fillRect(x, y, width, height); g.setColor(fgColor); g.setFont(font); g.drawString(strTray, x / 2 + width / 2, y + height + 10); super.paint(g); }
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { if (image != null && showimage) { // TODO: add ability to scale maintaining aspect ratio fitting to size of parent // TODO: Need to add not zoom all the way, but zoom to a box of aspect ratio 4/3 Graphics2D g2 = (Graphics2D) g; AffineTransform tran = new AffineTransform(1f, 0f, 0f, 1f, 0, 0); float widthscale = (float) c.getWidth() / image.getWidth(); float heightscale = (float) c.getHeight() / image.getHeight(); switch (drawmode) { case DRAW_MODE_ASPECT: float scale; if (widthscale < heightscale) { scale = widthscale; } else { scale = heightscale; } tran.scale(scale, scale); g2.drawImage( image, new AffineTransformOp(tran, AffineTransformOp.TYPE_BILINEAR), (int) (c.getWidth() - image.getWidth() * scale) / 2, (int) (c.getHeight() - image.getHeight() * scale) / 2); break; case DRAW_MODE_WIDTH: tran.scale(widthscale, widthscale); g2.drawImage(image, tran, null); break; case DRAW_MODE_HEIGHT: tran.scale(heightscale, heightscale); g2.drawImage(image, tran, null); break; default: tran.scale(widthscale, heightscale); g2.drawImage(image, tran, null); break; } } }
public static BufferedImage componentToImage(Component component, Rectangle region) { BufferedImage img = new BufferedImage( component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_ARGB_PRE); Graphics g = img.getGraphics(); g.setColor(component.getForeground()); g.setFont(component.getFont()); component.paintAll(g); g.dispose(); if (region == null) { return img; } return img.getSubimage(region.x, region.y, region.width, region.height); }
/** * Centers the given component on the user's screen. * * @param component a component (usually a frame.) */ public static void centerOnScreen(Component component) { Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit(); Dimension screenSize = toolkit.getScreenSize(); int screenWidth = (int) screenSize.getWidth(); int screenHeight = (int) screenSize.getHeight(); int componentWidth = component.getWidth(); int componentHeight = component.getHeight(); int top = (screenHeight - componentHeight) / 2; int left = (screenWidth - componentWidth) / 2; Utils.changeFrameLocation(component, left, top); }
public static final Component getChildAtLine( Container container, Point point, boolean horizontal) { if (horizontal) { for (int i = 0; i < container.getComponentCount(); i++) { Component component = container.getComponent(i); if (point.x >= component.getX() && point.x < component.getX() + component.getWidth()) return component; } } else { for (int i = 0; i < container.getComponentCount(); i++) { Component component = container.getComponent(i); if (point.y >= component.getY() && point.y < component.getY() + component.getHeight()) return component; } } return null; }
public static void fillComponent(Graphics g, Component c, Icon texture) { int x = 0; int y = 0; int w = c.getWidth(); int h = c.getHeight(); if (texture != null) { int tw = texture.getIconWidth(); int th = texture.getIconHeight(); Point p = JTattooUtilities.getRelLocation(c); y = -p.y; while (y < h) { x = -p.x; while (x < w) { texture.paintIcon(c, g, x, y); x += tw; } y += th; } } else { g.setColor(c.getBackground()); g.fillRect(x, y, w, h); } }
static void fillRect(final Graphics g, final Component c) { fillRect(g, c, c.getBackground(), 0, 0, c.getWidth(), c.getHeight()); }
public static void paintInCenterOf(@NotNull Component c, Graphics g, Icon icon) { final int x = (c.getWidth() - icon.getIconWidth()) / 2; final int y = (c.getHeight() - icon.getIconHeight()) / 2; icon.paintIcon(c, g, x, y); }
public void showUnderneathOf(@NotNull Component aComponent) { show(new RelativePoint(aComponent, new Point(0, aComponent.getHeight()))); }
private ArrayList<SoundSample> getSampleAsArrayList(Selection selection, Component c) { ArrayList<SoundSample> points = new ArrayList<SoundSample>(); // System.err.println(c.getHeight()); int sampleBeginIndex = selection.getBegin(); int sampleEndIndex = selection.getEnd(); if (!checkRange(sampleBeginIndex, sampleEndIndex)) { System.err.println( "The range of sample (" + sampleBeginIndex + " , " + sampleEndIndex + ") you are trying to visualize is invalid"); } else { int selectionLength = sampleEndIndex - sampleBeginIndex + 1; float maxValue = myHelper.getMaxSampleValue(); samplePerPixel = selectionLength * 1.0 / frameWidth; if (DEBUG) System.err.println("getSamples: sample per pixel " + samplePerPixel); if (selectionLength < frameWidth) { // if every sample can be displayed (i.e at least 1 pixel per sample) if (DEBUG) System.err.println("getSamples: Everything can be displayed " + selectionLength); int ratio = (int) Math.floor(frameWidth / selectionLength); if (DEBUG) System.err.println("getSamples: ratio " + ratio); int occupyPixels = ratio * selectionLength; selectionLength = selectionLength + (frameWidth - occupyPixels) / ratio; // fill up the display space with a few more samples if (DEBUG) System.err.println("getSamples: selection length " + selectionLength); if (selectionLength + sampleBeginIndex - 1 > data.size()) { selectionLength -= selectionLength + sampleBeginIndex - 1 - data.size(); } if (DEBUG) System.err.println("getSamples: Final selection length " + selectionLength); for (int i = 0; i < selectionLength; i++) { float sampleValue = data.get(sampleBeginIndex + i - 1); float y = ((float) Math.floor(c.getHeight() / 2) * (1 - sampleValue / maxValue)); points.add( new SoundSample(new Point2D.Float(i * ratio, y), sampleValue, sampleBeginIndex + i)); } } else if (selectionLength / 2 < frameWidth) { // we can sample at Nyquist rate // System.err.println("Sample begin " + sampleBeginIndex ); int start = sampleBeginIndex; for (int pixel = 0; pixel < frameWidth; pixel++) { // System.err.println("Sample index " + (start+ samplePerPixel * pixel) ); int sampleValue = data.get((int) Math.floor(start + samplePerPixel * pixel)); // int sampleValue = findMax(start, end); float y = ((float) Math.floor(c.getHeight() / 2) - (sampleValue * ((float) Math.floor(c.getHeight() / 2) / maxValue))); points.add(new SoundSample(new Point2D.Float(pixel, y), sampleValue, pixel)); } } else { // show general contour int start = sampleBeginIndex; int end = (int) (sampleBeginIndex + this.samplePerPixel); int baseLine = c.getHeight() / 2; for (int pixel = 0; pixel < frameWidth; pixel++) { ArrayList<Integer> minMax = findMinMax(start, end); // System.err.println(minMax); float y = baseLine - (minMax.get(0) * 0.9f / maxValue * baseLine); points.add(new SoundSample(new Point2D.Float(pixel, y), minMax.get(0), pixel)); y = baseLine - (minMax.get(0) * 0.7f / maxValue * baseLine); points.add(new SoundSample(new Point2D.Float(pixel, y), minMax.get(0) / 2, pixel)); // System.err.print("Min y: " + y + " "); y = baseLine - (minMax.get(1) * 0.9f / maxValue * baseLine); points.add(new SoundSample(new Point2D.Float(pixel, y), minMax.get(1) / 2, pixel)); y = baseLine - (minMax.get(1) * 0.7f / maxValue * baseLine); points.add(new SoundSample(new Point2D.Float(pixel, y), minMax.get(1), pixel)); // if (y > c.getHeight()) // System.err.println("not right"); start = end + 1; end = (int) (end + samplePerPixel); } } } return points; }
/*@Override*/ public void paintIcon(Component c, Graphics g, int x, int y) { final boolean expandToFit = (mWidth < 1); if (DEBUG.IMAGE && DEBUG.META) out( "paintIcon; onto=" + GUI.name(c) + " painter=" + GUI.name(mPainter) + "@" + Integer.toHexString((mPainter.hashCode()))); if (mPainter == null) { // note this means repaint updates would stop in a new parent, // tho assuming it's loaded by then, regular paints would work fine. mPainter = c; } if (DrawBorder && !expandToFit) { g.setColor(Color.gray); g.drawRect(x, y, mWidth - 1, mHeight - 1); } if (mImage == null) { if (!isLoading /*&& mPreviewData != null*/) { synchronized (this) { if (!isLoading /*&& mPreviewData != null*/) VUE.invokeAfterAWT(ResourceIcon.this); // load the preview } } g.setColor(Color.gray); g.drawRect(x, y, mWidth - 1, mHeight - 1); return; } int fitWidth, fitHeight; final Dimension maxImageSize; if (expandToFit) { // fill the given component fitWidth = c.getWidth(); fitHeight = c.getHeight(); maxImageSize = c.getSize(); } else { // paint at our fixed size fitWidth = mWidth; fitHeight = mHeight; if (DrawBorder) maxImageSize = new Dimension(fitWidth - BorderSpace * 2, fitHeight - BorderSpace * 2); else maxImageSize = new Dimension(fitWidth, fitHeight); if (DEBUG.IMAGE && DEBUG.META) out("paintIcon; into " + GUI.name(maxImageSize)); } double zoomFit; if (mImage == NoImage && expandToFit) { zoomFit = 1; } else { Rectangle2D imageBounds; if (CropToSquare) { // square off image, then fit in icon (todo: better; crop to icon) int smallestAxis = mImageWidth > mImageHeight ? mImageHeight : mImageWidth; imageBounds = new Rectangle2D.Float(0, 0, smallestAxis, smallestAxis); } else { // fit entire image in icon imageBounds = new Rectangle2D.Float(0, 0, mImageWidth, mImageHeight); } zoomFit = ZoomTool.computeZoomFit(maxImageSize, 0, imageBounds, null, false); if (zoomFit > MaxZoom) zoomFit = MaxZoom; } final int drawW = (int) (mImageWidth * zoomFit + 0.5); final int drawH = (int) (mImageHeight * zoomFit + 0.5); int xoff = x; int yoff = y; // center if drawable area is bigger than image if (drawW != fitWidth) xoff += (fitWidth - drawW) / 2; if (drawH != fitHeight) yoff += (fitHeight - drawH) / 2; Shape oldClip = null; if (CropToSquare && !expandToFit) { oldClip = g.getClip(); g.clipRect(x, y, mWidth, mHeight); } if (DEBUG.IMAGE && DEBUG.META) out("paintIcon; " + Util.tag(mImage) + " as " + drawW + "x" + drawH); g.drawImage(mImage, xoff, yoff, drawW, drawH, null); if (DEBUG.BOXES) { g.setColor(Color.green); ((Graphics2D) g) .setComposite( java.awt.AlphaComposite.getInstance(java.awt.AlphaComposite.SRC_OVER, 0.2f)); g.drawRect(x, y, mWidth - 1, mHeight - 1); ((Graphics2D) g).setComposite(java.awt.AlphaComposite.SrcOver); } if (CropToSquare && !expandToFit) g.setClip(oldClip); }