下面列出了java.awt.Graphics2D#drawLine ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 以水平方向绘制分隔线样式.
*
* @param g2 the g2
* @param c the c
* @param w the w
* @param h the h
*/
private void drawHorizonal(Graphics2D g2,JComponent c,int w,int h)
{
//** 绘制border的底线
//虚线样式
Stroke oldStroke = g2.getStroke();
Stroke sroke = new BasicStroke(1, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL, 0, new float[]{2, 2}, 0);//实线,空白
g2.setStroke(sroke);
//底边上(浅灰色)
// g2.setColor(new Color(180,180,180));
g2.setColor(c.getForeground());
g2.drawLine(0,h-2, w-1,h-2); // draw bottom1
//底边下(白色):绘制一条白色虚线的目的是与上面的灰线产生较强对比度从而形成立体效果
//,本L&F实现中因与Panel的底色对比度不够强烈而立体感不明显(颜色越深的底色最终效果越明显)
// g2.setColor(Color.white);
g2.setColor(c.getBackground());
g2.drawLine(0,h-1, w-1,h-1);//draw bottom2
g2.setStroke(oldStroke);
}
/**
*
* @param g
* @param width
* @param height
*/
public void draw(Graphics g, int width, int height, PaintMode notUsed) {
//Disable drawing lines very near camera, because it causes strange effects.
//Value of 1.5 was found with visual experiment, so feel free to modify it.
if (Math.abs(projectedCoords[0][0]) > 1.5 ||
Math.abs(projectedCoords[0][1]) > 1.5 ||
this.hidden) {
return;
}
g.setColor(color);
deviceCoords[0][0] = (int)((projectedCoords[0][0] + 0.5) * width);
deviceCoords[0][1] = (int)((projectedCoords[0][1] + 0.5) * height);
deviceCoords[1][0] = (int)((projectedCoords[1][0] + 0.5) * width);
deviceCoords[1][1] = (int)((projectedCoords[1][1] + 0.5) * height);
Graphics2D g2 = (Graphics2D)g;
g2.setStroke(new BasicStroke(thickness));
g2.drawLine(deviceCoords[0][0], deviceCoords[0][1],
deviceCoords[1][0], deviceCoords[1][1]);
}
@Override
public void paintIcon(Component c, Graphics g1, int x, int y) {
Graphics2D g = (Graphics2D) g1;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
Stroke oldStroke = g.getStroke();
if (hover) {
g.setColor(Color.GRAY);
} else {
g.setColor(Color.LIGHT_GRAY);
}
g.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g.fillOval(x + 2, y + 2, 18, 18);
g.setColor(Color.WHITE);
g.drawLine(x + 6, y + 6, x + 15, y + 15);
g.drawLine(x + 15, y + 6, x + 6, y + 15);
g.setStroke(oldStroke);
}
public ChoiceV2Renderer(ChoiceV2 choice) {
BufferedImage yes =
new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = yes.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.BLUE);
g.drawLine(0, 5, 3, 10);
g.drawLine(1, 5, 4, 10);
g.drawLine(3, 10, 10, 0);
g.drawLine(4, 9, 9, 0);
g.dispose();
BufferedImage blank =
new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB);
yesImage = new ImageIcon(yes);
blankImage = new ImageIcon(blank);
this.choice = choice;
}
/**
* Mostra a área de impressão no diagrama
*
* @param g
* @param wdt largura
* @param ht altura
*/
public void PaintAI(Graphics2D g, int wdt, int ht) {
Paint bkppaint = g.getPaint();
g.setColor(new Color(221, 221, 221));
int w = wdt;
while (w < getWidth()) {
g.drawLine(w, 1, w, getHeight() - 1);
w += wdt;
}
int h = ht;
while (h < getHeight()) {
g.drawLine(1, h, getWidth() - 1, h);
h += ht;
}
g.setPaint(bkppaint);
}
private void paintWaitDurationRule(Graphics2D g) {
g.setColor(WAIT_DURATION_COLOR);
Point point = toGraphArea(waitDuration, vMax);
Stroke defaultStroke = g.getStroke();
g.setStroke(DOTTED);
g.drawLine(point.x, point.y, point.x, point.y + graphArea.height);
g.setStroke(defaultStroke);
Graphics2D gg = (Graphics2D) g.create();
gg.translate(point.x, point.y);
gg.rotate(Math.PI / 4);
if (grabbedRule == Rule.WAIT_DURATION_RULE) {
gg.fill3DRect(-4, -4, 9, 9, true);
} else {
gg.fill3DRect(-3, -3, 7, 7, true);
}
}
public void draw (Graphics2D g, double zeroX, double zeroY, double pixelsPerUnit, int pointsToDraw) {
Point lastP = null;
for (int i=0; i<pointsToDraw; ++i) {
double t = i * 1.0 / pointsToDraw;
Vector v = getPoint(t);
Point p = new Point((int) Math.round((v.coord(0)-zeroX)*pixelsPerUnit),
(int) Math.round((zeroY-v.coord(1))*pixelsPerUnit));
if (null != lastP) g.drawLine(lastP.x, lastP.y, p.x, p.y);
lastP = p;
}
}
void draw(Graphics2D _g2, int _index) {
TrailPoint point = points[_index];
Color theColor = getDrawingPanel3D().projectColor(getRealStyle().getLineColor(), point.getDistance());
_g2.setStroke(getRealStyle().getLineStroke());
_g2.setColor(theColor);
if((_index==0)||!point.connected) {
_g2.drawLine((int) point.pixel[0], (int) point.pixel[1], (int) point.pixel[0], (int) point.pixel[1]);
} else {
TrailPoint pointPrev = points[_index-1];
_g2.drawLine((int) point.pixel[0], (int) point.pixel[1], (int) pointPrev.pixel[0], (int) pointPrev.pixel[1]);
}
}
/** Draw the {@link PlotMarker}s
* @param gc Graphics context
*/
private void drawPlotMarkers(final Graphics2D gc)
{
final int y0 = plot_area.getBounds().y;
final int y1 = y0 + plot_area.getBounds().height;
final Stroke old_stroke = gc.getStroke();
gc.setStroke(AxisPart.TICK_STROKE);
for (PlotMarker<XTYPE> marker : plot_markers)
{
gc.setColor(GraphicsUtils.convert(marker.getColor()));
final int x = x_axis.getScreenCoord(marker.getPosition());
gc.drawLine(x, y0, x, y1);
}
gc.setStroke(old_stroke);
}
private void draw(final Graphics2D g, final int fx, final int fy, final int x, final int y) {
g.setColor(Color.white);
g.drawLine(fx, fy, x, y);
g.fillOval(x -4, y -4, 9, 9);
g.setColor(Color.black);
g.drawOval(x -2, y -2, 5, 5);
}
/**
* Paint the background of the button using the specified colors.
*
* @param g the Graphics2D context to paint with.
* @param c the component.
* @param width the width of the component.
* @param height the height of the component.
* @param colors the color set to use to paint the button.
*/
private void paintBackground(Graphics2D g, JComponent c, int width, int height, ButtonColors colors) {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
g.setColor(colors.top);
g.drawLine(0, 0, width - 2, 0);
g.setColor(colors.left);
g.drawLine(0, 1, 0, height - 3);
g.setColor(colors.edge);
g.drawLine(width - 1, 0, width - 1, height - 2);
g.drawLine(0, height - 2, width - 2, height - 2);
g.setColor(colors.shadow);
g.drawLine(0, height - 1, width - 1, height - 1);
g.setColor(colors.interior);
g.fillRect(1, 1, width - 1, height - 2);
}
/**
* Get hatch style image
*
* @param style Hatch style
* @param size
* @param stripeColor Stripe color
* @param backColor Background color
* @return Hatch style image
*/
public static BufferedImage getHatchImage(HatchStyle style, int size, Color stripeColor, Color backColor) {
BufferedImage bi = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bi.createGraphics();
int alpha = backColor.getAlpha();
if (alpha > 0) {
g2.setColor(backColor);
g2.fillRect(0, 0, size, size);
}
g2.setColor(stripeColor);
switch (style) {
case HORIZONTAL:
g2.drawLine(0, size / 2, size, size / 2);
break;
case VERTICAL:
g2.drawLine(size / 2, 0, size / 2, size);
break;
case FORWARD_DIAGONAL:
g2.drawLine(0, 0, size, size);
break;
case BACKWARD_DIAGONAL:
//g2.drawLine(size, 0, 0, size);
g2.draw(new Line2D.Float(0, size, size, 0));
break;
case CROSS:
g2.drawLine(0, size / 2, size, size / 2);
g2.drawLine(size / 2, 0, size / 2, size);
break;
case DIAGONAL_CROSS:
g2.drawLine(0, 0, size, size);
g2.drawLine(0, size, size, 0);
break;
case DOT:
g2.fill(new Ellipse2D.Float(size / 2, size / 2, 2, 2));
break;
}
return bi;
}
private void drawGrid( Graphics2D g2 ) {
int totalGridWidth = numCharAcross * gridWidth;
int totalGridHeight = numCharDown * gridHeight;
g2.setColor( Color.black );
for ( int i = 0; i < numCharDown + 1; i++ )
g2.drawLine( canvasInset_X, i * gridHeight + canvasInset_Y,
canvasInset_X + totalGridWidth, i * gridHeight + canvasInset_Y );
for ( int i = 0; i < numCharAcross + 1; i++ )
g2.drawLine( i * gridWidth + canvasInset_X, canvasInset_Y,
i * gridWidth + canvasInset_X, canvasInset_Y + totalGridHeight );
}
@Override
public Shape paint(Graphics2D g, ScreenTransform transform) {
if (lines.isEmpty()) {
return null;
}
if (thick) {
g.setStroke(THICK_STROKE);
}
else {
g.setStroke(THIN_STROKE);
}
g.setColor(colour);
Path2D result = new Path2D.Double();
for (Line2D line : lines) {
Point2D start = line.getOrigin();
Point2D end = line.getEndPoint();
int x1 = transform.xToScreen(start.getX());
int y1 = transform.yToScreen(start.getY());
int x2 = transform.xToScreen(end.getX());
int y2 = transform.yToScreen(end.getY());
g.drawLine(x1, y1, x2, y2);
if (arrow) {
DrawingTools.drawArrowHeads(x1, y1, x2, y2, g);
}
result.moveTo(x1, y1);
result.lineTo(x2, y2);
// Logger.debug("Painting line " + name + " (" + line + ") from " + x1 + ", " + y1 + " -> " + x2 + ", " + y2);
}
return g.getStroke().createStrokedShape(result);
}
private void drawLine(Graphics2D g, int ix1, int iy1, int ix2, int iy2) {
if (opts.line != Line.NONE) {
g.setColor(opts.seriesColor);
setStroke(g);
g.drawLine(ix1, iy1, ix2, iy2);
}
}
private void drawGrid( Graphics2D g2 ) {
int totalGridWidth = numCharAcross * gridWidth;
int totalGridHeight = numCharDown * gridHeight;
g2.setColor( Color.black );
for ( int i = 0; i < numCharDown + 1; i++ )
g2.drawLine( canvasInset_X, i * gridHeight + canvasInset_Y,
canvasInset_X + totalGridWidth, i * gridHeight + canvasInset_Y );
for ( int i = 0; i < numCharAcross + 1; i++ )
g2.drawLine( i * gridWidth + canvasInset_X, canvasInset_Y,
i * gridWidth + canvasInset_X, canvasInset_Y + totalGridHeight );
}
void render(View2D c, Graphics2D g) {
if (!c.isVisible())
return;
Font oldFont = g.getFont();
Stroke oldStroke = g.getStroke();
int w = c.getWidth();
int h = c.getHeight();
g.setStroke(stroke);
g.setFont(smallFont);
int k;
for (int i = 1; i < nx; i++) {
k = Math.round(i * w / nx);
g.setColor(c.getContrastColor(k, h - 12));
if (i % 10 == 0) {
g.drawLine(k, h, k, h - 10);
if (dx < 0.001) {
centerString(FORMAT_SCIENTIFIC_NOTATION.format(xmin + i * dx), g, k, h - 12);
} else {
centerString(FORMAT_DECIMAL.format(xmin + i * dx), g, k, h - 12);
}
} else {
g.drawLine(k, h, k, h - 5);
}
}
g.setColor(c.getContrastColor(w - 10, h - 12));
centerString(unit, g, w - 10, h - 12);
for (int i = 1; i < ny; i++) {
k = Math.round((1f - (float) i / (float) ny) * h);
g.setColor(c.getContrastColor(15, k + 3));
if (i % 10 == 0) {
g.drawLine(0, k, 10, k);
if (dy < 0.001) {
centerString(FORMAT_SCIENTIFIC_NOTATION.format(ymin + i * dy), g, 25, k + 3);
} else {
centerString(FORMAT_DECIMAL.format(ymin + i * dy), g, 15, k + 3);
}
} else {
g.drawLine(0, k, 5, k);
}
}
g.setColor(c.getContrastColor(15, 10));
centerString(unit, g, 15, 10);
g.setStroke(oldStroke);
g.setFont(oldFont);
}
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g.create();
int w = getWidth();
int h = getHeight();
g2d.setColor(Color.black);
g2d.fillRect(0, 0, w, h);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
antialiasHint);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
renderHint);
g2d.transform(transform);
g2d.setPaint(paint);
switch (shapeType) {
default:
case RECT:
g2d.fillRect(0, 0, w, h);
break;
case ELLIPSE:
g2d.fillOval(0, 0, w, h);
break;
case MULTIPLE:
g2d.fillRect(0, 0, w/2, h/2);
g2d.fillOval(w/2, 0, w/2, h/2);
g2d.drawOval(0, h/2, w/2, h/2);
g2d.drawLine(0, h/2, w/2, h);
g2d.drawLine(0, h, w/2, h/2);
Polygon p = new Polygon();
p.addPoint(w/2, h);
p.addPoint(w, h);
p.addPoint(3*w/4, h/2);
g2d.fillPolygon(p);
break;
}
switch (paintType) {
default:
case BASIC:
case LINEAR:
g2d.setColor(Color.white);
g2d.fillRect(startX-1, startY-1, 2, 2);
g2d.drawString("1", startX, startY + 12);
g2d.fillRect(endX-1, endY-1, 2, 2);
g2d.drawString("2", endX, endY + 12);
break;
case RADIAL:
g2d.setColor(Color.white);
g2d.fillRect(ctrX-1, ctrY-1, 2, 2);
g2d.drawString("C", ctrX, ctrY + 12);
g2d.fillRect(focusX-1, focusY-1, 2, 2);
g2d.drawString("F", focusX, focusY + 12);
break;
}
g2d.dispose();
}
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g.create();
int w = getWidth();
int h = getHeight();
g2d.setColor(Color.black);
g2d.fillRect(0, 0, w, h);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
antialiasHint);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
renderHint);
g2d.transform(transform);
g2d.setPaint(paint);
switch (shapeType) {
default:
case RECT:
g2d.fillRect(0, 0, w, h);
break;
case ELLIPSE:
g2d.fillOval(0, 0, w, h);
break;
case MULTIPLE:
g2d.fillRect(0, 0, w/2, h/2);
g2d.fillOval(w/2, 0, w/2, h/2);
g2d.drawOval(0, h/2, w/2, h/2);
g2d.drawLine(0, h/2, w/2, h);
g2d.drawLine(0, h, w/2, h/2);
Polygon p = new Polygon();
p.addPoint(w/2, h);
p.addPoint(w, h);
p.addPoint(3*w/4, h/2);
g2d.fillPolygon(p);
break;
}
switch (paintType) {
default:
case BASIC:
case LINEAR:
g2d.setColor(Color.white);
g2d.fillRect(startX-1, startY-1, 2, 2);
g2d.drawString("1", startX, startY + 12);
g2d.fillRect(endX-1, endY-1, 2, 2);
g2d.drawString("2", endX, endY + 12);
break;
case RADIAL:
g2d.setColor(Color.white);
g2d.fillRect(ctrX-1, ctrY-1, 2, 2);
g2d.drawString("C", ctrX, ctrY + 12);
g2d.fillRect(focusX-1, focusY-1, 2, 2);
g2d.drawString("F", focusX, focusY + 12);
break;
}
g2d.dispose();
}
public void drawBoxPlot(Graphics2D g2d, int x, int y, int width, int height, double[] vals, double minValue, double maxValue, boolean drawOutliers) {
double median = JSci.maths.ArrayMath.percentile(vals, 0.50d);
double q1 = JSci.maths.ArrayMath.percentile(vals, 0.25d);
double q3 = JSci.maths.ArrayMath.percentile(vals, 0.75d);
double iqr = q3 - q1;
//Draw median:
int posY = y + height - (int) Math.round((double) height * (median - minValue) / (maxValue - minValue));
g2d.setStroke(new java.awt.BasicStroke(2.0f, java.awt.BasicStroke.CAP_BUTT, java.awt.BasicStroke.JOIN_ROUND));
g2d.drawLine(x, posY, x + width, posY);
//Draw IQR:
int posY1 = y + height - (int) Math.round((double) height * (q3 - minValue) / (maxValue - minValue));
int posY2 = y + height - (int) Math.round((double) height * (q1 - minValue) / (maxValue - minValue));
g2d.setStroke(new java.awt.BasicStroke(1.0f, java.awt.BasicStroke.CAP_BUTT, java.awt.BasicStroke.JOIN_ROUND));
g2d.drawRect(x, posY1, width, posY2 - posY1);
//Draw whiskers:
double whiskerTop = q3 + 1.5d * iqr;
double whiskerBottom = q1 - 1.5d * iqr;
double max = Primitives.max(vals);
double min = Primitives.min(vals);
if (min > whiskerBottom) {
whiskerBottom = min;
}
if (max < whiskerTop) {
whiskerTop = max;
}
posY = y + height - (int) Math.round((double) height * (whiskerTop - minValue) / (maxValue - minValue));
g2d.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 2.0f, new float[]{2.0f}, 0.0f));
g2d.drawLine(x + width / 2, posY, x + width / 2, posY1);
g2d.setStroke(new java.awt.BasicStroke(1.0f, java.awt.BasicStroke.CAP_BUTT, java.awt.BasicStroke.JOIN_ROUND));
g2d.drawLine(x + width / 2 - 5, posY, x + width / 2 + 5, posY);
posY = y + height - (int) Math.round((double) height * (whiskerBottom - minValue) / (maxValue - minValue));
g2d.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 2.0f, new float[]{2.0f}, 0.0f));
g2d.drawLine(x + width / 2, posY2, x + width / 2, posY);
g2d.setStroke(new java.awt.BasicStroke(1.0f, java.awt.BasicStroke.CAP_BUTT, java.awt.BasicStroke.JOIN_ROUND));
g2d.drawLine(x + width / 2 - 5, posY, x + width / 2 + 5, posY);
//Draw outliers:
if (drawOutliers) {
java.awt.AlphaComposite alphaComposite10 = java.awt.AlphaComposite.getInstance(java.awt.AlphaComposite.SRC_OVER, 0.10f);
g2d.setComposite(alphaComposite10);
for (int v = 0; v < vals.length; v++) {
if (vals[v] > whiskerTop || vals[v] < whiskerBottom) {
posY = y + height - (int) Math.round((double) height * (vals[v] - minValue) / (maxValue - minValue));
g2d.drawOval(x + width / 2 - 2, posY - 2, 5, 5);
}
}
}
}