下面列出了java.awt.BasicStroke#JOIN_MITER 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void drawRect(SunGraphics2D sg2d,
int x, int y, int w, int h)
{
if (w >= 0 && h >= 0) {
if (sg2d.strokeState < SunGraphics2D.STROKE_CUSTOM) {
BasicStroke bs = ((BasicStroke) sg2d.stroke);
if (w > 0 && h > 0) {
if (bs.getLineJoin() == BasicStroke.JOIN_MITER &&
bs.getDashArray() == null)
{
double lw = bs.getLineWidth();
drawRectangle(sg2d, x, y, w, h, lw);
return;
}
} else {
// Note: This calls the integer version which
// will verify that the local drawLine optimizations
// work and call super.drawLine(), if not.
drawLine(sg2d, x, y, x+w, y+h);
return;
}
}
super.drawRect(sg2d, x, y, w, h);
}
}
private BasicStroke createStroke(float lineThickness) {
if (dashPattern == null) {
return new BasicStroke(lineThickness,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER);
}
else {
return new BasicStroke(lineThickness,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,
10.0f,
dashPattern,
0);
}
}
@Override
public void paint(Graphics g) {
if (g == null || !(g instanceof Graphics2D)) {
return;
}
g.setColor(Color.white);
g.setXORMode(Color.black);
Graphics2D dg = (Graphics2D) g;
Stroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,
10.0f,
new float[]{1.0f, 1.0f},
0.0f);
dg.setStroke(stroke);
try {
dg.draw(new Line2D.Float(10, 10, 20, 20));
} catch (Throwable e) {
synchronized (this) {
theError = e;
}
} finally {
didDraw.countDown();
}
}
@Override
public void paint(Graphics g) {
if (g == null || !(g instanceof Graphics2D)) {
return;
}
g.setColor(Color.white);
g.setXORMode(Color.black);
Graphics2D dg = (Graphics2D) g;
Stroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,
10.0f,
new float[]{1.0f, 1.0f},
0.0f);
dg.setStroke(stroke);
try {
dg.draw(new Line2D.Float(10, 10, 20, 20));
} catch (Throwable e) {
synchronized (this) {
theError = e;
}
} finally {
didDraw.countDown();
}
}
private static void draw(final Image img) {
float[] dashes = {200.0f, 200.0f};
BasicStroke bs = new BasicStroke(20.0f,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,
1.0f,
dashes,
0.0f);
Graphics2D g = (Graphics2D) img.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, 200, 40);
Line2D line = new Line2D.Double(400, 400, 3600, 400);
g.setColor(Color.BLACK);
g.scale(0.05, 0.05);
g.setStroke(bs);
g.draw(line);
g.dispose();
}
private static void draw(final Image img) {
float[] dashes = {200.0f, 200.0f};
BasicStroke bs = new BasicStroke(20.0f,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,
1.0f,
dashes,
0.0f);
Graphics2D g = (Graphics2D) img.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, 200, 40);
Line2D line = new Line2D.Double(400, 400, 3600, 400);
g.setColor(Color.BLACK);
g.scale(0.05, 0.05);
g.setStroke(bs);
g.draw(line);
g.dispose();
}
public void drawRect(SunGraphics2D sg2d,
int x, int y, int w, int h)
{
if (w >= 0 && h >= 0) {
if (sg2d.strokeState < SunGraphics2D.STROKE_CUSTOM) {
BasicStroke bs = ((BasicStroke) sg2d.stroke);
if (w > 0 && h > 0) {
if (bs.getLineJoin() == BasicStroke.JOIN_MITER &&
bs.getDashArray() == null)
{
double lw = bs.getLineWidth();
drawRectangle(sg2d, x, y, w, h, lw);
return;
}
} else {
// Note: This calls the integer version which
// will verify that the local drawLine optimizations
// work and call super.drawLine(), if not.
drawLine(sg2d, x, y, x+w, y+h);
return;
}
}
super.drawRect(sg2d, x, y, w, h);
}
}
/**
* Creates a dashed border of the specified {@code paint}, {@code thickness},
* line shape, relative {@code length}, and relative {@code spacing}.
* If the specified {@code paint} is {@code null},
* the component's foreground color will be used to render the border.
*
* @param paint the {@link Paint} object used to generate a color
* @param thickness the width of a dash line
* @param length the relative length of a dash line
* @param spacing the relative spacing between dash lines
* @param rounded whether or not line ends should be round
* @return the {@code Border} object
*
* @throws IllegalArgumentException if the specified {@code thickness} is less than {@code 1}, or
* if the specified {@code length} is less than {@code 1}, or
* if the specified {@code spacing} is less than {@code 0}
* @since 1.7
*/
public static Border createDashedBorder(Paint paint, float thickness, float length, float spacing, boolean rounded) {
boolean shared = !rounded && (paint == null) && (thickness == 1.0f) && (length == 1.0f) && (spacing == 1.0f);
if (shared && (sharedDashedBorder != null)) {
return sharedDashedBorder;
}
if (thickness < 1.0f) {
throw new IllegalArgumentException("thickness is less than 1");
}
if (length < 1.0f) {
throw new IllegalArgumentException("length is less than 1");
}
if (spacing < 0.0f) {
throw new IllegalArgumentException("spacing is less than 0");
}
int cap = rounded ? BasicStroke.CAP_ROUND : BasicStroke.CAP_SQUARE;
int join = rounded ? BasicStroke.JOIN_ROUND : BasicStroke.JOIN_MITER;
float[] array = { thickness * (length - 1.0f), thickness * (spacing + 1.0f) };
Border border = createStrokeBorder(new BasicStroke(thickness, cap, join, thickness * 2.0f, array, 0.0f), paint);
if (shared) {
sharedDashedBorder = border;
}
return border;
}
private BasicStroke createStroke(float lineThickness) {
if (dashPattern == null) {
return new BasicStroke(lineThickness,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER);
}
else {
return new BasicStroke(lineThickness,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,
10.0f,
dashPattern,
0);
}
}
IMGrayUnderline() {
stroke = new BasicStroke(DEFAULT_THICKNESS,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,
10.0f,
new float[] {1, 1},
0);
}
IMGrayUnderline() {
stroke = new BasicStroke(DEFAULT_THICKNESS,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,
10.0f,
new float[] {1, 1},
0);
}
public void draw(SunGraphics2D sg2d, Shape s) {
if (sg2d.strokeState < SunGraphics2D.STROKE_CUSTOM) {
BasicStroke bs = ((BasicStroke) sg2d.stroke);
if (s instanceof Rectangle2D) {
if (bs.getLineJoin() == BasicStroke.JOIN_MITER &&
bs.getDashArray() == null)
{
Rectangle2D r2d = (Rectangle2D) s;
double w = r2d.getWidth();
double h = r2d.getHeight();
double x = r2d.getX();
double y = r2d.getY();
if (w >= 0 && h >= 0) {
double lw = bs.getLineWidth();
drawRectangle(sg2d, x, y, w, h, lw);
}
return;
}
} else if (s instanceof Line2D) {
Line2D l2d = (Line2D) s;
if (drawGeneralLine(sg2d,
l2d.getX1(), l2d.getY1(),
l2d.getX2(), l2d.getY2()))
{
return;
}
}
}
outpipe.draw(sg2d, s);
}
protected boolean selectStylePen(int cap, int join, float width,
Color color) {
long endCap;
long lineJoin;
float[] rgb = color.getRGBColorComponents(null);
switch(cap) {
case BasicStroke.CAP_BUTT: endCap = PS_ENDCAP_FLAT; break;
case BasicStroke.CAP_ROUND: endCap = PS_ENDCAP_ROUND; break;
default:
case BasicStroke.CAP_SQUARE: endCap = PS_ENDCAP_SQUARE; break;
}
switch(join) {
case BasicStroke.JOIN_BEVEL:lineJoin = PS_JOIN_BEVEL; break;
default:
case BasicStroke.JOIN_MITER:lineJoin = PS_JOIN_MITER; break;
case BasicStroke.JOIN_ROUND:lineJoin = PS_JOIN_ROUND; break;
}
return (selectStylePen(getPrintDC(), endCap, lineJoin, width,
(int) (rgb[0] * MAX_WCOLOR),
(int) (rgb[1] * MAX_WCOLOR),
(int) (rgb[2] * MAX_WCOLOR)));
}
protected boolean selectStylePen(int cap, int join, float width,
Color color) {
long endCap;
long lineJoin;
float[] rgb = color.getRGBColorComponents(null);
switch(cap) {
case BasicStroke.CAP_BUTT: endCap = PS_ENDCAP_FLAT; break;
case BasicStroke.CAP_ROUND: endCap = PS_ENDCAP_ROUND; break;
default:
case BasicStroke.CAP_SQUARE: endCap = PS_ENDCAP_SQUARE; break;
}
switch(join) {
case BasicStroke.JOIN_BEVEL:lineJoin = PS_JOIN_BEVEL; break;
default:
case BasicStroke.JOIN_MITER:lineJoin = PS_JOIN_MITER; break;
case BasicStroke.JOIN_ROUND:lineJoin = PS_JOIN_ROUND; break;
}
return (selectStylePen(getPrintDC(), endCap, lineJoin, width,
(int) (rgb[0] * MAX_WCOLOR),
(int) (rgb[1] * MAX_WCOLOR),
(int) (rgb[2] * MAX_WCOLOR)));
}
protected boolean selectStylePen(int cap, int join, float width,
Color color) {
long endCap;
long lineJoin;
float[] rgb = color.getRGBColorComponents(null);
switch(cap) {
case BasicStroke.CAP_BUTT: endCap = PS_ENDCAP_FLAT; break;
case BasicStroke.CAP_ROUND: endCap = PS_ENDCAP_ROUND; break;
default:
case BasicStroke.CAP_SQUARE: endCap = PS_ENDCAP_SQUARE; break;
}
switch(join) {
case BasicStroke.JOIN_BEVEL:lineJoin = PS_JOIN_BEVEL; break;
default:
case BasicStroke.JOIN_MITER:lineJoin = PS_JOIN_MITER; break;
case BasicStroke.JOIN_ROUND:lineJoin = PS_JOIN_ROUND; break;
}
return (selectStylePen(getPrintDC(), endCap, lineJoin, width,
(int) (rgb[0] * MAX_WCOLOR),
(int) (rgb[1] * MAX_WCOLOR),
(int) (rgb[2] * MAX_WCOLOR)));
}
/**
* Maps a line join code from AWT to the corresponding JavaFX
* StrokeLineJoin enum value.
*
* @param c the line join code.
*
* @return A JavaFX line join value.
*/
private StrokeLineJoin awtToJavaFXLineJoin(int j) {
if (j == BasicStroke.JOIN_BEVEL) {
return StrokeLineJoin.BEVEL;
} else if (j == BasicStroke.JOIN_MITER) {
return StrokeLineJoin.MITER;
} else if (j == BasicStroke.JOIN_ROUND) {
return StrokeLineJoin.ROUND;
} else {
throw new IllegalArgumentException("Unrecognised join code: " + j);
}
}
public void draw(SunGraphics2D sg2d, Shape s) {
if (sg2d.strokeState < SunGraphics2D.STROKE_CUSTOM) {
BasicStroke bs = ((BasicStroke) sg2d.stroke);
if (s instanceof Rectangle2D) {
if (bs.getLineJoin() == BasicStroke.JOIN_MITER &&
bs.getDashArray() == null)
{
Rectangle2D r2d = (Rectangle2D) s;
double w = r2d.getWidth();
double h = r2d.getHeight();
double x = r2d.getX();
double y = r2d.getY();
if (w >= 0 && h >= 0) {
double lw = bs.getLineWidth();
drawRectangle(sg2d, x, y, w, h, lw);
}
return;
}
} else if (s instanceof Line2D) {
Line2D l2d = (Line2D) s;
if (drawGeneralLine(sg2d,
l2d.getX1(), l2d.getY1(),
l2d.getX2(), l2d.getY2()))
{
return;
}
}
}
outpipe.draw(sg2d, s);
}
public static BufferedImage indicateSplit(BufferedImage source,
List<Integer> rows, List<Integer> cols,
Color lineColor, int lineWidth, boolean showSize, double scale) {
try {
if (rows == null || cols == null) {
return source;
}
int width = source.getWidth();
int height = source.getHeight();
int imageType = source.getType();
if (imageType == BufferedImage.TYPE_CUSTOM) {
imageType = BufferedImage.TYPE_INT_ARGB;
}
BufferedImage target = new BufferedImage(width, height, imageType);
Graphics2D g = target.createGraphics();
g.drawImage(source, 0, 0, width, height, null);
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f);
g.setComposite(ac);
g.setColor(lineColor);
BasicStroke stroke = new BasicStroke(lineWidth, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,
1f, new float[]{lineWidth, lineWidth}, 0f);
// BasicStroke stroke = new BasicStroke(lineWidth, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND);
g.setStroke(stroke);
for (int i = 0; i < rows.size(); ++i) {
int row = (int) (rows.get(i) / scale);
if (row <= 0 || row >= height - 1) {
continue;
}
g.drawLine(0, row, width, row);
}
for (int i = 0; i < cols.size(); ++i) {
int col = (int) (cols.get(i) / scale);
if (col <= 0 || col >= width - 1) {
continue;
}
g.drawLine(col, 0, col, height);
}
if (showSize) {
List<String> texts = new ArrayList<>();
List<Integer> xs = new ArrayList<>();
List<Integer> ys = new ArrayList<>();
for (int i = 0; i < rows.size() - 1; ++i) {
int h = rows.get(i + 1) - rows.get(i) + 1;
for (int j = 0; j < cols.size() - 1; ++j) {
int w = cols.get(j + 1) - cols.get(j) + 1;
texts.add(w + "x" + h);
xs.add(cols.get(j) + w / 3);
ys.add(rows.get(i) + h / 3);
// logger.debug(w / 2 + ", " + h / 2 + " " + w + "x" + h);
}
}
int fontSize = width / (cols.size() * 10);
Font font = new Font(Font.MONOSPACED, Font.BOLD, fontSize);
g.setFont(font);
for (int i = 0; i < texts.size(); ++i) {
g.drawString(texts.get(i), xs.get(i), ys.get(i));
}
}
g.dispose();
return target;
} catch (Exception e) {
logger.error(e.toString());
return source;
}
}
/**
* Draw the bounding rectangle using transformed coordinates.
*/
@Override
protected void deviceFrameRect(int x, int y, int width, int height,
Color color) {
AffineTransform deviceTransform = getTransform();
/* check if rotated or sheared */
int transformType = deviceTransform.getType();
boolean usePath = ((transformType &
(AffineTransform.TYPE_GENERAL_ROTATION |
AffineTransform.TYPE_GENERAL_TRANSFORM)) != 0);
if (usePath) {
draw(new Rectangle2D.Float(x, y, width, height));
return;
}
Stroke stroke = getStroke();
if (stroke instanceof BasicStroke) {
BasicStroke lineStroke = (BasicStroke) stroke;
int endCap = lineStroke.getEndCap();
int lineJoin = lineStroke.getLineJoin();
/* check for default style and try to optimize it by
* calling the frameRect native function instead of using paths.
*/
if ((endCap == BasicStroke.CAP_SQUARE) &&
(lineJoin == BasicStroke.JOIN_MITER) &&
(lineStroke.getMiterLimit() ==10.0f)) {
float lineWidth = lineStroke.getLineWidth();
Point2D.Float penSize = new Point2D.Float(lineWidth,
lineWidth);
deviceTransform.deltaTransform(penSize, penSize);
float deviceLineWidth = Math.min(Math.abs(penSize.x),
Math.abs(penSize.y));
/* transform upper left coordinate */
Point2D.Float ul_pos = new Point2D.Float(x, y);
deviceTransform.transform(ul_pos, ul_pos);
/* transform lower right coordinate */
Point2D.Float lr_pos = new Point2D.Float(x + width,
y + height);
deviceTransform.transform(lr_pos, lr_pos);
float w = (float) (lr_pos.getX() - ul_pos.getX());
float h = (float)(lr_pos.getY() - ul_pos.getY());
WPrinterJob wPrinterJob = (WPrinterJob) getPrinterJob();
/* use selectStylePen, if supported */
if (wPrinterJob.selectStylePen(endCap, lineJoin,
deviceLineWidth, color) == true) {
wPrinterJob.frameRect((float)ul_pos.getX(),
(float)ul_pos.getY(), w, h);
}
/* not supported, must be a Win 9x */
else {
double lowerRes = Math.min(wPrinterJob.getXRes(),
wPrinterJob.getYRes());
if ((deviceLineWidth/lowerRes) < MAX_THINLINE_INCHES) {
/* use the default pen styles for thin pens. */
wPrinterJob.selectPen(deviceLineWidth, color);
wPrinterJob.frameRect((float)ul_pos.getX(),
(float)ul_pos.getY(), w, h);
}
else {
draw(new Rectangle2D.Float(x, y, width, height));
}
}
}
else {
draw(new Rectangle2D.Float(x, y, width, height));
}
}
}
/**
* Draw the bounding rectangle using transformed coordinates.
*/
@Override
protected void deviceFrameRect(int x, int y, int width, int height,
Color color) {
AffineTransform deviceTransform = getTransform();
/* check if rotated or sheared */
int transformType = deviceTransform.getType();
boolean usePath = ((transformType &
(AffineTransform.TYPE_GENERAL_ROTATION |
AffineTransform.TYPE_GENERAL_TRANSFORM)) != 0);
if (usePath) {
draw(new Rectangle2D.Float(x, y, width, height));
return;
}
Stroke stroke = getStroke();
if (stroke instanceof BasicStroke) {
BasicStroke lineStroke = (BasicStroke) stroke;
int endCap = lineStroke.getEndCap();
int lineJoin = lineStroke.getLineJoin();
/* check for default style and try to optimize it by
* calling the frameRect native function instead of using paths.
*/
if ((endCap == BasicStroke.CAP_SQUARE) &&
(lineJoin == BasicStroke.JOIN_MITER) &&
(lineStroke.getMiterLimit() ==10.0f)) {
float lineWidth = lineStroke.getLineWidth();
Point2D.Float penSize = new Point2D.Float(lineWidth,
lineWidth);
deviceTransform.deltaTransform(penSize, penSize);
float deviceLineWidth = Math.min(Math.abs(penSize.x),
Math.abs(penSize.y));
/* transform upper left coordinate */
Point2D.Float ul_pos = new Point2D.Float(x, y);
deviceTransform.transform(ul_pos, ul_pos);
/* transform lower right coordinate */
Point2D.Float lr_pos = new Point2D.Float(x + width,
y + height);
deviceTransform.transform(lr_pos, lr_pos);
float w = (float) (lr_pos.getX() - ul_pos.getX());
float h = (float)(lr_pos.getY() - ul_pos.getY());
WPrinterJob wPrinterJob = (WPrinterJob) getPrinterJob();
/* use selectStylePen, if supported */
if (wPrinterJob.selectStylePen(endCap, lineJoin,
deviceLineWidth, color) == true) {
wPrinterJob.frameRect((float)ul_pos.getX(),
(float)ul_pos.getY(), w, h);
}
/* not supported, must be a Win 9x */
else {
double lowerRes = Math.min(wPrinterJob.getXRes(),
wPrinterJob.getYRes());
if ((deviceLineWidth/lowerRes) < MAX_THINLINE_INCHES) {
/* use the default pen styles for thin pens. */
wPrinterJob.selectPen(deviceLineWidth, color);
wPrinterJob.frameRect((float)ul_pos.getX(),
(float)ul_pos.getY(), w, h);
}
else {
draw(new Rectangle2D.Float(x, y, width, height));
}
}
}
else {
draw(new Rectangle2D.Float(x, y, width, height));
}
}
}