public boolean contains(int x, int y) { // If the button has changed size, // make a new shape object. if (shape == null || !shape.getBounds().equals(getBounds())) { shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight()); } return shape.contains(x, y); }
void addShape(Shape shape) { // Add the shape to the canvas, and set its size/position and color. // The shape is added at the top-left corner, with size 80-by-50. // Then redraw the canvas to show the newly added shape. shape.setColor(currentColor); shape.reshape(3, 3, 80, 50); shapes.add(shape); repaint(); }
public void paintComponent(Graphics g) { // In the paint method, all the shapes in ArrayList are // copied onto the canvas. g.setColor(getBackground()); g.fillRect(0, 0, getSize().width, getSize().height); int top = shapes.size(); for (int i = 0; i < top; i++) { Shape s = (Shape) shapes.get(i); s.draw(g); } }
/** * Finds whether a point is within a node * * @param p point * @return node id that the point is in, or -1 if no node contains the point */ public int findNodeClicked(Point p) { double xs = (double) (this.getWidth() - b - (nodes.get(0).r * nodeScaling)); double ys = (double) (this.getHeight() - b - (nodes.get(0).r * nodeScaling) - noticeBorder); for (drawNode N : nodes.values()) { Shape S = new Ellipse2D.Double(N.x * xs, N.y * ys, N.r * nodeScaling, N.r * nodeScaling); if (S.contains(p)) { return N.id; } } return -1; }
public void setCoordinates(int x1, int y1, int x2, int y2) { super.setCoordinates(x1, y1, x2, y2); startX = x1 - this.x1; startY = y1 - this.y1; endX = x2 - this.x1; endY = y2 - this.y1; }
public void paint(Graphics g) { g.setColor(Color.black); super.paint(g); int h = Math.abs(y1 - y2) - 1, w = Math.abs(x1 - x2) - 1; // int x0=x1>x2?x2:x1, y0=y1>y2?y2:y1; int x0 = 0, y0 = 0; g.drawRect(x0, y0, w, h); }
public void mouseDragged(MouseEvent evt) { // User has moved the mouse. Move the dragged shape by the same amount. int x = evt.getX(); int y = evt.getY(); if (shapeBeingDragged != null) { shapeBeingDragged.moveBy(x - prevDragX, y - prevDragY); prevDragX = x; prevDragY = y; repaint(); // redraw canvas to show shape in new position } }
public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException { int p0 = getStartOffset(); int p1 = getEndOffset(); if ((pos >= p0) && (pos <= p1)) { Rectangle r = a.getBounds(); if (pos == p1) { r.x += r.width; } r.width = 0; return r; } throw new BadLocationException(pos + " not in range " + p0 + "," + p1, pos); }
public void mousePressed(MouseEvent evt) { // User has pressed the mouse. Find the shape that the user has clicked on, if // any. If there is a shape at the position when the mouse was clicked, then // start dragging it. If the user was holding down the shift key, then bring // the dragged shape to the front, in front of all the other shapes. int x = evt.getX(); // x-coordinate of point where mouse was clicked int y = evt.getY(); // y-coordinate of point for (int i = shapes.size() - 1; i >= 0; i--) { // check shapes from front to back Shape s = (Shape) shapes.get(i); if (s.containsPoint(x, y)) { shapeBeingDragged = s; prevDragX = x; prevDragY = y; if (evt.isShiftDown()) { // Bring the shape to the front by moving it to shapes.remove(s); // the end of the list of shapes. shapes.add(s); repaint(); // repaint canvas to show shape in front of other shapes } return; } } }
public void paint(Graphics g, Shape a) { Graphics2D g2 = (Graphics2D) g; Rectangle2D abounds = a.getBounds2D(); AffineTransform saveTransform = g2.getTransform(); Paint savePaint = g2.getPaint(); try { g2.translate(abounds.getX() - bounds.getX(), abounds.getY() - bounds.getY()); g2.setPaint(Color.BLACK); // FIXME p.paint(g2); } finally { g2.setTransform(saveTransform); g2.setPaint(savePaint); } }
public void render(int w, int h, Graphics2D g2) { FontRenderContext frc = g2.getFontRenderContext(); for (int i = 0; i < 2; i++) { layouts[i] = new TextLayout(text[i], fonts[i], frc); float rw = layouts[i].getAdvance(); float rh = layouts[i].getAscent() + layouts[i].getDescent(); float rx = (float) ((w - rw) / 2); float ry = (float) ((i == 0) ? h / 3 : h * 0.75f); // draw highlighted shape Shape hilite = layouts[i].getLogicalHighlightShape(0, curPos[i]); AffineTransform at = AffineTransform.getTranslateInstance(rx, ry); hilite = at.createTransformedShape(hilite); float hy = (float) hilite.getBounds2D().getY(); float hh = (float) hilite.getBounds2D().getHeight(); g2.setColor(colors[i]); g2.fill(hilite); // get caret shape Shape[] shapes = layouts[i].getCaretShapes(curPos[i]); Shape caret = at.createTransformedShape(shapes[0]); g2.setColor(Color.black); layouts[i].draw(g2, rx, ry); g2.draw(caret); g2.draw(new Rectangle2D.Float(rx, hy, rw, hh)); // Display character advances. for (int j = 0; j <= layouts[i].getCharacterCount(); j++) { float[] cInfo = layouts[i].getCaretInfo(TextHitInfo.leading(j)); String str = String.valueOf((int) cInfo[0]); TextLayout tl = new TextLayout(str, smallF, frc); tl.draw(g2, (float) rx + cInfo[0] - tl.getAdvance() / 2, hy + hh + tl.getAscent() + 1.0f); } } }
/* * Creates a line of width EDGE_SELECT_WIDTH for each edge * and tests if mouse click was in that Shape's boundary. * Returns the edge if one was selected, null otherwise. */ public TreeEdge testEdgeShapes(MouseEvent event) { if (argument == null || argument.getTree() == null) return null; double x = event.getX(); double y = event.getY(); BasicStroke edgeWidth = new BasicStroke(EDGE_SELECT_WIDTH); if (argument.getBreadthFirstTraversal() == null) return null; Enumeration nodeList = argument.getBreadthFirstTraversal().elements(); while (nodeList.hasMoreElements()) { TreeVertex vertex = (TreeVertex) nodeList.nextElement(); if (argument.isMultiRoots() && vertex.getLayer() == 0) continue; Enumeration edges = vertex.getEdgeList().elements(); while (edges.hasMoreElements()) { TreeEdge edge = (TreeEdge) edges.nextElement(); Shape shape = edge.getShape(this); Shape wideEdge = edgeWidth.createStrokedShape(shape); TreeVertex child = edge.getDestVertex(); if (wideEdge.contains(x, y)) { edge.setSelected(!edge.isSelected()); return edge; } } } return null; }
/** this method paints the line. */ public void paint(Graphics g) { super.paint(g); if (GlobalDebug.isOn) System.out.println("Line.paint()"); g.setColor(Color.black); g.drawLine(startX, startY, endX - 1, endY - 1); // g.drawLine(0,0,30,30); /* if (y1 <= y2) { g.drawLine (0,0,x2-x1-1,y2-y1-1); } else { g.drawLine (0,y1-y2-1,x2-x1-1,0); } */ }
// record where the mouse gets pressed public void mousePressed(MouseEvent e) { x = pressx = e.getX(); y = pressy = e.getY(); Point p0 = new Point(x, y); Point p1 = new Point(pressx, pressy); if (mode == 0) { theShape = theLine = new Line(p0, p1); } else if (mode == 1) { theShape = theBox = new Box(p0, p1); } allTheShapes[allTheShapesCount++] = theShape; theShape.color = theColorPicker.b.color; }
public void mouseReleased(MouseEvent evt) { // User has released the mouse. Move the dragged shape, then set // shapeBeingDragged to null to indicate that dragging is over. // If the shape lies completely outside the canvas, remove it // from the list of shapes (since there is no way to ever move // it back onscreen). int x = evt.getX(); int y = evt.getY(); if (shapeBeingDragged != null) { shapeBeingDragged.moveBy(x - prevDragX, y - prevDragY); if (shapeBeingDragged.left >= getSize().width || shapeBeingDragged.top >= getSize().height || shapeBeingDragged.left + shapeBeingDragged.width < 0 || shapeBeingDragged.top + shapeBeingDragged.height < 0) { // shape is off-screen shapes.remove(shapeBeingDragged); // remove shape from list of shapes } shapeBeingDragged = null; repaint(); } }
public Shape createStrokedShape(Shape shape) { GeneralPath result = new GeneralPath(); PathIterator it = new FlatteningPathIterator(shape.getPathIterator(null), FLATNESS); float points[] = new float[6]; float moveX = 0, moveY = 0; float lastX = 0, lastY = 0; float thisX = 0, thisY = 0; int type = 0; boolean first = false; float next = 0; int phase = 0; float factor = 1; while (!it.isDone()) { type = it.currentSegment(points); switch (type) { case PathIterator.SEG_MOVETO: moveX = lastX = points[0]; moveY = lastY = points[1]; result.moveTo(moveX, moveY); first = true; next = wavelength / 2; break; case PathIterator.SEG_CLOSE: points[0] = moveX; points[1] = moveY; // Fall into.... case PathIterator.SEG_LINETO: thisX = points[0]; thisY = points[1]; float dx = thisX - lastX; float dy = thisY - lastY; float distance = (float) Math.sqrt(dx * dx + dy * dy); if (distance >= next) { float r = 1.0f / distance; float angle = (float) Math.atan2(dy, dx); while (distance >= next) { float x = lastX + next * dx * r; float y = lastY + next * dy * r; float tx = amplitude * dy * r; float ty = amplitude * dx * r; if ((phase & 1) == 0) result.lineTo(x + amplitude * dy * r, y - amplitude * dx * r); else result.lineTo(x - amplitude * dy * r, y + amplitude * dx * r); next += wavelength; phase++; } } next -= distance; first = false; lastX = thisX; lastY = thisY; if (type == PathIterator.SEG_CLOSE) result.closePath(); break; } it.next(); } // return stroke.createStrokedShape( result ); return result; }
// Graphics2D g=getG();return g==null?null:g.getClip();} public Rectangle getClipBounds() { Shape s = bufferClip(); return s == null ? null : s.getBounds(); }
@Override public int getIconWidth() { return star.getBounds().width; }
@Override public int getIconHeight() { return star.getBounds().height; }
// ========================================================= drawCurrentShape private void drawCurrentShape(Graphics2D g2) { g2.setColor(_color); // Set the color. _shape.draw(g2, _start, _end); // Draw the shape. }
// Main program -- create and start GUI public Mesh(boolean debug) { // Create drawing area for shape worldDraw = new WorldView(this, shape, debug); worldDraw.setSize(500, 500); // Create menubar JMenuBar menubar = new JMenuBar(); setJMenuBar(menubar); JMenu menu = new JMenu("File"); menu.getPopupMenu().setLightWeightPopupEnabled(false); menubar.add(menu); // Exit when quit selected JMenuItem resetm = menu.add("Reset"); resetm.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { Parameter.blockAction(true); // Reset all parameters shape.reset(); Parameter.blockAction(false); Parameter.onUserAction(); } }); // Exit when quit selected JMenuItem quitm = menu.add("Quit"); quitm.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); // ------------------------------------------------------ // Lay out main window GridBagLayout layout = new GridBagLayout(); GridBagConstraints con = new GridBagConstraints(); Container c = getContentPane(); c.setLayout(layout); // World viewport con.gridwidth = 1; con.weightx = 0.2; con.weighty = 1.0; con.fill = GridBagConstraints.BOTH; con.insets = new Insets(4, 4, 4, 2); layout.setConstraints(worldDraw, con); c.add(worldDraw); // -- Parameter controls // Make container for controls Container cc = new Container(); con.gridwidth = GridBagConstraints.REMAINDER; con.weightx = 0.2; con.weighty = 1.0; con.fill = GridBagConstraints.HORIZONTAL; layout.setConstraints(cc, con); c.add(cc); // Fill compartment GridBagLayout clayout = new GridBagLayout(); cc.setLayout(clayout); GridBagConstraints ccon = new GridBagConstraints(); ccon.weightx = 1.0; ccon.weighty = 1.0; ccon.anchor = GridBagConstraints.NORTH; ccon.insets = new Insets(10, 10, 0, 10); ccon.fill = GridBagConstraints.BOTH; ccon.gridwidth = GridBagConstraints.REMAINDER; makeControls(cc, clayout, ccon, shape.getParams(), shape.getOptions(), "Object parameters"); // ------------------------------------------------------ // Exit when window closes addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); // Placement of window on screen setLocation(100, 50); pack(); setVisible(true); }
/** * Paints the image. * * @param g the rendering surface to use * @param a the allocated region to render into * @see View#paint */ public void paint(Graphics g, Shape a) { Color oldColor = g.getColor(); fBounds = a.getBounds(); int border = getBorder(); int x = fBounds.x + border + getSpace(X_AXIS); int y = fBounds.y + border + getSpace(Y_AXIS); int width = fWidth; int height = fHeight; int sel = getSelectionState(); // Make sure my Component is in the right place: /* if( fComponent == null ) { fComponent = new Component() { }; fComponent.addMouseListener(this); fComponent.addMouseMotionListener(this); fComponent.setCursor(Cursor.getDefaultCursor()); // use arrow cursor fContainer.add(fComponent); } fComponent.setBounds(x,y,width,height); */ // If no pixels yet, draw gray outline and icon: if (!hasPixels(this)) { g.setColor(Color.lightGray); g.drawRect(x, y, width - 1, height - 1); g.setColor(oldColor); loadIcons(); Icon icon = fImage == null ? sMissingImageIcon : sPendingImageIcon; if (icon != null) icon.paintIcon(getContainer(), g, x, y); } // Draw image: if (fImage != null) { g.drawImage(fImage, x, y, width, height, this); // Use the following instead of g.drawImage when // BufferedImageGraphics2D.setXORMode is fixed (4158822). // Use Xor mode when selected/highlighted. // ! Could darken image instead, but it would be more expensive. /* if( sel > 0 ) g.setXORMode(Color.white); g.drawImage(fImage,x, y, width,height,this); if( sel > 0 ) g.setPaintMode(); */ } // If selected exactly, we need a black border & grow-box: Color bc = getBorderColor(); if (sel == 2) { // Make sure there's room for a border: int delta = 2 - border; if (delta > 0) { x += delta; y += delta; width -= delta << 1; height -= delta << 1; border = 2; } bc = null; g.setColor(Color.black); // Draw grow box: g.fillRect(x + width - 5, y + height - 5, 5, 5); } // Draw border: if (border > 0) { if (bc != null) g.setColor(bc); // Draw a thick rectangle: for (int i = 1; i <= border; i++) g.drawRect(x - i, y - i, width - 1 + i + i, height - 1 + i + i); g.setColor(oldColor); } }