下面列出了java.awt.BasicStroke#CAP_SQUARE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
/**
* 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 void setBondStroke() {
int cap = BasicStroke.CAP_BUTT;
int join = BasicStroke.JOIN_BEVEL;
if (params.bondStrokeCap == BondStrokeCap.ROUND) {
cap = BasicStroke.CAP_ROUND;
} else if (params.bondStrokeCap == BondStrokeCap.SQUARE) {
cap = BasicStroke.CAP_SQUARE;
}
if (params.bondStrokeJoin == BondStrokeJoin.BEVEL) {
join = BasicStroke.JOIN_BEVEL;
} else if (params.bondStrokeJoin == BondStrokeJoin.ROUND) {
join = BasicStroke.JOIN_ROUND;
}
bondStroke = new BasicStroke(params.bondStrokeWidth, cap, join);
}
/**
* Returns the SWT line cap corresponding to the specified AWT line cap.
*
* @param awtLineCap the AWT line cap.
*
* @return The SWT line cap.
*/
private int toSwtLineCap(int awtLineCap) {
if (awtLineCap == BasicStroke.CAP_BUTT) {
return SWT.CAP_FLAT;
}
else if (awtLineCap == BasicStroke.CAP_ROUND) {
return SWT.CAP_ROUND;
}
else if (awtLineCap == BasicStroke.CAP_SQUARE) {
return SWT.CAP_SQUARE;
}
else {
throw new IllegalArgumentException("AWT LineCap " + awtLineCap
+ " not recognised");
}
}
/**
* Returns the AWT line cap corresponding to the specified SWT line cap.
*
* @param swtLineCap the SWT line cap.
*
* @return The AWT line cap.
*/
private int toAwtLineCap(int swtLineCap) {
if (swtLineCap == SWT.CAP_FLAT) {
return BasicStroke.CAP_BUTT;
}
else if (swtLineCap == SWT.CAP_ROUND) {
return BasicStroke.CAP_ROUND;
}
else if (swtLineCap == SWT.CAP_SQUARE) {
return BasicStroke.CAP_SQUARE;
}
else {
throw new IllegalArgumentException("SWT LineCap " + swtLineCap
+ " not recognised");
}
}
/**
* 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;
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
RenderingHints renderHints
= new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
renderHints.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.addRenderingHints(renderHints);
g2d.setPaint(Color.BLACK);
Stroke stroke = new BasicStroke(2.f,
BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER);
g2d.setStroke(stroke);
PintarTextos(g2d);
}
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 cap code from AWT to the corresponding JavaFX StrokeLineCap
* enum value.
*
* @param c the line cap code.
*
* @return A JavaFX line cap value.
*/
private StrokeLineCap awtToJavaFXLineCap(int c) {
if (c == BasicStroke.CAP_BUTT) {
return StrokeLineCap.BUTT;
} else if (c == BasicStroke.CAP_ROUND) {
return StrokeLineCap.ROUND;
} else if (c == BasicStroke.CAP_SQUARE) {
return StrokeLineCap.SQUARE;
} else {
throw new IllegalArgumentException("Unrecognised cap code: " + c);
}
}
public int getCapSelection() {
String s = getComboCap().getSelectedItem().toString();
if (s.equals("Butt"))
return BasicStroke.CAP_BUTT;
else if (s.equals("Round"))
return BasicStroke.CAP_ROUND;
else
return BasicStroke.CAP_SQUARE;
}
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)));
}
/**
* Creates a new Stroke-Object for the given type and with.
*
* @param type
* the stroke-type. (Must be one of the constants defined in this class.)
* @param width
* the stroke's width.
* @return the stroke, never null.
*/
public static Stroke createStroke( final int type, final float width ) {
final Configuration repoConf = ClassicEngineBoot.getInstance().getGlobalConfig();
final boolean useWidthForStrokes =
"true".equals( repoConf.getConfigProperty( "org.pentaho.reporting.engine.classic.core.DynamicStrokeDashes" ) );
final float effectiveWidth;
if ( useWidthForStrokes ) {
effectiveWidth = width;
} else {
effectiveWidth = 1;
}
switch ( type ) {
case STROKE_DASHED:
return new BasicStroke( width, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 10.0f, new float[] {
6 * effectiveWidth, 6 * effectiveWidth }, 0.0f );
case STROKE_DOTTED:
return new BasicStroke( width, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 5.0f, new float[] { 0.0f,
2 * effectiveWidth }, 0.0f );
case STROKE_DOT_DASH:
return new BasicStroke( width, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 10.0f, new float[] { 0,
2 * effectiveWidth, 6 * effectiveWidth, 2 * effectiveWidth }, 0.0f );
case STROKE_DOT_DOT_DASH:
return new BasicStroke( width, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 10.0f, new float[] { 0,
2 * effectiveWidth, 0, 2 * effectiveWidth, 6 * effectiveWidth, 2 * effectiveWidth }, 0.0f );
default:
return new BasicStroke( width );
}
}
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)));
}
GrammarEditorIconImage(ProjectJig pj,int span){
super(span,span,BufferedImage.TYPE_INT_RGB);
//init graphics
Graphics2D g=createGraphics();
g.setRenderingHints(UI.RENDERING_HINTS);
//fill background
g.setColor(UI.ELEMENTMENU_ICONBACKGROUND);
g.fillRect(0,0,span,span);
//glean metrics and transform
DPolygon hostmetagonpoints=getHostMPPoints(pj);
Path2D.Double hostmetagonpath=UI.getClosedPath(hostmetagonpoints);
Rectangle2D bounds=hostmetagonpath.getBounds2D();
double bw=bounds.getWidth(),bh=bounds.getHeight(),scale;
int maxpolygonimagespan=span-(UI.ELEMENTMENU_ICONGEOMETRYINSET*2);
scale=(bw>bh)?maxpolygonimagespan/bw:maxpolygonimagespan/bh;
AffineTransform t=new AffineTransform();
t.scale(scale,-scale);//note y flip
double
xoffset=-bounds.getMinX()+(((span-(bw*scale))/2)/scale),
yoffset=-bounds.getMaxY()-(((span-(bh*scale))/2)/scale);
t.translate(xoffset,yoffset);
g.transform(t);
//render host metagon area
// g.setPaint(UI.ELEMENTMENU_ICON_FILL);
// g.fill(hostmetagonpath);
//render section strokes
BasicStroke stroke=new BasicStroke(
(float)(UI.ELEMENTMENU_ICONPATHSTROKETHICKNESS/scale),
BasicStroke.CAP_SQUARE,
BasicStroke.JOIN_ROUND,
0,null,0);
g.setStroke(stroke);
renderSections(g,pj);}
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)));
}
Stroke createStroke(double thickness){
Stroke s=new BasicStroke((float)(thickness/imagescale),BasicStroke.CAP_SQUARE,BasicStroke.JOIN_ROUND,0,null,0);
return s;}
/**
* Draw the bounding rectangle using transformed coordinates.
*/
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));
}
}
}
/**
*
*/
public static Stroke getStroke(JRPen pen, int lineCap)
{
float lineWidth = pen.getLineWidth();
if (lineWidth > 0f)
{
LineStyleEnum lineStyle = pen.getLineStyleValue();
switch (lineStyle)
{
case DOUBLE :
{
return
new BasicStroke(
lineWidth / 3,
lineCap,
BasicStroke.JOIN_MITER
);
}
case DOTTED :
{
switch (lineCap)
{
case BasicStroke.CAP_SQUARE :
{
return
new BasicStroke(
lineWidth,
lineCap,
BasicStroke.JOIN_MITER,
10f,
new float[]{0, 2 * lineWidth},
0f
);
}
case BasicStroke.CAP_BUTT :
{
return
new BasicStroke(
lineWidth,
lineCap,
BasicStroke.JOIN_MITER,
10f,
new float[]{lineWidth, lineWidth},
0f
);
}
}
}
case DASHED :
{
switch (lineCap)
{
case BasicStroke.CAP_SQUARE :
{
return
new BasicStroke(
lineWidth,
lineCap,
BasicStroke.JOIN_MITER,
10f,
new float[]{4 * lineWidth, 4 * lineWidth},
0f
);
}
case BasicStroke.CAP_BUTT :
{
return
new BasicStroke(
lineWidth,
lineCap,
BasicStroke.JOIN_MITER,
10f,
new float[]{5 * lineWidth, 3 * lineWidth},
0f
);
}
}
}
case SOLID :
default :
{
return
new BasicStroke(
lineWidth,
lineCap,
BasicStroke.JOIN_MITER
);
}
}
}
return null;
}
/**
* 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));
}
}
}
private BasicStroke createStroke(ViewportDef viewportdef,double size){
BasicStroke s=new BasicStroke((float)(size*viewportdef.scale),BasicStroke.CAP_SQUARE,BasicStroke.JOIN_ROUND,0,null,0);
return s;}