下面列出了java.awt.BasicStroke#JOIN_BEVEL 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Returns the SWT line join corresponding to the specified AWT line join.
*
* @param awtLineJoin the AWT line join.
*
* @return The SWT line join.
*/
private int toSwtLineJoin(int awtLineJoin) {
if (awtLineJoin == BasicStroke.JOIN_BEVEL) {
return SWT.JOIN_BEVEL;
}
else if (awtLineJoin == BasicStroke.JOIN_MITER) {
return SWT.JOIN_MITER;
}
else if (awtLineJoin == BasicStroke.JOIN_ROUND) {
return SWT.JOIN_ROUND;
}
else {
throw new IllegalArgumentException("AWT LineJoin " + awtLineJoin
+ " not recognised");
}
}
public void paintBorder(Component c, Graphics g, int x, int y,
int width, int height)
{
// g.drawLine(x,y, widthheight); // draw top
// g.drawLine(x,y, x,height-1); // draw left
// g.drawLine(width-1,y, width-1,height-1); // draw right
//** 绘制border的底线
//虚线样式
Stroke oldStroke = ((Graphics2D)g).getStroke();
Stroke sroke = new BasicStroke(1, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL, 0, new float[]{1, 2}, 0);//实线,空白
((Graphics2D)g).setStroke(sroke);
//底边上(浅灰色)
g.setColor(new Color(200,200,200));
g.drawLine(x,height-2, width-1,height-2); // draw bottom1
//底边下(白色):绘制一条白色虚线的目的是与上面的灰线产生较强对比度从而形成立体效果
//,本L&F实现中因与Panel的底色对比度不够强烈而立体感不明显(颜色越深的底色最终效果越明显)
g.setColor(Color.white);
g.drawLine(x,height-1, width-1,height-1);//draw bottom2
((Graphics2D)g).setStroke(oldStroke);
}
private void initDefaultValues() {
setRenderingOptimized(true);
Color systemSelection = Utils.getSystemSelection();
setLineStroke(new BasicStroke(1));
setLinePaint(systemSelection);
setFillPaint(new Color(systemSelection.getRed(),
systemSelection.getGreen(),
systemSelection.getBlue(), 80));
oddPerfStroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[] { 6, 6 }, 6);
evenPerfStroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[] { 6, 6 }, 0);
setLineMode(true, true, true, true);
}
private static void draw(final Image img) {
float[] dashes = {10.0f, 10.0f};
BasicStroke bs = new BasicStroke(0.0f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL, 10.0f, dashes,
0.0f);
Graphics2D g = (Graphics2D) img.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, 200, 40);
Line2D line = new Line2D.Double(20, 20, 180, 20);
g.setColor(Color.BLACK);
g.setStroke(bs);
g.draw(line);
g.dispose();
}
public int getJoinSelection() {
String s = getComboJoin().getSelectedItem().toString();
if (s.equals("Bevel"))
return BasicStroke.JOIN_BEVEL;
else if (s.equals("Miter"))
return BasicStroke.JOIN_MITER;
else
return BasicStroke.JOIN_ROUND;
}
/**
* Creates a new instance.
*
* @param datasetIndex the dataset index.
*/
public Pin(int datasetIndex) {
super(datasetIndex);
this.paint = Color.red;
this.stroke = new BasicStroke(3.0f, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_BEVEL);
}
/**
* 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);
}
}
/**
* 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);
}
}
/**
* Convert a sld line join definition to the java awt value.
*
* @param sldJoin the sld join string.
* @return the awt value.
*/
public static int sld2awtJoin( String sldJoin ) {
if (sldJoin.equals(lineJoinNames[1])) {
return BasicStroke.JOIN_BEVEL;
} else if (sldJoin.equals("") || sldJoin.equals(lineJoinNames[2])) {
return BasicStroke.JOIN_MITER;
} else if (sldJoin.equals(lineJoinNames[3])) {
return BasicStroke.JOIN_ROUND;
} else {
throw new IllegalArgumentException("unsupported line join");
}
}
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)));
}
private static void draw(final Image img) {
float[] dashes = {10.0f, 10.0f};
BasicStroke bs = new BasicStroke(0.0f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL, 10.0f, dashes,
0.0f);
Graphics2D g = (Graphics2D) img.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, 200, 40);
Line2D line = new Line2D.Double(20, 20, 180, 20);
g.setColor(Color.BLACK);
g.setStroke(bs);
g.draw(line);
g.dispose();
}
private void initDefaultValues() {
sMarkPaint = new Color(120, 120, 120);
sOddPerfPaint = new Color(120, 120, 120);
sEvenPerfPaint = Color.WHITE;
hMarkPaint = new Color(80, 80, 80);
hOddPerfPaint = Color.BLACK;
hEvenPerfPaint = Color.WHITE;
markStroke = new BasicStroke(2.8f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
oddPerfStroke = new BasicStroke(1f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_BEVEL, 0, new float[] { 1.0f, 3.0f }, 0);
evenPerfStroke = new BasicStroke(1f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_BEVEL, 0, new float[] { 1.0f, 3.0f }, 2);
selectionExtent = 3;
}
public static void main(String[] args) {
GeneralPath shape = new GeneralPath();
int[] pointTypes = {0, 0, 1, 1, 0, 1, 1, 0};
double[] xpoints = {428, 420, 400, 400, 400, 400, 420, 733};
double[] ypoints = {180, 180, 180, 160, 30, 10, 10, 10};
shape.moveTo(xpoints[0], ypoints[0]);
for (int i = 1; i < pointTypes.length; i++) {
if (pointTypes[i] == 1 && i < pointTypes.length - 1) {
shape.quadTo(xpoints[i], ypoints[i],
xpoints[i + 1], ypoints[i + 1]);
} else {
shape.lineTo(xpoints[i], ypoints[i]);
}
}
BufferedImage image = new
BufferedImage(1000, 1000, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();
Color color = new Color(124, 0, 124, 255);
g2.setColor(color);
Stroke stroke = new BasicStroke(1.0f,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL,
10.0f, new float[] {9, 6}, 0.0f);
g2.setStroke(stroke);
g2.draw(shape);
}
/**
* 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);
}
}
private Stroke createStripedStroke(int thickness)
{
return new BasicStroke(thickness, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{9}, 0);
}
public void paint( Graphics g, JComponent c )
{
boolean vertical = ((JSeparator)c).getOrientation() == SwingConstants.VERTICAL;
Dimension size = c.getSize();
//虚线样式
Stroke oldStroke = ((Graphics2D)g).getStroke();
Stroke sroke = new BasicStroke(1, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL, 0, new float[]{2, 2}, 0);//实线,空白
((Graphics2D)g).setStroke(sroke);//
Color temp = g.getColor();
UIDefaults table = UIManager.getLookAndFeelDefaults();
Color shadow = table.getColor("ToolBar.shadow");
Color highlight = table.getColor("ToolBar.highlight");
// TODO BUG_001:不知何故,垂直分隔条并不能像水平分隔条一样,拥有默认设置的new Dimension(6, 6)
// 而只有new Dimension(1, ...),而当它floating时却能正常表现(只能绘出hilight而不能绘出shadow)
//,有待深入研究,垂直的分隔条则不会有此种情况
if (vertical)
{
int x = (size.width / 2) - 1;
//* 当BUG_001存在时,暂时使用以下代码解决:把本该显示hilight的
//* 线条用shadow颜色绘制,最大可能保证ui的正常展现
// g.setColor(shadow);
// g.drawLine(x, 2, x, size.height - 2);
g.setColor(shadow);//highlight);
g.drawLine(x + 1, 2, x + 1, size.height - 2);
//* 当BUG_001不存在时,应该使用以下代码
// g.setColor(shadow);
// g.drawLine(x, 2, x, size.height - 2);
// g.setColor(highlight);
// g.drawLine(x + 1, 2, x + 1, size.height - 2);
}
else
{
int y = (size.height / 2) - 1;
g.setColor(shadow);
g.drawLine(2, y, size.width - 2, y);
g.setColor(highlight);
g.drawLine(2, y + 1, size.width - 2, y + 1);
}
g.setColor(temp);
//
((Graphics2D)g).setStroke(oldStroke);
}
public Shape createStrokedShape(Shape p) {
if (thinStrokes == null) {
thinStrokes = new BasicStroke[100];
for (int a = 0; a < thinStrokes.length; a++) {
float f = .15f + (2.05f - .15f) * (a) / (thinStrokes.length);
thinStrokes[a] = new BasicStroke(f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL, 10);
}
}
GeneralPath path = new GeneralPath();
Random r = new Random(randomSeed);
float h = thickness * thickness;
int thicknessMax = Math.min(thinStrokes.length,
(int) (thinStrokes.length * h + thinStrokes.length * .2f));
int thicknessMin = (int) (thinStrokes.length * h / 2f);
GeneralPath thisLayer = new GeneralPath(Path2D.WIND_NON_ZERO);
GeneralPathWriter writer = new GeneralPathWriter(thisLayer);
for (int a = 0; a < layers; a++) {
writer.reset();
float k1 = a * width / (layers - 1f);
float k2 = k1 - width / 2;
InsetPathWriter insetWriter;
if (k2 > 0) {
insetWriter = new InsetPathWriter(writer, Math.abs(k2), theta);
} else {
insetWriter = new InsetPathWriter(writer, Math.abs(k2),
(float) (Math.PI + theta));
}
insetWriter.write(p);
MeasuredShape[] measuredLayers = MeasuredShape
.getSubpaths(thisLayer);
float minStreakDistance = (4 + 10 * thickness) / 1f;
float maxStreakDistance = (40 + 10 * thickness) / 1f;
float k3 = Math.abs(k2);
float minGapDistance = (4 + 10 * k3) / 1f;
float maxGapDistance = (40 + 10 * k3) / 1f;
for (int b = 0; b < measuredLayers.length; b++) {
r.setSeed(randomSeed + 1000 * a + 10000 * b);
float x = 0;
if (a != layers / 2) {
float k4 = Math.abs(k2 / width);
x = (maxGapDistance - minGapDistance) * r.nextFloat()
+ k4 * (.3f * r.nextFloat() + .7f) * minGapDistance;
}
boolean first = true;
while (x < measuredLayers[b].getOriginalDistance()) {
float streakDistance = minStreakDistance
+ (maxStreakDistance - minStreakDistance)
* r.nextFloat();
float gapDistance;
if (first) {
first = false;
gapDistance = (.2f + .8f * r.nextFloat())
* minGapDistance
+ (maxGapDistance - minGapDistance)
* r.nextFloat();
} else {
gapDistance = minGapDistance
+ (maxGapDistance - minGapDistance)
* r.nextFloat();
}
if (x + streakDistance > measuredLayers[b]
.getOriginalDistance()) {
float z = 0;
if (a != layers / 2)
z = (maxGapDistance - minGapDistance)
* r.nextFloat();
streakDistance = measuredLayers[b].getOriginalDistance()
- x - z;
}
if (streakDistance > 0) {
GeneralPath p2 = measuredLayers[b].getShape(
x / measuredLayers[b].getClosedDistance(),
streakDistance / measuredLayers[b]
.getClosedDistance());
path.append(
thinStrokes[r
.nextInt(thicknessMax - thicknessMin)
+ thicknessMin].createStrokedShape(p2),
false);
}
x = x + (streakDistance + gapDistance);
}
}
}
return path;
}
/**
* Returns a stroke style string based on the current stroke and
* alpha settings.
*
* @return A stroke style string.
*/
private String strokeStyle() {
double strokeWidth = 1.0f;
String strokeCap = DEFAULT_STROKE_CAP;
String strokeJoin = DEFAULT_STROKE_JOIN;
float miterLimit = DEFAULT_MITER_LIMIT;
float[] dashArray = new float[0];
if (this.stroke instanceof BasicStroke) {
BasicStroke bs = (BasicStroke) this.stroke;
strokeWidth = bs.getLineWidth() > 0.0 ? bs.getLineWidth()
: this.zeroStrokeWidth;
switch (bs.getEndCap()) {
case BasicStroke.CAP_ROUND:
strokeCap = "round";
break;
case BasicStroke.CAP_SQUARE:
strokeCap = "square";
break;
case BasicStroke.CAP_BUTT:
default:
// already set to "butt"
}
switch (bs.getLineJoin()) {
case BasicStroke.JOIN_BEVEL:
strokeJoin = "bevel";
break;
case BasicStroke.JOIN_ROUND:
strokeJoin = "round";
break;
case BasicStroke.JOIN_MITER:
default:
// already set to "miter"
}
miterLimit = bs.getMiterLimit();
dashArray = bs.getDashArray();
}
StringBuilder b = new StringBuilder();
b.append("stroke-width: ").append(strokeWidth).append(";");
b.append("stroke: ").append(svgColorStr()).append(";");
b.append("stroke-opacity: ").append(getColorAlpha() * getAlpha())
.append(";");
if (!strokeCap.equals(DEFAULT_STROKE_CAP)) {
b.append("stroke-linecap: ").append(strokeCap).append(";");
}
if (!strokeJoin.equals(DEFAULT_STROKE_JOIN)) {
b.append("stroke-linejoin: ").append(strokeJoin).append(";");
}
if (Math.abs(DEFAULT_MITER_LIMIT - miterLimit) < 0.001) {
b.append("stroke-miterlimit: ").append(geomDP(miterLimit));
}
if (dashArray != null && dashArray.length != 0) {
b.append("stroke-dasharray: ");
for (int i = 0; i < dashArray.length; i++) {
if (i != 0) b.append(", ");
b.append(dashArray[i]);
}
b.append(";");
}
if (this.checkStrokeControlHint) {
Object hint = getRenderingHint(RenderingHints.KEY_STROKE_CONTROL);
if (RenderingHints.VALUE_STROKE_NORMALIZE.equals(hint)
&& !this.shapeRendering.equals("crispEdges")) {
b.append("shape-rendering:crispEdges;");
}
if (RenderingHints.VALUE_STROKE_PURE.equals(hint)
&& !this.shapeRendering.equals("geometricPrecision")) {
b.append("shape-rendering:geometricPrecision;");
}
}
return b.toString();
}
public static void main(String args[]) throws Exception {
int width = 800, height = 600;
// TYPE_INT_ARGB specifies the image format: 8-bit RGBA packed
// into integer pixels
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int fontLeft = 3;
int fontAddY = 150;
int fontNowY = 80;
int lineAddY = 60;
// Financial
Font font = new Font("", Font.BOLD, 14);
g2.setFont(font);
String message = "Financial";
// 原本純白的底
//g2.setColor(Color.WHITE);
//g2.fillRect(0, 0, width, height);
// 填滿 grid 圖片的底
BufferedImage bg = ImageIO.read( new File("/home/git/bamboobsc/gsbsc-web/WebContent/images/s-map-bg-grid.png") );
for (int y=0; y<height; y+=bg.getHeight()) {
for (int x=0; x<width; x+=bg.getWidth()) {
g2.drawImage(bg, x, y, null);
}
}
g2.setPaint( new Color(64, 64, 64) );
g2.drawString(message, fontLeft, fontNowY);
Stroke dashed = new BasicStroke(2, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{9}, 0);
g2.setStroke(dashed);
g2.drawLine(0, fontNowY+lineAddY, width, fontNowY+lineAddY);
fontNowY = fontNowY + fontAddY;
// Customer
message = "Customer";
g2.drawString(message, fontLeft, fontNowY);
g2.setStroke(dashed);
g2.drawLine(0, fontNowY+lineAddY, width, fontNowY+lineAddY);
fontNowY = fontNowY + fontAddY;
// Internal business processes
message = "Internal business processes";
g2.drawString(message, fontLeft, fontNowY);
g2.setStroke(dashed);
g2.drawLine(0, fontNowY+lineAddY, width, fontNowY+lineAddY);
fontNowY = fontNowY + fontAddY;
// Learning and growth
message = "Learning and growth";
g2.drawString(message, fontLeft, fontNowY);
g2.setStroke(dashed);
g2.drawLine(0, fontNowY+lineAddY, width, fontNowY+lineAddY);
fontNowY = fontNowY + fontAddY;
ImageIO.write(bi, "PNG", new File("/tmp/ex/strategy-map-background-"+System.currentTimeMillis()+".png"));
}