java.awt.Graphics2D.drawLine()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(12.1k)|赞(0)|评价(0)|浏览(285)

本文整理了Java中java.awt.Graphics2D.drawLine()方法的一些代码示例,展示了Graphics2D.drawLine()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Graphics2D.drawLine()方法的具体详情如下:
包路径:java.awt.Graphics2D
类名称:Graphics2D
方法名:drawLine

Graphics2D.drawLine介绍

暂无

代码示例

代码示例来源:origin: libgdx/libgdx

/** Extract a ninepatch from a texture atlas, according to the android specification.
 * @see <a href="http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch">ninepatch specification</a>
 * @param page The image file related to the page the region is in
 * @param region The region to extract */
private BufferedImage extractNinePatch (BufferedImage page, Region region, File outputDirFile) {
  BufferedImage splitImage = extractImage(page, region, outputDirFile, NINEPATCH_PADDING);
  Graphics2D g2 = splitImage.createGraphics();
  g2.setColor(Color.BLACK);
  // Draw the four lines to save the ninepatch's padding and splits
  int startX = region.splits[0] + NINEPATCH_PADDING;
  int endX = region.width - region.splits[1] + NINEPATCH_PADDING - 1;
  int startY = region.splits[2] + NINEPATCH_PADDING;
  int endY = region.height - region.splits[3] + NINEPATCH_PADDING - 1;
  if (endX >= startX) g2.drawLine(startX, 0, endX, 0);
  if (endY >= startY) g2.drawLine(0, startY, 0, endY);
  if (region.pads != null) {
    int padStartX = region.pads[0] + NINEPATCH_PADDING;
    int padEndX = region.width - region.pads[1] + NINEPATCH_PADDING - 1;
    int padStartY = region.pads[2] + NINEPATCH_PADDING;
    int padEndY = region.height - region.pads[3] + NINEPATCH_PADDING - 1;
    g2.drawLine(padStartX, splitImage.getHeight() - 1, padEndX, splitImage.getHeight() - 1);
    g2.drawLine(splitImage.getWidth() - 1, padStartY, splitImage.getWidth() - 1, padEndY);
  }
  g2.dispose();
  return splitImage;
}

代码示例来源:origin: stackoverflow.com

public void drawDashedLine(Graphics g, int x1, int y1, int x2, int y2){

    //creates a copy of the Graphics instance
    Graphics2D g2d = (Graphics2D) g.create();

    //set the stroke of the copy, not the original 
    Stroke dashed = new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{9}, 0);
    g2d.setStroke(dashed);
    g2d.drawLine(x1, y1, x2, y2);

    //gets rid of the copy
    g2d.dispose();
}

代码示例来源:origin: looly/hutool

/**
 * 绘制干扰线
 * 
 * @param g {@link Graphics2D}画笔
 * @param random 随机对象
 */
private void drawInterfere(Graphics2D g, ThreadLocalRandom random) {
  // 干扰线
  for (int i = 0; i < this.interfereCount; i++) {
    int xs = random.nextInt(width);
    int ys = random.nextInt(height);
    int xe = xs + random.nextInt(width / 8);
    int ye = ys + random.nextInt(height / 8);
    g.setColor(ImageUtil.randomColor(random));
    g.drawLine(xs, ys, xe, ye);
  }
}
// ----------------------------------------------------------------------------------------------------- Private method start

代码示例来源:origin: pentaho/pentaho-kettle

private void drawImage( SwingUniversalImage img, int locationX, int locationY, int imageSize ) {
 if ( isDrawingPixelatedImages() && img.isBitmap() ) {
  BufferedImage bi = new BufferedImage( imageSize, imageSize, BufferedImage.TYPE_INT_ARGB );
  Graphics2D g2 = (Graphics2D) bi.getGraphics();
  g2.setColor( Color.WHITE );
  g2.fillRect( 0, 0, imageSize, imageSize );
  g2.drawImage( img.getAsBitmapForSize( imageSize, imageSize ), 0, 0, observer );
  g2.dispose();
  for ( int x = 0; x < bi.getWidth( observer ); x++ ) {
   for ( int y = 0; y < bi.getHeight( observer ); y++ ) {
    int rgb = bi.getRGB( x, y );
    gc.setColor( new Color( rgb ) );
    gc.setStroke( new BasicStroke( 1.0f ) );
    gc.drawLine( locationX + xOffset + x, locationY + yOffset + y, locationX + xOffset + x, locationY
     + yOffset + y );
   }
  }
 } else {
  gc.setBackground( Color.white );
  gc.clearRect( locationX, locationY, imageSize, imageSize );
  img.drawToGraphics( gc, locationX, locationY, imageSize, imageSize );
 }
}

代码示例来源:origin: plantuml/plantuml

public BufferedImage createBufferedImage() {
  final BufferedImage im = new BufferedImage(widthCell * 15, heightCell * 15, BufferedImage.TYPE_INT_RGB);
  final Graphics2D g2d = im.createGraphics();
  g2d.setColor(Color.WHITE);
  g2d.fillRect(0, 0, im.getWidth(), im.getHeight());
  g2d.setColor(Color.BLACK);
  for (ANode n : board.getNodes()) {
    final int x = board.getCol(n) * widthCell;
    final int y = n.getRow() * heightCell;
    g2d.drawString(n.getCode(), x + 5, y + heightCell / 2 - 5);
    g2d.drawOval(x, y, widthCell / 2, heightCell / 2);
  }
  for (ALink link : board.getLinks()) {
    final ANode n1 = link.getNode1();
    final ANode n2 = link.getNode2();
    final int x1 = 10 + board.getCol(n1) * widthCell;
    final int y1 = 10 + n1.getRow() * heightCell;
    final int x2 = 10 + board.getCol(n2) * widthCell;
    final int y2 = 10 + n2.getRow() * heightCell;
    g2d.drawLine(x1, y1, x2, y2);
  }
  return im;
}

代码示例来源:origin: plantuml/plantuml

private void renderCustomShape(DiagramShape shape, Graphics2D g2){
    CustomShapeDefinition definition = shape.getDefinition();
    
    Rectangle bounds = shape.getBounds();
    
    if(definition.hasBorder()){
      g2.setColor(shape.getStrokeColor());
      if(shape.isStrokeDashed())
        g2.setStroke(dashStroke);
      else
        g2.setStroke(normalStroke);
      g2.drawLine(bounds.x, bounds.y, bounds.x + bounds.width, bounds.y);
      g2.drawLine(bounds.x + bounds.width, bounds.y, bounds.x + bounds.width, bounds.y + bounds.height);
      g2.drawLine(bounds.x, bounds.y + bounds.height, bounds.x + bounds.width, bounds.y + bounds.height);
      g2.drawLine(bounds.x, bounds.y, bounds.x, bounds.y + bounds.height);
      
//            g2.drawRect(bounds.x, bounds.y, bounds.width, bounds.height); //looks different!            
    }
    
    //TODO: custom shape distintion relies on filename extension. Make this more intelligent
    if(definition.getFilename().endsWith(".png")){
      renderCustomPNGShape(shape, g2);
    } else if(definition.getFilename().endsWith(".svg")){
      // renderCustomSVGShape(shape, g2);
      throw new UnsupportedOperationException();
    }
  }

代码示例来源:origin: stackoverflow.com

Graphics2D g=(Graphics2D) image.getGraphics();
//draw over your image
g.drawLine(1,1,100,100);
g.dispose();
//save your image, display it, etc...

代码示例来源:origin: graphhopper/graphhopper

public void plotEdge(Graphics2D g2, double lat, double lon, double lat2, double lon2, float width) {
  g2.setStroke(new BasicStroke(width));
  g2.drawLine((int) getX(lon), (int) getY(lat), (int) getX(lon2), (int) getY(lat2));
}

代码示例来源:origin: kiegroup/optaplanner

public void drawRoute(Graphics2D g, double lon1, double lat1, double lon2, double lat2, boolean straight, boolean dashed) {
  int x1 = translateLongitudeToX(lon1);
  int y1 = translateLatitudeToY(lat1);
  int x2 = translateLongitudeToX(lon2);
  int y2 = translateLatitudeToY(lat2);
  if (dashed) {
    g.setStroke(TangoColorFactory.FAT_DASHED_STROKE);
  }
  if (straight) {
    g.drawLine(x1, y1, x2, y2);
  } else {
    double xDistPart = (x2 - x1) / 3.0;
    double yDistPart = (y2 - y1) / 3.0;
    double ctrlx1 = x1 + xDistPart + yDistPart;
    double ctrly1 = y1 - xDistPart + yDistPart;
    double ctrlx2 = x2 - xDistPart - yDistPart;
    double ctrly2 = y2 + xDistPart - yDistPart;
    g.draw(new CubicCurve2D.Double(x1, y1, ctrlx1, ctrly1, ctrlx2, ctrly2, x2, y2));
  }
  if (dashed) {
    g.setStroke(TangoColorFactory.NORMAL_STROKE);
  }
}

代码示例来源:origin: pentaho/pentaho-kettle

private void drawImage( SwingUniversalImage img, int centerX, int centerY, double angle, int imageSize ) {
 if ( isDrawingPixelatedImages() && img.isBitmap() ) {
  BufferedImage bi =  img.getAsBitmapForSize( imageSize, imageSize, angle );
  int offx = centerX + xOffset - bi.getWidth() / 2;
  int offy = centerY + yOffset - bi.getHeight() / 2;
  for ( int x = 0; x < bi.getWidth( observer ); x++ ) {
   for ( int y = 0; y < bi.getHeight( observer ); y++ ) {
    int rgb = bi.getRGB( x, y );
    gc.setColor( new Color( rgb ) );
    gc.setStroke( new BasicStroke( 1.0f ) );
    gc.drawLine( offx + x, offy + y, offx + x, offy + y );
   }
  }
 } else {
  gc.setBackground( Color.white );
  gc.clearRect( centerX, centerY, imageSize, imageSize );
  img.drawToGraphics( gc, centerX, centerY, imageSize, imageSize, angle );
 }
}

代码示例来源:origin: marytts/marytts

double xp = 0.0;
double yp = 0.0;
g.setColor(currentGraphColor);
for (int i = index_fromX; i < index_toX; i++) {
  if (!Double.isNaN(data[i])) {
      g.drawLine(image_refX + (int) xo, image_refY - (int) yo, image_refX + (int) xp, image_refY - (int) yp);
        histHeight = startY - topY;
      g.setColor(currentGraphColor);
      g.fillRect(image_refX + (int) xp - histogramWidth / 2, topY, histogramWidth, histHeight);
      g.setColor(histogramBorderColor);
      g.drawRect(image_refX + (int) xp - histogramWidth / 2, topY, histogramWidth, histHeight);

代码示例来源:origin: libgdx/libgdx

/** Extract a ninepatch from a texture atlas, according to the android specification.
 * @see <a href="http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch">ninepatch specification</a>
 * @param page The image file related to the page the region is in
 * @param region The region to extract */
private BufferedImage extractNinePatch (BufferedImage page, Region region, File outputDirFile) {
  BufferedImage splitImage = extractImage(page, region, outputDirFile, NINEPATCH_PADDING);
  Graphics2D g2 = splitImage.createGraphics();
  g2.setColor(Color.BLACK);
  // Draw the four lines to save the ninepatch's padding and splits
  int startX = region.splits[0] + NINEPATCH_PADDING;
  int endX = region.width - region.splits[1] + NINEPATCH_PADDING - 1;
  int startY = region.splits[2] + NINEPATCH_PADDING;
  int endY = region.height - region.splits[3] + NINEPATCH_PADDING - 1;
  if (endX >= startX) g2.drawLine(startX, 0, endX, 0);
  if (endY >= startY) g2.drawLine(0, startY, 0, endY);
  if (region.pads != null) {
    int padStartX = region.pads[0] + NINEPATCH_PADDING;
    int padEndX = region.width - region.pads[1] + NINEPATCH_PADDING - 1;
    int padStartY = region.pads[2] + NINEPATCH_PADDING;
    int padEndY = region.height - region.pads[3] + NINEPATCH_PADDING - 1;
    g2.drawLine(padStartX, splitImage.getHeight() - 1, padEndX, splitImage.getHeight() - 1);
    g2.drawLine(splitImage.getWidth() - 1, padStartY, splitImage.getWidth() - 1, padEndY);
  }
  g2.dispose();
  return splitImage;
}

代码示例来源:origin: RaiMan/SikuliX2

g2d.setColor(selFrameColor);
g2d.setStroke(bs);
g2d.draw(rectSelection);
int cx = (x1 + x2) / 2;
int cy = (y1 + y2) / 2;
g2d.setColor(selCrossColor);
g2d.setStroke(_StrokeCross);
g2d.drawLine(cx, y1, cx, y2);
g2d.drawLine(x1, cy, x2, cy);

代码示例来源:origin: looly/hutool

/**
 * 绘制干扰线
 * 
 * @param g {@link Graphics2D}画笔
 * @param random 随机对象
 */
private void drawInterfere(Graphics2D g, ThreadLocalRandom random) {
  // 干扰线
  for (int i = 0; i < this.interfereCount; i++) {
    int xs = random.nextInt(width);
    int ys = random.nextInt(height);
    int xe = xs + random.nextInt(width / 8);
    int ye = ys + random.nextInt(height / 8);
    g.setColor(ImageUtil.randomColor(random));
    g.drawLine(xs, ys, xe, ye);
  }
}
// ----------------------------------------------------------------------------------------------------- Private method start

代码示例来源:origin: stackoverflow.com

public BufferedImage createSkelethonizationImage() {
  BufferedImage image = new BufferedImage(width, height);
  Graphics2D g2 = image.createGraphics();

  // Perform your drawing here
  g2.drawLine(...);

  g2.dispose();
  return image;
}

代码示例来源:origin: graphhopper/graphhopper

public void plotDirectedEdge(Graphics2D g2, double lat, double lon, double lat2, double lon2, float width) {
  g2.setStroke(new BasicStroke(width));
  int startLon = (int) getX(lon);
  int startLat = (int) getY(lat);
  int destLon = (int) getX(lon2);
  int destLat = (int) getY(lat2);
  g2.drawLine(startLon, startLat, destLon, destLat);
  // only for deep zoom show direction
  if (scaleX < 0.0001) {
    g2.setStroke(new BasicStroke(3));
    Path2D.Float path = new Path2D.Float();
    path.moveTo(destLon, destLat);
    path.lineTo(destLon + 6, destLat - 2);
    path.lineTo(destLon + 6, destLat + 2);
    path.lineTo(destLon, destLat);
    AffineTransform at = new AffineTransform();
    double angle = Math.atan2(lat2 - lat, lon2 - lon);
    at.rotate(-angle + Math.PI, destLon, destLat);
    path.transform(at);
    g2.draw(path);
  }
}

代码示例来源:origin: stackoverflow.com

public void drawDashedLine(Graphics g, int x1, int y1, int x2, int y2){
     //creates a copy of the Graphics instance
     Graphics2D g2d = (Graphics2D) g.create();
     Stroke dashed = new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{9}, 0);
     g2d.setStroke(dashed);
     g2d.drawLine(x1, y1, x2, y2);
     //gets rid of the copy
     g2d.dispose();
 }

代码示例来源:origin: i2p/i2p.i2p

void drawLine(int x1, int y1, int x2, int y2, Paint paint, Stroke stroke) {
  gd.setStroke(stroke);
  gd.setPaint(paint);
  gd.drawLine(x1, y1, x2, y2);
}

代码示例来源:origin: libgdx/libgdx

int x = (int)(yAxisWidth + chartWidth * percent);
    if (i != 0 && i != xSplit) {
      g.setColor(Color.lightGray);
      g.drawLine(x, chartY + 1, x, chartY + chartHeight);
    g.drawLine(x, y - 4, x, y - 8);
    if (isExpanded) {
      x -= labelWidth / 2;
      g.drawLine(chartX, y, chartX + chartWidth - 1, y);
    g.drawLine(yAxisWidth - 4, y, yAxisWidth, y);
  g.fillRect(x - 2, y - 2, titleWidth + 4, numberHeight + 4);
g.setStroke(new BasicStroke(isExpanded ? 3 : 2));
int lastX = -1, lastY = -1;
for (Point point : points) {
  Point pixel = pointToPixel(point);
  if (lastX != -1) g.drawLine(lastX, lastY, (int)pixel.x, (int)pixel.y);
  lastX = (int)pixel.x;
  lastY = (int)pixel.y;
g.drawLine(lastX, lastY, chartX + chartWidth - 1, lastY);
for (int i = 0, n = points.size(); i < n; i++) {
  Point point = points.get(i);

代码示例来源:origin: marytts/marytts

double xp = 0.0;
double yp = 0.0;
g.setColor(currentGraphColor);
for (int i = index_fromX; i < index_toX; i++) {
  if (!Double.isNaN(data[i])) {
      g.drawLine(image_refX + (int) xo, image_refY - (int) yo, image_refX + (int) xp, image_refY - (int) yp);
        histHeight = startY - topY;
      g.setColor(currentGraphColor);
      g.fillRect(image_refX + (int) xp - histogramWidth / 2, topY, histogramWidth, histHeight);
      g.setColor(histogramBorderColor);
      g.drawRect(image_refX + (int) xp - histogramWidth / 2, topY, histogramWidth, histHeight);

相关文章

Graphics2D类方法