📄 svgeditor.java
字号:
/** the select and drag and resize tool */ public final static int SELECT_TOOL = 0; /** the Rectangle tool */ public final static int RECT_TOOL = 1; /** the Ellipse tool */ public final static int ELLIPSE_TOOL = 2; /** the Line tool */ public final static int LINE_TOOL = 3; /** the select and drag tool */ public final static int MOVE_TOOL = 10; /** the drag point tool */ public final static int MOVE_POINT_TOOL = 11; /** the north west resize tool */ public final static int RESIZE_NW_TOOL = 20; /** the north resize tool */ public final static int RESIZE_N_TOOL = 21; /** the north east resize tool */ public final static int RESIZE_NE_TOOL = 22; /** the east resize tool */ public final static int RESIZE_E_TOOL = 23; /** the south east resize tool */ public final static int RESIZE_SE_TOOL = 24; /** the south resize tool */ public final static int RESIZE_S_TOOL = 25; /** the south west resize tool */ public final static int RESIZE_SW_TOOL = 26; /** the west resize tool */ public final static int RESIZE_W_TOOL = 27; /** the clean video frame image */ protected Image image; /** * the image including graphical objects from other annotations than * the current active annotation */ protected BufferedImage compoundImg; /** a graphics object of the compoundImg */ protected Graphics2D comG2d; /** * The GraphicsNodes don't seem to paint correctly when using * transparency on a Graphics2D object, so use a seperate * BufferedImage for the annotations */ //protected BufferedImage svgImg; //protected Graphics2D svgG2d; //private final Color clearColor = new Color(255, 255, 255, 0); protected final AlphaComposite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f); /** Holds value of property DOCUMENT ME! */ private final BasicStroke selectionStroke = new BasicStroke(1.0f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 10.0f, new float[] { 2, 3 }, 1.0f); private int zoom = 100; private float zoomFactor = 1.0f; private AffineTransform trans = new AffineTransform(); /** the graphical shape that is referenced by the active annotation */ protected Shape currentShape; /** a clone from the currentShape for editing purposes */ protected Shape copyShape; /** the bounding box of the current shape */ protected Rectangle2D bBox; /** the active area for a line object, for dragging */ protected Polygon lineBox; /** the corner markers of the selected shape */ protected Rectangle2D[] corners; /** Holds value of property DOCUMENT ME! */ private final int MARKER_SIZE = 6; /** Holds value of property DOCUMENT ME! */ private final int MIN_RESIZE_SIZE = 8; /** the bounds of the editable space, the size of a video frame */ private Rectangle editRect; private Point editPoint; private boolean dragging = false; private Point dragStart; private Rectangle2D resizeStartRect; private int toolMode = SELECT_TOOL; /** the select tool can have different submodes, move and resize */ private int toolSubMode = MOVE_TOOL; /** * Creates a new EditorPanel instance */ public EditorPanel() { super(); setBackground(Color.white); addMouseListener(this); addMouseMotionListener(this); setTransferHandler(new IDTransferHandler()); editRect = new Rectangle(); corners = new Rectangle2D[] { new Rectangle(0, 0, MARKER_SIZE, MARKER_SIZE), new Rectangle(0, 0, MARKER_SIZE, MARKER_SIZE), new Rectangle(0, 0, MARKER_SIZE, MARKER_SIZE), new Rectangle(0, 0, MARKER_SIZE, MARKER_SIZE) }; } /** * Creates a new EditorPanel instance * * @param image an image from the video * @param shape the current 2d annotation */ public EditorPanel(Image image, Shape shape) { this(); setImage(image); currentShape = shape; setShape(shape); } /** * Set the image that is displayed underneath the 2d annotations. * * @param image the background image, a frame image from the media */ public void setImage(Image image) { this.image = image; if (image != null) { setPreferredSize(new Dimension(image.getWidth(null), image.getHeight(null))); editRect.setRect(0, 0, image.getWidth(null), image.getHeight(null)); } paintBuffer(); } /** * Paint the background image and 2d annotations other than the current * active annotation that occur on the current frame in the media. */ protected void paintBuffer() { if ((compoundImg == null) || (compoundImg.getWidth() != getPreferredSize().width)) { compoundImg = new BufferedImage(getPreferredSize().width, getPreferredSize().height, BufferedImage.TYPE_INT_RGB); comG2d = compoundImg.createGraphics(); } comG2d.scale(zoomFactor, zoomFactor); if (image != null) { comG2d.drawImage(image, 0, 0, this); } comG2d.setComposite(alpha); comG2d.setColor(STROKE_COLOR); // draw other annotations using transparency long currentFrameTime = SVGEditor.this.currentFrame * SVGEditor.this.msPerFrame; //long time = annotation.getBeginTimeBoundary(); Iterator tierIt = transcription.getTiers().iterator(); while (tierIt.hasNext()) { TierImpl tier = (TierImpl) tierIt.next(); if (tier == annotation.getTier()) { continue; } if ((tier.getLinguisticType() != null) && tier.getLinguisticType().hasGraphicReferences()) { Vector anns = tier.getAnnotations(); Iterator annIter = anns.iterator(); while (annIter.hasNext()) { Annotation a = (Annotation) annIter.next(); if (currentFrameTime > -1) { if ((a.getBeginTimeBoundary() <= currentFrameTime) && (a.getEndTimeBoundary() > currentFrameTime)) { if (((SVGAlignableAnnotation) a).getShape() != null) { comG2d.draw(((SVGAlignableAnnotation) a).getShape()); break; } } } else { if ((a.getBeginTimeBoundary() <= annotation.getBeginTimeBoundary()) && (a.getEndTimeBoundary() > annotation.getBeginTimeBoundary())) { if (((SVGAlignableAnnotation) a).getShape() != null) { comG2d.draw(((SVGAlignableAnnotation) a).getShape()); break; } } } } } } comG2d.setComposite(AlphaComposite.Src); repaint(); } /** * Returns the current image. * * @return the current image */ public Image getImage() { return image; } /** * Overrides the <code>JComponent</code> method by painting the * abckground image and the current 2d shape that is being edited. * * @param g the object to render to */ public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; if (compoundImg != null) { g2d.drawImage(compoundImg, 0, 0, this); } if (copyShape != null) { g2d.setColor(STROKE_COLOR); g2d.scale(zoomFactor, zoomFactor); //copyShape.paint(g2d, true); g2d.draw(copyShape); if (bBox != null) { g2d.setColor(Constants.ACTIVEANNOTATIONCOLOR); g2d.setStroke(createScaledStroke()); if (copyShape instanceof Line2D) { if (!dragging) { for (int i = 0; i < corners.length; i++) { if (corners[i].contains( ((Line2D) copyShape).getP1()) || corners[i].contains( ((Line2D) copyShape).getP2())) { g2d.fill(corners[i]); } } } } else { g2d.draw(bBox); if (!dragging) { for (int i = 0; i < corners.length; i++) { g2d.fill(corners[i]); } } } } } } /** * Set the <code>Shape</code> that is referenced by the current active * annotation. * * @param shape the referenced shape */ public void setShape(Shape shape) { //currentShape = shape; if (shape != null) { if (shape instanceof RectangularShape) { copyShape = (RectangularShape) ((RectangularShape) shape).clone(); } else if (shape instanceof GeneralPath) { copyShape = (GeneralPath) ((GeneralPath) shape).clone(); } else if (shape instanceof Line2D) { copyShape = (Line2D) ((Line2D) shape).clone(); adjustActiveLineArea(); } // add Polygon, Point?? if (copyShape != null) { bBox = copyShape.getBounds2D(); adjustAnchors(); } } else { copyShape = null; bBox = null; } repaint(); updateObjectStatus(); } /** * Returns the edited shape. When the changes are commited the shape is * added to the annotation. * * @return the current shape or null */ public Shape getShape() { return copyShape; } /** * Tells the editor pane to create a new Shape from an object in the * library. While the editor only accepts a reference to one single * symbol this is effectively the same as setGraphicalAnnotation. * * @param id the id of the symbol */ public void addGraphicAnnotation(String id) { if (libTable != null) { Shape s = (Shape) libTable.get(id); if (s != null) { setShape(s); // center the shape if (copyShape != null) { double w = copyShape.getBounds().getWidth(); double h = copyShape.getBounds().getHeight(); if (copyShape instanceof RectangularShape) { ((RectangularShape) copyShape).setFrame((image.getWidth( null) / 2) - (w / 2), (image.getHeight(null) / 2) - (h / 2), w, h); bBox.setRect(copyShape.getBounds2D()); adjustAnchors(); } else if (copyShape instanceof Line2D) { int wi = image.getWidth(null); int he = image.getHeight(null); ((Line2D) copyShape).setLine((wi / 2) - (wi / 4), (he / 2) - (he / 4), (wi / 2) + (wi / 4), (he / 2) + (he / 4)); adjustActiveLineArea(); } updateObjectStatus(); } } } } /** * Returns the current zoomlevel. * * @return the current zoomlevel */ public int getZoom() { return zoom; } /** * Sets the current zoomlevel. * * @param zoom the new zoomlevel */ public void setZoom(int zoom) { this.zoom = zoom;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -