Java Code Examples for java.awt.Graphics2D.drawLine()

The following are Jave code examples for showing how to use drawLine() of the java.awt.Graphics2D class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: ramus   File: SectorRowsEditor.java   View Source Code Vote up 7 votes
@Override
public void paint(final Graphics gr) {
    super.paint(gr);
    int l = getWidth();
    if (l > getHeight())
        l = getHeight();
    l /= 2;
    final int x = (getWidth() - l) / 2;
    final int y = (getHeight() - l) / 2;
    final Graphics2D g = (Graphics2D) gr;

    g.setColor(getBackground());
    final Insets is = getBorder().getBorderInsets(this);
    g.fillRect(is.left, is.right, getWidth() - is.right - is.left,
            getHeight() - is.bottom - is.left);
    final Color f = getForeground();
    g.setColor(new Color(255 - (255 - f.getRed()) / 2, 255 - (255 - f
            .getGreen()) / 2, 255 - (255 - f.getBlue()) / 2));
    g.setStroke(new BasicStroke(3.0f/*
                                     * , BasicStroke.CAP_ROUND,
			 * BasicStroke.JOIN_ROUND
			 */));
    g.drawLine(x, y, x + l, y + l);
    g.drawLine(x, y + l, x + l, y);

}
 
Example 2
Project: the-one-mdonnyk   File: PathGraphic.java   View Source Code Vote up 6 votes
/**
 * Draws a line trough all path's coordinates.
 * @param g2 The graphics context to draw to
 */
@Override
public void draw(Graphics2D g2) {
	if (coords == null) {
		return;
	}
	
	g2.setColor(PATH_COLOR);
	Coord prev = coords.get(0);
	
	for (int i=1, n=coords.size(); i < n; i++) {
		Coord next = coords.get(i);
		g2.drawLine(scale(prev.getX()), scale(prev.getY()),
				scale(next.getX()), scale(next.getY()));
		prev = next;
	}
}
 
Example 3
Project: incubator-netbeans   File: PalettePanel.java   View Source Code Vote up 6 votes
@Override
protected void paintComponent(Graphics g) {
    if( isAquaLaF && g instanceof Graphics2D ) {
        Graphics2D g2d = (Graphics2D) g;
        Color col1 = UIManager.getColor("NbExplorerView.quicksearch.background.top"); //NOI18N
        Color col2 = UIManager.getColor("NbExplorerView.quicksearch.background.bottom"); //NOI18N
        Color col3 = UIManager.getColor("NbExplorerView.quicksearch.border"); //NOI18N
        if( col1 != null && col2 != null && col3 != null ) {
            g2d.setPaint( new GradientPaint(0, 0, col1, 0, getHeight(), col2));
            g2d.fillRect(0, 0, getWidth(), getHeight());
            g2d.setColor( col3 );
            g2d.drawLine(0, 0, getWidth(), 0);
            return;
        }
    }
    super.paintComponent(g);
    g.setColor( UIManager.getColor( "PropSheet.setBackground" ) ); //NOI18N
    g.drawLine(0, 0, getWidth(), 0);
}
 
Example 4
Project: geomapapp   File: CoreDescriptionsDisplay.java   View Source Code Vote up 6 votes
public void drawLineAtAge( int currentAge )	{
	if ( exists ) {
		synchronized (getTreeLock()) {
			Graphics2D g = (Graphics2D)getGraphics();
			Rectangle r = getVisibleRect();
			int x1 = r.x;
			int x2 = r.x+r.width;
			g.setXORMode( Color.cyan );
			if ( prevAge != -1)	{
				g.drawLine(x1, prevAge, x2, prevAge);
			}
			g.drawLine(x1, currentAge, x2, currentAge);
			prevAge = currentAge;
		}
	}
}
 
Example 5
Project: VASSAL-src   File: IconButton.java   View Source Code Vote up 5 votes
public IconButton(int type, int size, Color color, float width) {
  super();

  setMinimumSize(new Dimension(size, size));
  setPreferredSize(new Dimension(size, size));

  final BufferedImage image =
    ImageUtils.createCompatibleTranslucentImage(size, size);

  final Graphics2D g = image.createGraphics();
  g.setStroke(new BasicStroke(width));
  g.setColor(color);

  switch (type) {
  case PLUS_ICON:
    g.drawLine(5, size / 2, size - 5, size / 2);
    g.drawLine(size / 2, 5, size / 2, size - 5);
    break;
  case MINUS_ICON:
    g.drawLine(5, size / 2, size - 5, size / 2);
    break;
  case TICK_ICON:
    g.drawLine(5, size/2, size/2, size-5);
    g.drawLine(size/2, size-5, 5, size-5);
    break;
  case CROSS_ICON:
    g.drawLine(5, 5, size-5, size-5);
    g.drawLine(5, size-5, size-5, 5);
    break;
  }
  setIcon(new ImageIcon(image));
}
 
Example 6
Project: AWGW   File: GridPanel.java   View Source Code Vote up 5 votes
/**
 * Draw the gridlines for the grid. We only draw the portion of the
 * lines that intersect the current clipping bounds.
 *
 * @param g2 the Graphics2 object to use to render
 */
private void drawGridlines(Graphics2D g2) {
    Rectangle curClip = g2.getClip().getBounds();
    int top = getInsets().top, left = getInsets().left;

    int miny = Math.max(0, (curClip.y - top) / (cellSize + 1)) * (cellSize + 1) + top;
    int minx = Math.max(0, (curClip.x - left) / (cellSize + 1)) * (cellSize + 1) + left;
    int maxy = Math.min(numRows,
            (curClip.y + curClip.height - top + cellSize) / (cellSize + 1))
            * (cellSize + 1) + top;
    int maxx = Math.min(numCols,
            (curClip.x + curClip.width - left + cellSize) / (cellSize + 1))
            * (cellSize + 1) + left;

    g2.setColor(Color.GRAY);
    for (int y = miny; y <= maxy; y += cellSize + 1)
        for (int x = minx; x <= maxx; x += cellSize + 1) {
            Location loc = locationForPoint(
                    new Point(x + cellSize / 2, y + cellSize / 2));
            if (loc != null && !grid.isValid(loc))
                g2.fillRect(x + 1, y + 1, cellSize, cellSize);
        }

    g2.setColor(Color.BLACK);
    for (int y = miny; y <= maxy; y += cellSize + 1)
        // draw horizontal lines
        g2.drawLine(minx, y, maxx, y);

    for (int x = minx; x <= maxx; x += cellSize + 1)
        // draw vertical lines
        g2.drawLine(x, miny, x, maxy);
}
 
Example 7
Project: openjdk-jdk10   File: IncorrectTextSize.java   View Source Code Vote up 5 votes
public static void main(final String[] args) throws IOException {
    for (int  point = 5; point < 11; ++point) {
        Graphics2D g2d = bi.createGraphics();
        g2d.setFont(new Font(Font.DIALOG, Font.PLAIN, point));
        g2d.scale(scale, scale);
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, width, height);
        g2d.setColor(Color.green);
        g2d.drawString(TEXT, 0, 20);
        int length = g2d.getFontMetrics().stringWidth(TEXT);
        if (length < 0) {
            throw new RuntimeException("Negative length");
        }
        for (int i = (length + 1) * scale; i < width; ++i) {
            for (int j = 0; j < height; ++j) {
                if (bi.getRGB(i, j) != Color.white.getRGB()) {
                    g2d.drawLine(length, 0, length, height);
                    ImageIO.write(bi, "png", new File("image.png"));
                    System.out.println("length = " + length);
                    System.err.println("Wrong color at x=" + i + ",y=" + j);
                    System.err.println("Color is:" + new Color(bi.getRGB(i,
                                                                         j)));
                    throw new RuntimeException("Test failed.");
                }
            }
        }
        g2d.dispose();
    }
}
 
Example 8
Project: geomapapp   File: PDBDataSetGraph.java   View Source Code Vote up 5 votes
/**
	Draws the last line in polygon poly to XYGraph xyg
	Uses XOR mode
*/
void drawLasso(XYGraph xyg) {
	synchronized (xyg.getTreeLock()) {
		Graphics2D g = (Graphics2D) xyg.getGraphics(); 
		g.setXORMode(Color.GRAY);
		int x1 = poly.xpoints[poly.npoints-2];
		int y1 = poly.ypoints[poly.npoints-2];
		int x2 = poly.xpoints[poly.npoints-1];
		int y2 = poly.ypoints[poly.npoints-1];
		g.drawLine(x1, y1, x2, y2);
	}
}
 
Example 9
Project: geomapapp   File: PDBDataSetGraph.java   View Source Code Vote up 5 votes
void drawLine(XYGraph xyg) {
	if( Float.isNaN(lastV) || side==0) return;
	synchronized (xyg.getTreeLock()) {
		Graphics2D g = (Graphics2D)xyg.getGraphics();
		g.transform(dat);
		g.setXORMode( Color.white );
		g.setColor(Color.blue);
		if (side<0) g.drawLine((int)((lastV-x0)*xScale), y1, (int)((lastV-x0)*xScale), y2);
		else g.drawLine(x1, (int)((lastV-y0)*-yScale), x2, (int)((lastV-y0)*-yScale));
	}
}
 
Example 10
Project: incubator-netbeans   File: HighlightsViewUtils.java   View Source Code Vote up 5 votes
static void paintTextLimitLine(Graphics2D g, DocumentView docView, int x, int y, int lastX, int lastY) {
    int textLimitLineX = docView.op.getTextLimitLineX();
    if (textLimitLineX > 0 && textLimitLineX >= x && textLimitLineX <= lastX) {
        g.setColor(docView.op.getTextLimitLineColor());
        g.drawLine(textLimitLineX, y, textLimitLineX, lastY);
    }
}
 
Example 11
Project: rapidminer   File: PlotterAdapter.java   View Source Code Vote up 5 votes
/** This method can be used to draw a legend on the given graphics context. */
public void drawSimpleNumericalLegend(Graphics graphics, int x, int y, String legendColumnName, String minColorString,
		String maxColorString) {
	Graphics2D g = (Graphics2D) graphics.create();

	// painting label name
	g.setFont(LABEL_FONT_BOLD);
	g.setColor(Color.black);
	g.drawString(legendColumnName, x, y + 1);
	Rectangle2D legendNameBounds = LABEL_FONT.getStringBounds(legendColumnName, g.getFontRenderContext());
	g.translate(legendNameBounds.getWidth() + 5, 0);

	// painting legend
	g.setFont(LABEL_FONT);
	g.setColor(Color.black);
	Rectangle2D minStringBounds = LABEL_FONT.getStringBounds(minColorString, g.getFontRenderContext());

	int keyX = x;
	int keyY = y;

	g.drawString(minColorString, keyX, keyY + 1);
	keyX += minStringBounds.getWidth() + 5;
	for (int i = 0; i < 100; i++) {
		double scaledColor = i / 100.0d;
		Color lineColor = getColorProvider().getPointColor(scaledColor, 255);
		g.setColor(lineColor);
		g.drawLine(keyX, keyY, keyX, keyY - 8);
		keyX++;
	}
	g.setColor(Color.black);
	Rectangle2D frame = new Rectangle2D.Double(keyX - 101, keyY - 8, 101, 8);
	g.draw(frame);
	keyX += 4;
	g.drawString(maxColorString, keyX, keyY + 1);
}
 
Example 12
Project: geomapapp   File: DataSetGraph.java   View Source Code Vote up 5 votes
/**
	undraws the polygon represented by poly
*/
void unDrawLasso(XYGraph xyg){
	synchronized (xyg.getTreeLock()) {
		Graphics2D g = (Graphics2D) xyg.getGraphics(); 
		g.setXORMode(Color.GRAY);
		for(int i=1;i<poly.npoints;i++) {
			g.drawLine(poly.xpoints[i-1], poly.ypoints[i-1], poly.xpoints[i], poly.ypoints[i]);
		}
	}
}
 
Example 13
Project: Logisim   File: TransmissionGate.java   View Source Code Vote up 4 votes
private void drawInstance(InstancePainter painter, boolean isGhost) {
	Bounds bds = painter.getBounds();
	Object powerLoc = painter.getAttributeValue(Wiring.ATTR_GATE);
	Direction facing = painter.getAttributeValue(StdAttr.FACING);
	boolean flip = (facing == Direction.SOUTH || facing == Direction.WEST) == (powerLoc == Wiring.GATE_TOP_LEFT);

	int degrees = Direction.WEST.toDegrees() - facing.toDegrees();
	if (flip)
		degrees += 180;
	double radians = Math.toRadians((degrees + 360) % 360);

	Graphics2D g = (Graphics2D) painter.getGraphics().create();
	g.rotate(radians, bds.getX() + 20, bds.getY() + 20);
	g.translate(bds.getX(), bds.getY());
	GraphicsUtil.switchToWidth(g, Wire.WIDTH);

	Color gate0 = g.getColor();
	Color gate1 = gate0;
	Color input = gate0;
	Color output = gate0;
	Color platform = gate0;
	if (!isGhost && painter.getShowState()) {
		gate0 = painter.getPort(GATE0).getColor();
		gate1 = painter.getPort(GATE0).getColor();
		input = painter.getPort(INPUT).getColor();
		output = painter.getPort(OUTPUT).getColor();
		platform = computeOutput(painter).getColor();
	}

	g.setColor(flip ? input : output);
	g.drawLine(0, 20, 11, 20);
	g.drawLine(11, 13, 11, 27);

	g.setColor(flip ? output : input);
	g.drawLine(29, 20, 40, 20);
	g.drawLine(29, 13, 29, 27);

	g.setColor(gate0);
	g.drawLine(20, 35, 20, 40);
	GraphicsUtil.switchToWidth(g, 1);
	g.drawOval(18, 30, 4, 4);
	g.drawLine(10, 30, 30, 30);
	GraphicsUtil.switchToWidth(g, Wire.WIDTH);

	g.setColor(gate1);
	g.drawLine(20, 9, 20, 0);
	GraphicsUtil.switchToWidth(g, 1);
	g.drawLine(10, 10, 30, 10);

	g.setColor(platform);
	g.drawLine(9, 12, 31, 12);
	g.drawLine(9, 28, 31, 28);
	if (flip) { // arrow
		g.drawLine(18, 17, 21, 20);
		g.drawLine(18, 23, 21, 20);
	} else {
		g.drawLine(22, 17, 19, 20);
		g.drawLine(22, 23, 19, 20);
	}

	g.dispose();
}
 
Example 14
Project: SE2017-Team1   File: TestUtil.java   View Source Code Vote up 4 votes
public static void drawAct(Graphics2D g2d, Point center, String title, String bodyText,
		boolean isExec, boolean isPend, boolean isPetri) {
	
	int leftX = center.x - Constants.ACTIVITY_WIDTH / 2;
	int rightX = center.x + Constants.ACTIVITY_WIDTH /2;
	int upperY = center.y - Constants.ACTIVITY_HEIGHT / 2;
	int lineY = upperY + Constants.HEADER_HEIGHT;
	
    // border
	g2d.setColor(Constants.ACTIVITY_BACKGROUND_COLOR);
	g2d.fillRect(leftX, upperY, Constants.ACTIVITY_WIDTH, Constants.ACTIVITY_HEIGHT);
	g2d.setColor(Color.BLACK);
	g2d.drawRect(leftX, upperY, Constants.ACTIVITY_WIDTH, Constants.ACTIVITY_HEIGHT);
	g2d.drawLine(leftX, lineY, rightX, lineY);
	
	// font
	g2d.setFont(Constants.BASIC_FONT);
	FontMetrics metrics = g2d.getFontMetrics(g2d.getFont());
	
	// title
    int xTitle = leftX + (Constants.ACTIVITY_WIDTH - metrics.stringWidth(title)) / 2;
    int yTitle = upperY + ((Constants.HEADER_HEIGHT - metrics.getHeight()) / 2) + metrics.getAscent();
    g2d.drawString(title, xTitle, yTitle);
	  
    // body text
    int xBody = leftX + (Constants.ACTIVITY_WIDTH - metrics.stringWidth(bodyText)) / 2;
    int yBody = upperY + Constants.LINE_HEIGHT + Constants.HEADER_HEIGHT + 9 + g2d.getFontMetrics().getHeight();
    g2d.drawString(bodyText, xBody, yBody);
    
    // checkmark
    if (isExec) {
    	g2d.setFont(Constants.CHECKMARK_FONT);
	    g2d.setColor(Constants.CHECKMARK_COLOR);
	    int xCheck = leftX + Constants.PETRI_MARGIN;
	    int yCheck = upperY + Constants.HEADER_HEIGHT + 20;
	    g2d.drawString(Constants.CHECKMARK_SYMB, xCheck, yCheck);
    }
    
    // exclamation mark
    if (isPend) {
    	g2d.setFont(Constants.EXCLAMATION_FONT);
	    g2d.setColor(Constants.EXCLAMATION_COLOR);
	    int xExclam = rightX - Constants.PETRI_MARGIN - 10;
	    int yExclam = upperY + Constants.HEADER_HEIGHT + 20;
	    g2d.drawString(Constants.EXCLAMATION_SYMB, xExclam, yExclam);
    }
    
    // Petri
    if (isPetri) {
    	g2d.setColor(Color.BLACK);
	    g2d.setFont(Constants.BASIC_FONT);
	    g2d.drawRect(leftX + Constants.PETRI_MARGIN, lineY + Constants.PETRI_MARGIN,
			Constants.ACTIVITY_WIDTH - 2 * Constants.PETRI_MARGIN, Constants.ACTIVITY_HEIGHT - Constants.HEADER_HEIGHT - 2 * Constants.PETRI_MARGIN);
    } 
}
 
Example 15
Project: incubator-netbeans   File: DropTargetLayer.java   View Source Code Vote up 4 votes
private static void drawVerticalTargetLine(Graphics2D g, int x, int y, int len) {
    g.setColor(DROP_TARGET_COLOR);
    g.setStroke(DROP_TARGET_LINE_STROKE);
    g.drawLine(x-1, y, x-1, y+len);
}
 
Example 16
Project: SE2017-Team2   File: CrDrawing.java   View Source Code Vote up 4 votes
/**
 * Draws an arrow representing a relation
 * @param g
 * @param e1
 * @param e2
 * @param c
 * @param id
 * @param isConditional
 */
private void drawArrow(Graphics2D g, Event e1, Event e2, Color c, String id, boolean isConditional) {
	Tuple<Position, Position> tuple = shortest(e1, e2);
	Position p1 = tuple.f;
	Position p2 = tuple.s;
	relationPos.add(p1); 
	relationPos.add(p2); 
	AffineTransform oldXForm = g.getTransform();
	g.setColor(c);
	int ARR_SIZE = 8;
	Stroke oldS = g.getStroke();
	int strokeSize = ARR_SIZE/4;
	Stroke newS = new BasicStroke(strokeSize);
	g.setStroke(newS);
		
	double dx = p2.x() - p1.x(), dy = p2.y() - p1.y();
	double angle = Math.atan2(dy, dx);
	
	int len = (int) Math.sqrt(dx*dx + dy*dy);
	AffineTransform at = AffineTransform.getTranslateInstance(p1.x(), p1.y());
	at.concatenate(AffineTransform.getRotateInstance(angle));
	g.transform(at);
	Font fontUp = new Font("Times New Roman", Font.BOLD, 18);
	Font fontDw = new Font("Times New Roman", Font.BOLD, -18);
	FontMetrics mUp = g.getFontMetrics(fontUp);
	if (isConditional) {
		if (p1.x() > p2.x()) {
			g.setFont(fontDw);
			g.drawString(id, (len/2), -strokeSize+MARGIN);
		}
		else {
			g.setFont(fontUp);
			g.drawString(id, (len/2)-mUp.stringWidth(id), -strokeSize);
		}
		len -= CIRCLE_RADIUS;
		g.drawLine(0, 0, len-ARR_SIZE, 0);
		g.fillRoundRect(len, -(CIRCLE_RADIUS/2), CIRCLE_RADIUS, CIRCLE_RADIUS, CIRCLE_RADIUS, CIRCLE_RADIUS);
	}
	else {
		if (p1.x() > p2.x()) {
			g.setFont(fontDw);
			g.drawString(id, CIRCLE_RADIUS+(len/2), -strokeSize+MARGIN);
		}
		else {
			g.setFont(fontUp);
			g.drawString(id, CIRCLE_RADIUS+(len/2)-mUp.stringWidth(id), -strokeSize);
		}
		g.drawLine(CIRCLE_RADIUS, 0, len-ARR_SIZE, 0);
		g.fillRoundRect(0, -(CIRCLE_RADIUS/2), CIRCLE_RADIUS, CIRCLE_RADIUS, CIRCLE_RADIUS, CIRCLE_RADIUS);
	}
	g.fillPolygon(new int[] {len, len-ARR_SIZE, len-ARR_SIZE, len},
                    new int[] {0, -ARR_SIZE, ARR_SIZE, 0}, 4);
	//reset
	g.setStroke(oldS);
	g.setTransform(oldXForm);
}
 
Example 17
Project: JGraphix   File: JGraphix_Example_1.java   View Source Code Vote up 4 votes
public static void main(String[] args) {
	DrawFrame df = DrawFrame.create("MyDrawing", 200, 300);
	Graphics2D g = df.getGraphics2D();
	
	float linewidth = 5.0f;	// line width = 5 pixels
	g.setStroke(new BasicStroke(linewidth));
	
	g.setColor(Color.BLUE);
	g.drawLine(40, 10, 10, 40);
	g.draw(new Line2D.Double(70.2, 10.3, 100.4, 40.5));

	g.fillOval(10, 60, 30, 30);
	g.drawOval(60, 60, 30, 30);
	g.fillRoundRect(110, 60, 30, 30, 10, 10);
	g.drawRoundRect(160, 60, 30, 30, 10, 10);

	g.setColor(Color.GREEN.darker());
	g.fillArc(10, 110, 30, 30, 45, 240);
	g.fillArc(60, 110, 30, 30, 45 + 240, 360 - 240);
	g.fillArc(110, 110, 30, 30, 90, 270);
	g.fillArc(160, 110, 30, 30, 270, 270);

	g.setColor(Color.MAGENTA);
	g.drawArc(10, 160, 30, 30, 45, 240);
	g.drawArc(60, 160, 30, 30, 45 + 240, 360 - 240);
	g.drawArc(110, 160, 30, 30, 90, 270);
	g.drawArc(160, 160, 30, 30, 270, 270);

	g.setColor(Color.ORANGE);
	g.fillPolygon(new int[] { 10, 40, 10, 40 }, new int[] { 210, 210, 240, 240 }, 4);
	g.drawPolygon(new int[] { 60, 90, 60, 90 }, new int[] { 210, 210, 240, 240 }, 4);
	g.drawPolyline(new int[] { 110, 140, 110, 140 }, new int[] { 210, 210, 240, 240 }, 4);
	g.drawPolyline(new int[] { 160, 160, 190, 190 }, new int[] {240, 210, 240, 210 }, 4);

	// Printing texts
	g.setColor(Color.BLACK);
	g.setFont(new Font("Monospaced", Font.PLAIN, 14));
	g.drawString("Drawn with JGraphix", 10, 275);

	df.refresh();

	System.out.println("done.");
}
 
Example 18
Project: SE2017-Team1   File: TestUtil.java   View Source Code Vote up 4 votes
public static void drawResp(
		Graphics2D g2d,
		int circleRespX, int circleRespY,
		int triangleRespX, int triangleRespY,
		char trianglePos, int addToLineX, int addToLineY) {
		  
	g2d.setColor(Constants.RESPONSE_RELATION_COLOR);
		  
	// circle
	g2d.fillOval(circleRespX - Constants.DOT_SIZE / 2, circleRespY - Constants.DOT_SIZE /2, Constants.DOT_SIZE, Constants.DOT_SIZE);
	
	// triangle
	Point pResp1 = new Point(triangleRespX, triangleRespY);
	int pResp2X = 0;
	int pResp3X = 0;
	int pResp2Y = 0;
	int pResp3Y = 0;
	  
	switch(trianglePos) {
	  	case 'U':
	  		pResp2X = pResp1.x - Constants.TRIANGLE_HEIGHT / 2;
	  		pResp3X = pResp1.x + Constants.TRIANGLE_HEIGHT / 2;
	  		pResp2Y = pResp1.y + Constants.TRIANGLE_HEIGHT;
	  		pResp3Y = pResp1.y + Constants.TRIANGLE_HEIGHT;
	  		break;
	  	case 'D':
	  		pResp2X = pResp1.x - Constants.TRIANGLE_HEIGHT / 2;
	  		pResp3X = pResp1.x + Constants.TRIANGLE_HEIGHT / 2;
	  		pResp2Y = pResp1.y - Constants.TRIANGLE_HEIGHT;
	  		pResp3Y = pResp1.y - Constants.TRIANGLE_HEIGHT;
	  		break;
	  	case 'L':
	  		pResp2X = pResp1.x + Constants.TRIANGLE_HEIGHT;
	  		pResp3X = pResp1.x + Constants.TRIANGLE_HEIGHT;
	  		pResp2Y = pResp1.y - Constants.TRIANGLE_HEIGHT / 2;
	  		pResp3Y = pResp1.y + Constants.TRIANGLE_HEIGHT / 2;
	  		break;
	  	case 'R':
	  		pResp2X = pResp1.x - Constants.TRIANGLE_HEIGHT;
	  		pResp3X = pResp1.x - Constants.TRIANGLE_HEIGHT;
	  		pResp2Y = pResp1.y - Constants.TRIANGLE_HEIGHT / 2;
	  		pResp3Y = pResp1.y + Constants.TRIANGLE_HEIGHT / 2;
	  		break;
	}
	
	Point pResp2 = new Point(pResp2X, pResp2Y);
	Point pResp3 = new Point(pResp3X, pResp3Y);
	int[] xsResp = {pResp1.x, pResp2.x, pResp3.x};
	int[] ysResp = {pResp1.y, pResp2.y, pResp3.y};
	g2d.fillPolygon(xsResp, ysResp, 3);
	  
	// line
	g2d.drawLine(circleRespX + addToLineX, circleRespY + addToLineY, triangleRespX, triangleRespY);
}
 
Example 19
Project: sumo   File: Convolution.java   View Source Code Vote up 4 votes
public void renderChar(FontMetrics fontMetrics, BufferedImage image, char c, int safetyMargin) {
	double[][] red = new double[image.getWidth()][image.getHeight()];
	double[][] green = new double[image.getWidth()][image.getHeight()];
	double[][] blue = new double[image.getWidth()][image.getHeight()];
	double[][] alpha = new double[image.getWidth()][image.getHeight()];
	
	ColorModel cm = image.getColorModel();
	
	for(int x=kernel.getWidth()/2; x < image.getWidth()-kernel.getWidth()/2; x++) {
		for(int y=kernel.getHeight()/2; y < image.getHeight()-kernel.getHeight()/2; y++) {
			
			for(int j=0 ; j<kernel.getWidth(); j++) {
				for(int k=0; k<kernel.getHeight(); k++) {
					
					int rgb = image.getRGB(
							x+j-kernel.getWidth()/2,
							y+k-kernel.getHeight()/2);
					
					double value = kernel.getValue(j, k);
					
					red[x][y] += cm.getRed(rgb)*value;
					green[x][y] += cm.getGreen(rgb)*value;
					blue[x][y] += cm.getBlue(rgb)*value;
					alpha[x][y] += cm.getAlpha(rgb)*value;
				}
			}
		}
	}
	
	if(normalize) {
		normalize(red, green, blue, alpha);
	}
	
	Graphics2D g = image.createGraphics();
	Clear.clear(g, image.getWidth(), image.getHeight());
	
	
	for(int x=0; x < image.getWidth(); x++) {
		for(int y=0; y < image.getHeight(); y++) {
			if(red[x][y] > 255) red[x][y] = 255;
			if(green[x][y] > 255) green[x][y] = 255;
			if(blue[x][y] > 255) blue[x][y] = 255;
			if(alpha[x][y] > 255) alpha[x][y] = 255;
			
			if(red[x][y] < 0) red[x][y] = 0;
			if(green[x][y] < 0) green[x][y] = 0;
			if(blue[x][y] < 0) blue[x][y] = 0;
			if(alpha[x][y] < 0) alpha[x][y] = 0;
			
			// @todo optimize drawing the image with image.setRGB #
			Color color = new Color((int)red[x][y], (int)green[x][y], (int)blue[x][y], (int)alpha[x][y]);
			g.setColor(color);
			g.drawLine(x, y, x, y);
		}
	}
	
}
 
Example 20
Project: FCat   File: CaptchaUtil.java   View Source Code Vote up 4 votes
public static BufferedImage createWeak(int width, int height, String captchaValue) {
	if (LOG.isDebugEnabled()) {
		LOG.debug("createWeak(int width, int height, String captchaValue) width=" + width + " height=" + height + " captchaValue=" + captchaValue);
	}

	if (captchaValue == null) {
		throw new NullPointerException("captchaValue=" + captchaValue);
	}
	width = widthFix(width);
	height = heightFix(height);

	BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

	Graphics2D graphic = bufferedImage.createGraphics();
	graphic.setColor(Color.WHITE);
	graphic.fillRect(0, 0, width, height);
	// graphic.setColor(Color.BLACK);
	// graphic.drawRect(0, 0, width - 1, height - 1);
	// (int) (height * 0.8)
	Font font = new Font("Arial", Font.PLAIN + Font.ITALIC, 30);
	graphic.setFont(font);

	// FontRenderContext fontRenderContext = graphic.getFontRenderContext();
	// Rectangle2D rectangle2D =
	// font.getStringBounds(String.valueOf(captchaValue.charAt(0)),
	// fontRenderContext);
	// rectangle2D.getWidth();

	int captchaValueLength = captchaValue.length();
	int splitSize = 2;
	int xx = 0;
	int widthChar = (width - (captchaValueLength * splitSize)) / captchaValueLength;
	for (int i = 0; i < captchaValueLength; i++) {
		xx = i * widthChar + splitSize;
		graphic.setColor(new Color(MathUtil.RANDOM.nextInt(110), MathUtil.RANDOM.nextInt(110), MathUtil.RANDOM.nextInt(110)));
		graphic.drawString(String.valueOf(captchaValue.charAt(i)), xx, (int) (height * 0.8) + MathUtil.RANDOM.nextInt(6));
	}

	// 加点
	for (int i = 0; i < (width + height); i++) {
		graphic.setColor(new Color(MathUtil.RANDOM.nextInt(255), MathUtil.RANDOM.nextInt(255), MathUtil.RANDOM.nextInt(255)));
		graphic.drawOval(MathUtil.RANDOM.nextInt(width), MathUtil.RANDOM.nextInt(height), 1, 1);
	}

	// 加线
	for (int i = 0; i < 4; i++) {
		graphic.setColor(new Color(MathUtil.RANDOM.nextInt(255), MathUtil.RANDOM.nextInt(255), MathUtil.RANDOM.nextInt(255)));
		graphic.drawLine(0, MathUtil.RANDOM.nextInt(height), width, MathUtil.RANDOM.nextInt(height));
	}
	return bufferedImage;
}