public void mouseDragged(MouseEvent evt) { Graphics g = canvas.getGraphics(); g.setColor(color); if (type == 0) { g.setXORMode(canvas.getBackground()); g.drawRect(start.x, start.y, end.x - start.x, end.y - start.y); end = evt.getPoint(); g.drawRect(start.x, start.y, end.x - start.x, end.y - start.y); } else if (type == 1) { int radius; g.setXORMode(canvas.getBackground()); radius = (int) Math.sqrt(Math.pow(end.x - start.x, 2) + Math.pow(end.y - start.y, 2)); g.drawOval(start.x - radius, start.y - radius, 2 * radius, 2 * radius); end = evt.getPoint(); radius = (int) Math.sqrt(Math.pow(end.x - start.x, 2) + Math.pow(end.y - start.y, 2)); g.drawOval(start.x - radius, start.y - radius, 2 * radius, 2 * radius); } else if (type == 2) { g.setXORMode(canvas.getBackground()); g.drawOval(start.x, start.y, end.x - start.x, end.y - start.y); end = evt.getPoint(); g.drawOval(start.x, start.y, end.x - start.x, end.y - start.y); } }
/** * Calls <code>setBoundsForFrame</code> with the new values. * * @param f the component to be resized * @param newX the new x-coordinate * @param newY the new y-coordinate * @param newWidth the new width * @param newHeight the new height */ public void resizeFrame(JComponent f, int newX, int newY, int newWidth, int newHeight) { if (dragMode == DEFAULT_DRAG_MODE || dragMode == FASTER_DRAG_MODE) { setBoundsForFrame(f, newX, newY, newWidth, newHeight); } else { JDesktopPane desktopPane = getDesktopPane(f); if (desktopPane != null) { Graphics g = JComponent.safelyGetGraphics(desktopPane); g.setXORMode(Color.white); if (currentBounds != null) { g.drawRect( currentBounds.x, currentBounds.y, currentBounds.width - 1, currentBounds.height - 1); } g.drawRect(newX, newY, newWidth - 1, newHeight - 1); // Work around for 6635462, see comment in dragFrame() sun.java2d.SurfaceData sData = ((sun.java2d.SunGraphics2D) g).getSurfaceData(); if (!sData.isSurfaceLost()) { currentBounds = new Rectangle(newX, newY, newWidth, newHeight); } g.setPaintMode(); g.dispose(); } } }
/** * Moves the visible location of the frame being dragged to the location specified. The means by * which this occurs can vary depending on the dragging algorithm being used. The actual logical * location of the frame might not change until <code>endDraggingFrame</code> is called. */ public void dragFrame(JComponent f, int newX, int newY) { if (dragMode == OUTLINE_DRAG_MODE) { JDesktopPane desktopPane = getDesktopPane(f); if (desktopPane != null) { Graphics g = JComponent.safelyGetGraphics(desktopPane); g.setXORMode(Color.white); if (currentLoc != null) { g.drawRect(currentLoc.x, currentLoc.y, f.getWidth() - 1, f.getHeight() - 1); } g.drawRect(newX, newY, f.getWidth() - 1, f.getHeight() - 1); /* Work around for 6635462: XOR mode may cause a SurfaceLost on first use. * Swing doesn't expect that its XOR drawRect did * not complete, so believes that on re-entering at * the next update location, that there is an XOR rect * to draw out at "currentLoc". But in fact * it's now got a new clean surface without that rect, * so drawing it "out" in fact draws it on, leaving garbage. * So only update/set currentLoc if the draw completed. */ sun.java2d.SurfaceData sData = ((sun.java2d.SunGraphics2D) g).getSurfaceData(); if (!sData.isSurfaceLost()) { currentLoc = new Point(newX, newY); } g.dispose(); } } else if (dragMode == FASTER_DRAG_MODE) { dragFrameFaster(f, newX, newY); } else { setBoundsForFrame(f, newX, newY, f.getWidth(), f.getHeight()); } }
private void paintBell(Graphics g) { P9TermPreferences preferences = P9Term.getPreferences(); if (preferences.getBoolean(P9TermPreferences.VISUAL_BELL) == false) { return; } Color foreground = preferences.getColor(P9TermPreferences.FOREGROUND_COLOR); if (preferences.getBoolean(P9TermPreferences.FANCY_BELL)) { // On decent hardware, we can produce a really tasteful effect by compositing a // semi-transparent rectangle over the terminal. // We need to choose a color that will show up against the background. // A reasonable assumption is that the user has already chosen such a color for the // foreground. Color color = new Color(foreground.getRed(), foreground.getGreen(), foreground.getBlue(), 100); g.setColor(color); g.fillRect(0, 0, getWidth(), getHeight()); } else { // On a remote X11 display (or really rubbish hardware) the compositing effect is // prohibitively expensive, so we offer XOR instead. Color background = preferences.getColor(P9TermPreferences.BACKGROUND_COLOR); ; final int R = blend(background.getRed(), foreground.getRed()); final int G = blend(background.getGreen(), foreground.getGreen()); final int B = blend(background.getBlue(), foreground.getBlue()); g.setColor(background); g.setXORMode(new Color(R, G, B)); g.fillRect(0, 0, getWidth(), getHeight()); g.setPaintMode(); } }
private void drawLine(JComponent c, int x, int y) { _xedit = x; _yedit = y; Graphics g = c.getGraphics(); g.setColor(Color.BLACK); g.setXORMode(c.getBackground()); g.drawLine(_xdown, _ydown, _xedit, _yedit); g.dispose(); }
protected void paintComponent(Graphics g) { super.paintComponent(g); // paints the background and image // If currentRect exists, paint a box on top. if (currentRect != null) { // Draw a rectangle on top of the image. g.setXORMode(Color.white); // Color of line varies // depending on image colors g.drawRect(rectToDraw.x, rectToDraw.y, rectToDraw.width - 1, rectToDraw.height - 1); controller.updateLabel(rectToDraw); } }
@Override protected void paint(Graphics graphics, IFigure fig, Insets insets, Color[] tl, Color[] br) { graphics.setLineStyle(Graphics.LINE_SOLID); graphics.setXORMode(false); Rectangle rect = getPaintRectangle(fig, insets); int top = rect.y; int left = rect.x; int bottom = rect.bottom() - 1; int right = rect.right() - 1; drawDarkerSide(graphics, br, top, left, bottom, right); drawLighterSide(graphics, tl, top, left, bottom, right); }
@Override public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException { Graphics2D g2; if (pageIndex > 0) return Printable.NO_SUCH_PAGE; g2 = (Graphics2D) g; g2.translate(pf.getImageableX(), pf.getImageableY()); Dimension imageSize = calcDimension(image, pf.getImageableWidth(), pf.getImageableHeight()); g2.drawImage( image, (int) (pf.getImageableWidth() / 2 - imageSize.width / 2), (int) (pf.getImageableHeight() / 2 - imageSize.height / 2), imageSize.width, imageSize.height, this); g.setXORMode(Color.white); Font font = g2.getFont().deriveFont(6); g2.setFont(font); int fileNameWidth = g2.getFontMetrics(font).stringWidth(fileName); if (fileNameWidth > imageSize.width) { pathStrings = fileName.split("\\\\"); result = pathStrings[0] + "\\...\\" + pathStrings[pathStrings.length - 1]; resultWidth = g.getFontMetrics(font).stringWidth(result); // TODO: // Add another if for truncating to just a few letters if needed. } else { result = fileName; resultWidth = g.getFontMetrics(font).stringWidth(result); } g.drawString( result, (int) (pf.getImageableWidth() / 2 + imageSize.width / 2 - resultWidth - 5), (int) (pf.getImageableHeight() / 2 + imageSize.height / 2 - 5)); return Printable.PAGE_EXISTS; }
@Override public void paintComponent(Graphics g) { super.paintComponent(g); if (image != null) { Dimension imageSize = calcDimension(image); System.out.println(imageSize + " or " + imageSize.getWidth()); g.drawImage( image, getWidth() / 2 - imageSize.width / 2, getHeight() / 2 - imageSize.height / 2, imageSize.width, imageSize.height, this); g.setXORMode(Color.white); Font font = g.getFont().deriveFont(8); g.setFont(font); int fileNameWidth = g.getFontMetrics(font).stringWidth(fileName); if (fileNameWidth > imageSize.width) { pathStrings = fileName.split("\\\\"); result = pathStrings[0] + "\\...\\" + pathStrings[pathStrings.length - 1]; resultWidth = g.getFontMetrics(font).stringWidth(result); // TODO: // Add another if for truncating to just a few letters if needed. } else { result = fileName; resultWidth = g.getFontMetrics(font).stringWidth(result); } g.drawString( result, getWidth() / 2 + imageSize.width / 2 - resultWidth - 5, getHeight() / 2 + imageSize.height / 2 - 5); } else g.drawImage(image, 0, 0, this); }
private void doPaint(Graphics g) { GraphicsUtil.setupAntialiasing(g); final boolean isEmpty = getIcon() == null && StringUtil.isEmpty(getText()); final Dimension size = getSize(); if (isSmallVariant()) { final Graphics2D g2 = (Graphics2D) g; g2.setColor(UIUtil.getControlColor()); final int w = getWidth(); final int h = getHeight(); if (getModel().isArmed() && getModel().isPressed()) { g2.setPaint( new GradientPaint( 0, 0, UIUtil.getControlColor(), 0, h, ColorUtil.shift(UIUtil.getControlColor(), 0.8))); } else { g2.setPaint( new GradientPaint( 0, 0, ColorUtil.shift(UIUtil.getControlColor(), 1.1), 0, h, ColorUtil.shift(UIUtil.getControlColor(), 0.9))); } g2.fillRect(2, 0, w - 2, h); GraphicsUtil.setupAntialiasing(g2); if (!myMouseInside) { g2.setPaint( new GradientPaint( 0, 0, UIUtil.getBorderColor(), 0, h, UIUtil.getBorderColor().darker())); // g2.setColor(UIUtil.getBorderColor()); } else { g2.setPaint( new GradientPaint( 0, 0, UIUtil.getBorderColor().darker(), 0, h, UIUtil.getBorderColor().darker().darker())); } g2.drawRect(2, 0, w - 3, h - 1); final Icon icon = getIcon(); int x = 7; if (icon != null) { icon.paintIcon(null, g, x, (size.height - icon.getIconHeight()) / 2); x += icon.getIconWidth() + 3; } if (!StringUtil.isEmpty(getText())) { final Font font = getFont(); g2.setFont(font); g2.setColor(UIManager.getColor("Panel.foreground")); g2.drawString(getText(), x, (size.height + font.getSize()) / 2 - 1); } } else { super.paintComponent(g); } final Insets insets = super.getInsets(); final Icon icon = isEnabled() ? ARROW_ICON : DISABLED_ARROW_ICON; final int x; if (isEmpty) { x = (size.width - icon.getIconWidth()) / 2; } else { if (isSmallVariant()) { x = size.width - icon.getIconWidth() - insets.right + 1; } else { x = size.width - icon.getIconWidth() - insets.right + (UIUtil.isUnderNimbusLookAndFeel() ? -3 : 2); } } if (UIUtil.isUnderDarcula()) { g.setXORMode(new Color(208, 188, 159)); } icon.paintIcon(null, g, x, (size.height - icon.getIconHeight()) / 2); g.setPaintMode(); }
/** Overrides <code>Graphics.setXORMode</code>. */ public void setXORMode(Color aColor) { if (debugLog()) { info().log(toShortString() + " Setting XOR mode: " + aColor); } graphics.setXORMode(aColor); }
/*.................................................................................................................*/ public Object doCommand(String commandName, String arguments, CommandChecker checker) { Tree trt = treeDisplay.getTree(); MesquiteTree t = null; if (trt instanceof MesquiteTree) t = (MesquiteTree) trt; if (checker.compare( this.getClass(), "Adjust tool has touched branch", "[branch number][x coordinate touched][y coordinate touched][modifiers]", commandName, "touchedPositionAdjust")) { if (t == null) return null; MesquiteInteger io = new MesquiteInteger(0); int node = MesquiteInteger.fromString(arguments, io); int x = MesquiteInteger.fromString(arguments, io); int y = MesquiteInteger.fromString(arguments, io); String mod = ParseUtil.getRemaining(arguments, io); Point newOnLine = treeDisplay.getTreeDrawing().projectionOnLine(node, x, y); originalX = newOnLine.x; originalY = newOnLine.y; // lastX= newOnLine.x; // lastY = newOnLine.y; Graphics g = null; if (GraphicsUtil.useXORMode(null, false)) { g = treeDisplay.getGraphics(); g.setXORMode(Color.white); g.setColor(Color.red); } // double bX = treeDisplay.getTreeDrawing().lineBaseX[node]; // double bY = treeDisplay.getTreeDrawing().lineBaseY[node]; // Math.sqrt((originalY-bY)*(originalY-bY) + (originalX-bX)*(originalX-bX)); lastBL = tree.getBranchLength(node); double shortestAbove = MesquiteDouble.unassigned; for (int daughter = t.firstDaughterOfNode(node); t.nodeExists(daughter); daughter = t.nextSisterOfNode(daughter)) shortestAbove = MesquiteDouble.minimum(shortestAbove, tree.getBranchLength(daughter)); if (shortestAbove == MesquiteDouble.unassigned) upperLimit = MesquiteDouble.infinite; else if (MesquiteDouble.isCombinable(lastBL)) upperLimit = shortestAbove + lastBL; else upperLimit = shortestAbove + 1.0; int ibX = treeDisplay.getTreeDrawing().lineBaseX[node]; int ibY = treeDisplay.getTreeDrawing().lineBaseY[node]; lastX = treeDisplay.getTreeDrawing().lineTipX[node]; lastY = treeDisplay.getTreeDrawing().lineTipY[node]; if (GraphicsUtil.useXORMode(null, false)) { drawThickLine(g, ibX, ibY, lastX, lastY); for (int daughter = t.firstDaughterOfNode(node); t.nodeExists(daughter); daughter = t.nextSisterOfNode(daughter)) drawThickLine( g, treeDisplay.getTreeDrawing().lineTipX[daughter], treeDisplay.getTreeDrawing().lineTipY[daughter], lastX, lastY); g.fillOval( lastX - ovalRadius, lastY - ovalRadius, ovalRadius + ovalRadius, ovalRadius + ovalRadius); try { g.drawString(MesquiteDouble.toString(lastBL), lastX + 10, lastY); } catch (InternalError e) { // workaround for bug on windows java 1.7. } catch (Throwable e) { } lineOn = true; g.dispose(); } } else if (checker.compare( this.getClass(), "Adjust tool has been dropped", "[branch number][x coordinate dropped][y coordinate dropped]", commandName, "droppedPositionAdjust")) { if (t == null) return null; if (editorOn) return null; MesquiteInteger io = new MesquiteInteger(0); int node = MesquiteInteger.fromString(arguments, io); int x = MesquiteInteger.fromString(arguments, io); int y = MesquiteInteger.fromString(arguments, io); if (lineOn) { Point newOnLine = treeDisplay.getTreeDrawing().projectionOnLine(node, x, y); double bX = treeDisplay.getTreeDrawing().lineBaseX[node]; double bY = treeDisplay.getTreeDrawing().lineBaseY[node]; double tX = treeDisplay.getTreeDrawing().lineTipX[node]; double tY = treeDisplay.getTreeDrawing().lineTipY[node]; double lengthLine = Math.sqrt((originalY - bY) * (originalY - bY) + (originalX - bX) * (originalX - bX)); double bL; if (lengthLine != 0) { double extension = Math.sqrt( (newOnLine.y - bY) * (newOnLine.y - bY) + (newOnLine.x - bX) * (newOnLine.x - bX)) / lengthLine; if (t.getBranchLength(node) == 0 || t.branchLengthUnassigned(node)) bL = extension; else bL = t.getBranchLength(node) * extension; } else bL = 1; if (bL > upperLimit) bL = upperLimit; else if (bL < lowerLimit) bL = lowerLimit; double oldBL = t.getBranchLength(node); if (!MesquiteDouble.isCombinable(oldBL)) oldBL = 1.0; t.setBranchLength(node, bL, false); double difference = oldBL - t.getBranchLength(node); for (int daughter = t.firstDaughterOfNode(node); t.nodeExists(daughter); daughter = t.nextSisterOfNode(daughter)) if (MesquiteDouble.isCombinable(t.getBranchLength(daughter))) t.setBranchLength(daughter, t.getBranchLength(daughter) + difference, false); t.notifyListeners(this, new Notification(MesquiteListener.BRANCHLENGTHS_CHANGED)); Graphics g = treeDisplay.getGraphics(); g.setPaintMode(); g.dispose(); treeDisplay.pleaseUpdate(true); lineOn = false; } } else if (checker.compare( this.getClass(), "Adjust tool is being dragged", "[branch number][x coordinate][y coordinate]", commandName, "draggedPositionAdjust")) { if (t == null) return null; if (editorOn) return null; MesquiteInteger io = new MesquiteInteger(0); int node = MesquiteInteger.fromString(arguments, io); int x = MesquiteInteger.fromString(arguments, io); int y = MesquiteInteger.fromString(arguments, io); if (lineOn) { Point newOnLine = treeDisplay.getTreeDrawing().projectionOnLine(node, x, y); // WARNING": This shouldn't result in length increase if simple click and release with no // drag; must subtract original X, Y Graphics g = null; if (GraphicsUtil.useXORMode(null, false)) { g = treeDisplay.getGraphics(); g.setXORMode(Color.white); g.setColor(Color.red); } // g.fillOval(lastX-ovalRadius, lastY-ovalRadius, ovalRadius + ovalRadius, ovalRadius + // ovalRadius); // g.fillOval(newOnLine.x-ovalRadius, newOnLine.y -ovalRadius, ovalRadius + ovalRadius, // ovalRadius + ovalRadius); // g.drawLine(originalX, originalY, lastX, lastY); // g.drawLine(originalX, originalY, newOnLine.x, newOnLine.y); // if decreasing, & unassigned involved: push unassigned down and assign values to // unassigned above; if increasing, push unassigne up int ibX = treeDisplay.getTreeDrawing().lineBaseX[node]; int ibY = treeDisplay.getTreeDrawing().lineBaseY[node]; int itX = treeDisplay.getTreeDrawing().lineTipX[node]; int itY = treeDisplay.getTreeDrawing().lineTipY[node]; double bX = ibX; double bY = ibY; double tX = itX; double tY = itY; double lengthLine = Math.sqrt((originalY - bY) * (originalY - bY) + (originalX - bX) * (originalX - bX)); if (lengthLine != 0) { if (GraphicsUtil.useXORMode(null, false)) { if (MesquiteTrunk.isMacOSX() && MesquiteTrunk.getJavaVersionAsDouble() >= 1.5 && MesquiteTrunk.getJavaVersionAsDouble() < 1.6) // due to a JVM bug g.fillRect(lastX, lastY - 20, 100, 20); g.drawString(MesquiteDouble.toString(lastBL), lastX + 10, lastY); if (MesquiteTrunk.isMacOSX() && MesquiteTrunk.getJavaVersionAsDouble() >= 1.5 && MesquiteTrunk.getJavaVersionAsDouble() < 1.6) // due to a JVM bug g.fillRect(lastX, lastY - 20, 100, 20); } double extension = Math.sqrt( (newOnLine.y - bY) * (newOnLine.y - bY) + (newOnLine.x - bX) * (newOnLine.x - bX)) / lengthLine; double bL; if (t.getBranchLength(node) == 0 || t.branchLengthUnassigned(node)) bL = extension; else bL = t.getBranchLength(node) * extension; if (bL > upperLimit) { bL = upperLimit; if (t.getBranchLength(node) == 0 || t.branchLengthUnassigned(node)) extension = upperLimit; else extension = upperLimit / t.getBranchLength(node); } else if (bL < lowerLimit) { bL = lowerLimit; if (t.getBranchLength(node) == 0 || t.branchLengthUnassigned(node)) extension = lowerLimit; else extension = lowerLimit / t.getBranchLength(node); } lastBL = bL; if (GraphicsUtil.useXORMode(null, false)) { drawThickLine(g, ibX, ibY, lastX, lastY); for (int daughter = t.firstDaughterOfNode(node); t.nodeExists(daughter); daughter = t.nextSisterOfNode(daughter)) drawThickLine( g, treeDisplay.getTreeDrawing().lineTipX[daughter], treeDisplay.getTreeDrawing().lineTipY[daughter], lastX, lastY); g.fillOval( lastX - ovalRadius, lastY - ovalRadius, ovalRadius + ovalRadius, ovalRadius + ovalRadius); } int newX = ibX + (int) (extension * (tX - bX)); int newY = ibY + (int) (extension * (tY - bY)); if (GraphicsUtil.useXORMode(null, false)) { g.drawString(MesquiteDouble.toString(bL), newX + 10, newY); drawThickLine(g, ibX, ibY, newX, newY); for (int daughter = t.firstDaughterOfNode(node); t.nodeExists(daughter); daughter = t.nextSisterOfNode(daughter)) drawThickLine( g, treeDisplay.getTreeDrawing().lineTipX[daughter], treeDisplay.getTreeDrawing().lineTipY[daughter], newX, newY); g.fillOval( newX - ovalRadius, newY - ovalRadius, ovalRadius + ovalRadius, ovalRadius + ovalRadius); } lastX = newX; lastY = newY; } // lastX= newOnLine.x; // lastY = newOnLine.y; } } return null; }
/** * Given a MouseEvent, check the source, and if it's a MapBean, then grab the projection and * java.awt.Graphics from it to use for generation and rendering of the EditableOMGraphic objects. * * @param e MouseEvent * @param firmPaint true if the graphic is being rendered at rest, with fill colors and true * colors, with the grab point if the state allows it. If false, then the fill color will not * be used, and just the graphic will be drawn. Use false for graphics that are moving. */ public void redraw(MouseEvent e, boolean firmPaint, boolean drawXOR) { if (DEBUG) { Debug.output("EditableOMGraphic.redraw(" + (firmPaint ? "firmPaint)" : ")")); } if (e == null) { if (lastMouseEvent == null) { return; } e = lastMouseEvent; } Object obj = e.getSource(); if (!(obj instanceof MapBean)) { return; } MapBean map = (MapBean) obj; Graphics g = map.getGraphics(); OMGraphic graphic = getGraphic(); if (firmPaint) { // So, with a firm paint, we want to clean the screen. If // the map is being buffered, we need to clean out the // buffer, which is why we set the Request paint to true, // to get the image rebuilt. Otherwise, a copy of the // graphic remains. map.setBufferDirty(true); graphic.generate(getProjection()); map.repaint(); } else { // If we get here, we are painting a moving object, so we // only want to do the outline to make it as fast as // possible. holder.setFrom(graphic); DrawingAttributes.DEFAULT.setTo(graphic); modifyOMGraphicForEditRender(); graphic.regenerate(getProjection()); if (drawXOR) { g.setXORMode(Color.lightGray); g.setColor((Color) graphic.getDisplayPaint()); render(g); } GrabPoint gp = getMovingPoint(); if (gp != null) { gp.set(e.getX(), e.getY()); if (gp instanceof OffsetGrabPoint) { ((OffsetGrabPoint) gp).moveOffsets(); } setGrabPoints(); } } if (!firmPaint) { generate(getProjection()); render(g); holder.setTo(graphic); } resetOMGraphicAfterEditRender(); g.dispose(); lastMouseEvent = e; }