下面列出了java.awt.Graphics2D#setStroke ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Draws a line perpendicular to the range axis.
*
* @param g2 the graphics device.
* @param plot the plot.
* @param axis the value axis.
* @param dataArea the area for plotting data (not yet adjusted for any 3D
* effect).
* @param value the value at which the grid line should be drawn.
* @param paint the paint (<code>null</code> not permitted).
* @param stroke the stroke (<code>null</code> not permitted).
*
* @see #drawRangeGridline
*
* @since 1.0.13
*/
public void drawRangeLine(Graphics2D g2, CategoryPlot plot, ValueAxis axis,
Rectangle2D dataArea, double value, Paint paint, Stroke stroke) {
Range range = axis.getRange();
if (!range.contains(value)) {
return;
}
PlotOrientation orientation = plot.getOrientation();
Line2D line = null;
double v = axis.valueToJava2D(value, dataArea, plot.getRangeAxisEdge());
if (orientation == PlotOrientation.HORIZONTAL) {
line = new Line2D.Double(v, dataArea.getMinY(), v,
dataArea.getMaxY());
}
else if (orientation == PlotOrientation.VERTICAL) {
line = new Line2D.Double(dataArea.getMinX(), v,
dataArea.getMaxX(), v);
}
g2.setPaint(paint);
g2.setStroke(stroke);
g2.draw(line);
}
/**
*
* @param g2d
*/
@Override
public void paint ( Graphics2D g2d ) {
paintInit( g2d );
g2d.setStroke( new BasicStroke( 1.0f ) );
g2d.drawLine( 0, 0, getWidth() - 1, 0 );
String label = "Residuals:";
TextLayout mLayout = //
new TextLayout(
label, g2d.getFont(), g2d.getFontRenderContext() );
Rectangle2D bounds = mLayout.getBounds();
g2d.drawString( label,//
getWidth() - (float) (bounds.getWidth()) - 2f,//
(float) mapY( getRangeY_Display() / 2.0 ) + (float) (bounds.getHeight() / 2f) );
}
@Override
public void drawPolygon(Vec2[] vertices, int vertexCount, Color3f color) {
Color s = cpool.getColor(color.x, color.y, color.z, 1f);
Graphics2D g = getGraphics();
saveState(g);
int[] xInts = xIntsPool.get(vertexCount);
int[] yInts = yIntsPool.get(vertexCount);
for (int i = 0; i < vertexCount; i++) {
getWorldToScreenToOut(vertices[i], temp);
xInts[i] = (int) temp.x;
yInts[i] = (int) temp.y;
}
g.setStroke(stroke);
g.setColor(s);
g.drawPolygon(xInts, yInts, vertexCount);
restoreState(g);
}
private static Paint createTransparentCheckeredPaint(Color color, int checkerSize) {
int s = checkerSize;
BufferedImage bufferedImage = new BufferedImage(2 * s, 2 * s, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bufferedImage.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // Anti-alias!
RenderingHints.VALUE_ANTIALIAS_ON);
Color c1 = DataStructureUtils.setColorAlpha(color, (int) (color.getAlpha() * .8));
Color c2 = DataStructureUtils.setColorAlpha(color, (int) (color.getAlpha() * .2));
g2.setStroke(new BasicStroke(0));
g2.setPaint(c2);
g2.setColor(c2);
g2.fillRect(0, 0, s, s);
g2.fillRect(s, s, s, s);
g2.setPaint(c1);
g2.setColor(c1);
g2.fillRect(0, s, s, s);
g2.fillRect(s, 0, s, s);
// paint with the texturing brush
Rectangle2D rect = new Rectangle2D.Double(0, 0, 2 * s, 2 * s);
return new TexturePaint(bufferedImage, rect);
}
/**
* This includes lines, a couple of simple BasicStrokes, and a DST_OVER
* AlphaComposite.
*
* @throws Exception
*/
@Test
public void testLines() throws Exception {
RenderTest t = new RenderTest() {
@Override
public void paint(Graphics2D g) {
g.setColor(Color.orange);
g.setStroke(new BasicStroke(13f));
g.drawLine(50, 3, 89, 20);
g.setColor(Color.cyan);
g.setComposite(AlphaComposite
.getInstance(AlphaComposite.DST_OVER, .8f));
g.setStroke(new BasicStroke(3f, BasicStroke.CAP_SQUARE,
BasicStroke.JOIN_ROUND));
g.drawLine(123, 187, 40, 14);
}
};
t.test();
}
/**
* Overrides the method in the subclass to draw a shadow in the first pass.
*
* @param g2 the graphics device.
* @param pass the pass.
* @param series the series index (zero-based).
* @param item the item index (zero-based).
* @param shape the shape.
*/
@Override
protected void drawFirstPassShape(Graphics2D g2, int pass, int series,
int item, Shape shape) {
if (isShadowPass(pass)) {
if (getWallPaint() != null) {
g2.setStroke(getItemStroke(series, item));
g2.setPaint(getWallPaint());
g2.translate(getXOffset(), getYOffset());
g2.draw(shape);
g2.translate(-getXOffset(), -getYOffset());
}
}
else {
// now draw the real shape
super.drawFirstPassShape(g2, pass, series, item, shape);
}
}
private void drawTextAndEmbellishments(Label label,
Graphics2D g2d,
float x,
float y) {
label.handleDraw(g2d, x, y);
if (!strikethrough && stdUnderline == null && imUnderline == null) {
return;
}
float x1 = x;
float x2 = x1 + (float)label.getLogicalBounds().getWidth();
CoreMetrics cm = label.getCoreMetrics();
if (strikethrough) {
Stroke savedStroke = g2d.getStroke();
g2d.setStroke(new BasicStroke(cm.strikethroughThickness,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER));
float strikeY = y + cm.strikethroughOffset;
g2d.draw(new Line2D.Float(x1, strikeY, x2, strikeY));
g2d.setStroke(savedStroke);
}
float ulOffset = cm.underlineOffset;
float ulThickness = cm.underlineThickness;
if (stdUnderline != null) {
stdUnderline.drawUnderline(g2d, ulThickness, x1, x2, y + ulOffset);
}
if (imUnderline != null) {
imUnderline.drawUnderline(g2d, ulThickness, x1, x2, y + ulOffset);
}
}
/**
* Draw the radial gridlines - the rings.
*
* @param g2 the drawing surface.
* @param plot the plot.
* @param radialAxis the radial axis.
* @param ticks the ticks.
* @param dataArea the data area.
*/
public void drawRadialGridLines(Graphics2D g2,
PolarPlot plot,
ValueAxis radialAxis,
List ticks,
Rectangle2D dataArea) {
g2.setFont(radialAxis.getTickLabelFont());
g2.setPaint(plot.getRadiusGridlinePaint());
g2.setStroke(plot.getRadiusGridlineStroke());
double axisMin = radialAxis.getLowerBound();
Point center = plot.translateValueThetaRadiusToJava2D(axisMin, axisMin,
dataArea);
Iterator iterator = ticks.iterator();
while (iterator.hasNext()) {
NumberTick tick = (NumberTick) iterator.next();
Point p = plot.translateValueThetaRadiusToJava2D(90.0,
tick.getNumber().doubleValue(), dataArea);
int r = p.x - center.x;
int upperLeftX = center.x - r;
int upperLeftY = center.y - r;
int d = 2 * r;
Ellipse2D ring = new Ellipse2D.Double(upperLeftX, upperLeftY, d, d);
g2.setPaint(plot.getRadiusGridlinePaint());
g2.draw(ring);
}
}
/**
* Draws a line perpendicular to the domain axis.
*
* @param g2 the graphics device.
* @param plot the plot.
* @param axis the value axis.
* @param dataArea the area for plotting data (not yet adjusted for any 3D
* effect).
* @param value the value at which the grid line should be drawn.
* @param paint the paint (<code>null</code> not permitted).
* @param stroke the stroke (<code>null</code> not permitted).
*
* @since 1.0.5
*/
public void drawDomainLine(Graphics2D g2, XYPlot plot, ValueAxis axis,
Rectangle2D dataArea, double value, Paint paint, Stroke stroke) {
Range range = axis.getRange();
if (!range.contains(value)) {
return;
}
PlotOrientation orientation = plot.getOrientation();
Line2D line = null;
double v = axis.valueToJava2D(value, dataArea,
plot.getDomainAxisEdge());
if (orientation.isHorizontal()) {
line = new Line2D.Double(dataArea.getMinX(), v, dataArea.getMaxX(),
v);
} else if (orientation.isVertical()) {
line = new Line2D.Double(v, dataArea.getMinY(), v,
dataArea.getMaxY());
}
g2.setPaint(paint);
g2.setStroke(stroke);
Object saved = g2.getRenderingHint(RenderingHints.KEY_STROKE_CONTROL);
g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
RenderingHints.VALUE_STROKE_NORMALIZE);
g2.draw(line);
g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, saved);
}
/**
* Draws a section label on the left side of the pie chart.
*
* @param g2 the graphics device.
* @param state the state.
* @param record the label record.
*/
protected void drawLeftLabel(Graphics2D g2, PiePlotState state,
PieLabelRecord record) {
double anchorX = state.getLinkArea().getMinX();
double targetX = anchorX - record.getGap();
double targetY = record.getAllocatedY();
if (this.labelLinksVisible) {
double theta = record.getAngle();
double linkX = state.getPieCenterX() + Math.cos(theta)
* state.getPieWRadius() * record.getLinkPercent();
double linkY = state.getPieCenterY() - Math.sin(theta)
* state.getPieHRadius() * record.getLinkPercent();
double elbowX = state.getPieCenterX() + Math.cos(theta)
* state.getLinkArea().getWidth() / 2.0;
double elbowY = state.getPieCenterY() - Math.sin(theta)
* state.getLinkArea().getHeight() / 2.0;
double anchorY = elbowY;
g2.setPaint(this.labelLinkPaint);
g2.setStroke(this.labelLinkStroke);
g2.draw(new Line2D.Double(linkX, linkY, elbowX, elbowY));
g2.draw(new Line2D.Double(anchorX, anchorY, elbowX, elbowY));
g2.draw(new Line2D.Double(anchorX, anchorY, targetX, targetY));
}
TextBox tb = record.getLabel();
tb.draw(g2, (float) targetX, (float) targetY, RectangleAnchor.RIGHT);
}
/**
* Draws a grid line against the range axis.
*
* @param g2 the graphics device.
* @param plot the plot.
* @param axis the value axis.
* @param dataArea the area for plotting data (not yet adjusted for any
* 3D effect).
* @param value the value at which the grid line should be drawn.
*/
@Override
public void drawDomainGridLine(Graphics2D g2, XYPlot plot, ValueAxis axis,
Rectangle2D dataArea, double value) {
Range range = axis.getRange();
if (!range.contains(value)) {
return;
}
PlotOrientation orientation = plot.getOrientation();
double v = axis.valueToJava2D(value, dataArea,
plot.getDomainAxisEdge());
Line2D line = null;
if (orientation == PlotOrientation.HORIZONTAL) {
line = new Line2D.Double(dataArea.getMinX(), v,
dataArea.getMaxX(), v);
}
else if (orientation == PlotOrientation.VERTICAL) {
line = new Line2D.Double(v, dataArea.getMinY(), v,
dataArea.getMaxY());
}
Paint paint = plot.getDomainGridlinePaint();
Stroke stroke = plot.getDomainGridlineStroke();
g2.setPaint(paint != null ? paint : Plot.DEFAULT_OUTLINE_PAINT);
g2.setStroke(stroke != null ? stroke : Plot.DEFAULT_OUTLINE_STROKE);
Object saved = g2.getRenderingHint(RenderingHints.KEY_STROKE_CONTROL);
g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
RenderingHints.VALUE_STROKE_NORMALIZE);
g2.draw(line);
g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, saved);
}
protected void paintLine(Graphics2D g) {
g.setColor(this.getColor());
Stroke old = g.getStroke();
g.setStroke(new BasicStroke(lineWeight));
g.drawLine((int) from.drawx, (int) from.drawy, (int) to.drawx,
(int) to.drawy);
g.setStroke(old);
}
/**
* Draws the annotation.
*
* @param g2 the graphics device.
* @param plot the plot.
* @param dataArea the data area.
* @param domainAxis the domain axis.
* @param rangeAxis the range axis.
*/
@Override
public void draw(Graphics2D g2, CategoryPlot plot, Rectangle2D dataArea,
CategoryAxis domainAxis, ValueAxis rangeAxis) {
CategoryDataset dataset = plot.getDataset();
int catIndex1 = dataset.getColumnIndex(this.category1);
int catIndex2 = dataset.getColumnIndex(this.category2);
int catCount = dataset.getColumnCount();
double lineX1 = 0.0f;
double lineY1 = 0.0f;
double lineX2 = 0.0f;
double lineY2 = 0.0f;
PlotOrientation orientation = plot.getOrientation();
RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
plot.getDomainAxisLocation(), orientation);
RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
plot.getRangeAxisLocation(), orientation);
if (orientation == PlotOrientation.HORIZONTAL) {
lineY1 = domainAxis.getCategoryJava2DCoordinate(
CategoryAnchor.MIDDLE, catIndex1, catCount, dataArea,
domainEdge);
lineX1 = rangeAxis.valueToJava2D(this.value1, dataArea, rangeEdge);
lineY2 = domainAxis.getCategoryJava2DCoordinate(
CategoryAnchor.MIDDLE, catIndex2, catCount, dataArea,
domainEdge);
lineX2 = rangeAxis.valueToJava2D(this.value2, dataArea, rangeEdge);
}
else if (orientation == PlotOrientation.VERTICAL) {
lineX1 = domainAxis.getCategoryJava2DCoordinate(
CategoryAnchor.MIDDLE, catIndex1, catCount, dataArea,
domainEdge);
lineY1 = rangeAxis.valueToJava2D(this.value1, dataArea, rangeEdge);
lineX2 = domainAxis.getCategoryJava2DCoordinate(
CategoryAnchor.MIDDLE, catIndex2, catCount, dataArea,
domainEdge);
lineY2 = rangeAxis.valueToJava2D(this.value2, dataArea, rangeEdge);
}
g2.setPaint(this.paint);
g2.setStroke(this.stroke);
g2.drawLine((int) lineX1, (int) lineY1, (int) lineX2, (int) lineY2);
}
/**
* Draws the visual representation of a single data item.
*
* @param g2 the graphics device.
* @param state the renderer state.
* @param plotArea the area within which the plot is being drawn.
* @param info optional information collection.
* @param plot the plot (can be used to obtain standard color
* information etc).
* @param domainAxis the horizontal axis.
* @param rangeAxis the vertical axis.
* @param dataset the dataset.
* @param series the series index (zero-based).
* @param item the item index (zero-based).
* @param crosshairState crosshair information for the plot
* (<code>null</code> permitted).
* @param pass the pass index.
*/
@Override
public void drawItem(Graphics2D g2, XYItemRendererState state,
Rectangle2D plotArea, PlotRenderingInfo info, XYPlot plot,
ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset,
int series, int item, CrosshairState crosshairState, int pass) {
WindDataset windData = (WindDataset) dataset;
Paint seriesPaint = getItemPaint(series, item);
Stroke seriesStroke = getItemStroke(series, item);
g2.setPaint(seriesPaint);
g2.setStroke(seriesStroke);
// get the data point...
Number x = windData.getX(series, item);
Number windDir = windData.getWindDirection(series, item);
Number wforce = windData.getWindForce(series, item);
double windForce = wforce.doubleValue();
double wdirt = Math.toRadians(windDir.doubleValue() * (-30.0) - 90.0);
double ax1, ax2, ay1, ay2, rax2, ray2;
RectangleEdge domainAxisLocation = plot.getDomainAxisEdge();
RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge();
ax1 = domainAxis.valueToJava2D(x.doubleValue(), plotArea,
domainAxisLocation);
ay1 = rangeAxis.valueToJava2D(0.0, plotArea, rangeAxisLocation);
rax2 = x.doubleValue() + (windForce * Math.cos(wdirt) * 8000000.0);
ray2 = windForce * Math.sin(wdirt);
ax2 = domainAxis.valueToJava2D(rax2, plotArea, domainAxisLocation);
ay2 = rangeAxis.valueToJava2D(ray2, plotArea, rangeAxisLocation);
int diri = windDir.intValue();
int forcei = wforce.intValue();
String dirforce = diri + "-" + forcei;
Line2D line = new Line2D.Double(ax1, ay1, ax2, ay2);
g2.draw(line);
g2.setPaint(Color.blue);
g2.setFont(new Font("Dialog", 1, 9));
g2.drawString(dirforce, (float) ax1, (float) ay1);
g2.setPaint(seriesPaint);
g2.setStroke(seriesStroke);
double alx2, aly2, arx2, ary2;
double ralx2, raly2, rarx2, rary2;
double aldir = Math.toRadians(windDir.doubleValue()
* (-30.0) - 90.0 - 5.0);
ralx2 = wforce.doubleValue() * Math.cos(aldir) * 8000000 * 0.8
+ x.doubleValue();
raly2 = wforce.doubleValue() * Math.sin(aldir) * 0.8;
alx2 = domainAxis.valueToJava2D(ralx2, plotArea, domainAxisLocation);
aly2 = rangeAxis.valueToJava2D(raly2, plotArea, rangeAxisLocation);
line = new Line2D.Double(alx2, aly2, ax2, ay2);
g2.draw(line);
double ardir = Math.toRadians(windDir.doubleValue()
* (-30.0) - 90.0 + 5.0);
rarx2 = wforce.doubleValue() * Math.cos(ardir) * 8000000 * 0.8
+ x.doubleValue();
rary2 = wforce.doubleValue() * Math.sin(ardir) * 0.8;
arx2 = domainAxis.valueToJava2D(rarx2, plotArea, domainAxisLocation);
ary2 = rangeAxis.valueToJava2D(rary2, plotArea, rangeAxisLocation);
line = new Line2D.Double(arx2, ary2, ax2, ay2);
g2.draw(line);
}
private void paint(SynchronousXYItem item, List<ItemSelection> highlighted,
List<ItemSelection> selected, Graphics2D g,
Rectangle dirtyArea, SynchronousXYChartContext context) {
if (highlighted.isEmpty()) return;
if (item.getValuesCount() < 1) return;
if (context.getViewWidth() == 0 || context.getViewHeight() == 0) return;
double itemValueFactor = type == TYPE_RELATIVE ?
getItemValueFactor(context, maxValueOffset,
item.getBounds().height) : 0;
for (ItemSelection selection : highlighted) {
XYItemSelection sel = (XYItemSelection)selection;
int valueIndex = sel.getValueIndex();
if (valueIndex == -1) continue;
int itemX = Utils.checkedInt(context.getViewX(
item.getXValue(valueIndex)));
int itemY = Utils.checkedInt(getYValue(item, valueIndex,
type, context, itemValueFactor));
if (fillColor != null) {
g.setPaint(fillColor);
g.fillOval(itemX - markRadius, itemY - markRadius,
markRadius * 2, markRadius * 2);
}
if (line2Color != null) {
g.setPaint(line2Color);
g.setStroke(line2Stroke);
g.drawOval(itemX - markRadius, itemY - markRadius,
markRadius * 2, markRadius * 2);
}
if (line1Color != null) {
int radius = markRadius + line2Width / 2;
g.setPaint(line1Color);
g.setStroke(line1Stroke);
g.drawOval(itemX - radius, itemY - radius,
radius * 2, radius * 2);
}
}
// System.err.println(">>> paintItem, dirtyArea: " + dirtyArea);
}
/**
* Draws the axis to the supplied graphics target ({@code g2}, with the
* specified starting and ending points for the line. This method is used
* internally, you should not need to call it directly.
*
* @param g2 the graphics target ({@code null} not permitted).
* @param pt0 the starting point ({@code null} not permitted).
* @param pt1 the ending point ({@code null} not permitted).
* @param opposingPt an opposing point (to determine which side of the
* axis line the labels should appear, {@code null} not permitted).
* @param tickData tick details ({@code null} not permitted).
* @param info an object to be populated with rendering info
* ({@code null} permitted).
* @param hinting perform element hinting?
*/
@Override
public void draw(Graphics2D g2, Point2D pt0, Point2D pt1,
Point2D opposingPt, List<TickData> tickData, RenderingInfo info,
boolean hinting) {
if (!isVisible()) {
return;
}
if (pt0.equals(pt1)) {
return;
}
// draw a line for the axis
g2.setStroke(getLineStroke());
g2.setPaint(getLineColor());
Line2D axisLine = new Line2D.Float(pt0, pt1);
g2.draw(axisLine);
// draw the tick marks and labels
g2.setFont(getTickLabelFont());
// we track the max width or height of the labels to know how far to
// offset the axis label when we draw it later
double maxTickLabelDim = 0.0;
if (getTickLabelOrientation().equals(LabelOrientation.PARALLEL)) {
LineMetrics lm = g2.getFontMetrics().getLineMetrics("123", g2);
maxTickLabelDim = lm.getHeight();
}
double tickMarkLength = getTickMarkLength();
double tickLabelOffset = getTickLabelOffset();
g2.setPaint(getTickMarkPaint());
g2.setStroke(getTickMarkStroke());
for (TickData t : tickData) {
if (tickMarkLength > 0.0) {
Line2D tickLine = Utils2D.createPerpendicularLine(axisLine,
t.getAnchorPt(), tickMarkLength, opposingPt);
g2.draw(tickLine);
}
String tickLabel = this.tickLabelFormatter.format(t.getDataValue());
if (getTickLabelOrientation().equals(
LabelOrientation.PERPENDICULAR)) {
maxTickLabelDim = Math.max(maxTickLabelDim,
g2.getFontMetrics().stringWidth(tickLabel));
}
}
if (getTickLabelsVisible()) {
g2.setPaint(getTickLabelColor());
if (getTickLabelOrientation().equals(
LabelOrientation.PERPENDICULAR)) {
drawPerpendicularTickLabels(g2, axisLine, opposingPt, tickData,
info, hinting);
} else {
drawParallelTickLabels(g2, axisLine, opposingPt, tickData,
info, hinting);
}
} else {
maxTickLabelDim = 0.0;
}
// draw the axis label (if any)...
if (getLabel() != null) {
Shape labelBounds = drawAxisLabel(getLabel(), g2, axisLine,
opposingPt, maxTickLabelDim + tickMarkLength
+ tickLabelOffset + getLabelOffset(), info, hinting);
}
}
/**
* Draw a single data item.
*
* @param g2 the graphics device.
* @param state the renderer state.
* @param dataArea the area in which the data is drawn.
* @param plot the plot.
* @param domainAxis the domain axis.
* @param rangeAxis the range axis.
* @param dataset the dataset.
* @param row the row index (zero-based).
* @param column the column index (zero-based).
* @param pass the pass index.
*/
@Override
public void drawItem(Graphics2D g2, CategoryItemRendererState state,
Rectangle2D dataArea, CategoryPlot plot, CategoryAxis domainAxis,
ValueAxis rangeAxis, CategoryDataset dataset, int row,
int column, int pass) {
// do nothing if item is not visible
if (!getItemVisible(row, column)) {
return;
}
Number value = dataset.getValue(row, column);
if (value == null) {
return;
}
PlotOrientation orientation = plot.getOrientation();
// current data point...
double x1s = domainAxis.getCategoryStart(column, getColumnCount(),
dataArea, plot.getDomainAxisEdge());
double x1 = domainAxis.getCategoryMiddle(column, getColumnCount(),
dataArea, plot.getDomainAxisEdge());
double x1e = 2 * x1 - x1s; // or: x1s + 2*(x1-x1s)
double y1 = rangeAxis.valueToJava2D(value.doubleValue(), dataArea,
plot.getRangeAxisEdge());
g2.setPaint(getItemPaint(row, column));
g2.setStroke(getItemStroke(row, column));
if (column != 0) {
Number previousValue = dataset.getValue(row, column - 1);
if (previousValue != null) {
// previous data point...
double previous = previousValue.doubleValue();
double x0s = domainAxis.getCategoryStart(column - 1,
getColumnCount(), dataArea, plot.getDomainAxisEdge());
double x0 = domainAxis.getCategoryMiddle(column - 1,
getColumnCount(), dataArea, plot.getDomainAxisEdge());
double x0e = 2 * x0 - x0s; // or: x0s + 2*(x0-x0s)
double y0 = rangeAxis.valueToJava2D(previous, dataArea,
plot.getRangeAxisEdge());
if (getStagger()) {
int xStagger = row * STAGGER_WIDTH;
if (xStagger > (x1s - x0e)) {
xStagger = (int) (x1s - x0e);
}
x1s = x0e + xStagger;
}
drawLine(g2, (State) state, orientation, x0e, y0, x1s, y0);
// extend x0's flat bar
drawLine(g2, (State) state, orientation, x1s, y0, x1s, y1);
// upright bar
}
}
drawLine(g2, (State) state, orientation, x1s, y1, x1e, y1);
// x1's flat bar
// draw the item labels if there are any...
if (isItemLabelVisible(row, column)) {
drawItemLabel(g2, orientation, dataset, row, column, x1, y1,
(value.doubleValue() < 0.0));
}
// add an item entity, if this information is being collected
EntityCollection entities = state.getEntityCollection();
if (entities != null) {
Rectangle2D hotspot = new Rectangle2D.Double();
if (orientation == PlotOrientation.VERTICAL) {
hotspot.setRect(x1s, y1, x1e - x1s, 4.0);
}
else {
hotspot.setRect(y1 - 2.0, x1s, 4.0, x1e - x1s);
}
addItemEntity(entities, dataset, row, column, hotspot);
}
}
@Override
public void paint( Graphics g ) {
Graphics2D g2 = (Graphics2D)g;
FlatUIUtils.setRenderingHints( g2 );
int width = getWidth();
int height = getHeight();
boolean enabled = isEnabled();
// paint hover or pressed background
if( enabled ) {
Color background = (pressedBackground != null && isPressed())
? deriveBackground( pressedBackground )
: ((hoverBackground != null && isHover())
? deriveBackground( hoverBackground )
: null);
if( background != null ) {
g.setColor( background );
g.fillRect( 0, 0, width, height );
}
}
int direction = getDirection();
boolean vert = (direction == NORTH || direction == SOUTH);
// compute width/height
int w = scale( arrowWidth + (chevron ? 0 : 1) );
int h = scale( (arrowWidth / 2) + (chevron ? 0 : 1) );
// rotate width/height
int rw = vert ? w : h;
int rh = vert ? h : w;
// chevron lines end 1px outside of width/height
if( chevron ) {
// add 1px to width/height for position calculation only
rw++;
rh++;
}
int x = Math.round( (width - rw) / 2f + scale( (float) xOffset ) );
int y = Math.round( (height - rh) / 2f + scale( (float) yOffset ) );
// move arrow for round borders
Container parent = getParent();
if( vert && parent instanceof JComponent && FlatUIUtils.hasRoundBorder( (JComponent) parent ) )
x -= scale( parent.getComponentOrientation().isLeftToRight() ? 1 : -1 );
// paint arrow
g.setColor( enabled
? (isHover() && hoverForeground != null ? hoverForeground : foreground)
: disabledForeground );
g.translate( x, y );
/*debug
debugPaint( g2, vert, rw, rh );
debug*/
Shape arrowShape = createArrowShape( direction, chevron, w, h );
if( chevron ) {
g2.setStroke( new BasicStroke( scale( 1f ) ) );
g2.draw( arrowShape );
} else {
// triangle
g2.fill( arrowShape );
}
g.translate( -x, -y );
}
private void drawLinedGrid(Graphics2D g)
{
int gridMagModel = viewport.gridMagnitudeModel();
double gridSizeModel = Math.pow(10, gridMagModel);
double gridSizeView = viewport.toView(gridSizeModel);
Envelope modelEnv = viewport.getModelEnv();
//System.out.println("gridSizeView= " + gridSizeView);
/**
* Major Grid (10x)
*/
double gridSize10Model = 10 * gridSizeModel;
PrecisionModel pmGrid10 = new PrecisionModel(1.0/gridSize10Model);
double basex10Model = pmGrid10.makePrecise(modelEnv.getMinX());
double basey10Model = pmGrid10.makePrecise(modelEnv.getMinY());
Point2D basePt10View = viewport.toView(new Coordinate(basex10Model, basey10Model));
double gridSize10View = viewport.toView(gridSize10Model);
/**
* Major-Major Grid (100x)
*/
double gridSize100Model = 100 * gridSizeModel;
PrecisionModel pmGrid100 = new PrecisionModel(1.0/gridSize100Model);
double basex100Model = pmGrid100.makePrecise(modelEnv.getMinX());
double basey100Model = pmGrid100.makePrecise(modelEnv.getMinY());
Point2D basePt100View = viewport.toView(new Coordinate(basex100Model, basey100Model));
double gridSize100View = viewport.toView(gridSize100Model);
/**
* Minor Grid
* Only display if dots are sparse enough
*/
if (gridSizeView >= 4) {
PrecisionModel pmGrid = new PrecisionModel(1.0/gridSizeModel);
double basexModel = pmGrid.makePrecise(modelEnv.getMinX());
double baseyModel = pmGrid.makePrecise(modelEnv.getMinY());
Point2D basePtView = viewport.toView(new Coordinate(basexModel, baseyModel));
g.setStroke(new BasicStroke());
g.setColor(ColorUtil.gray(GRID_MINOR_LINE_CLR));
drawGridLines(g, basePtView.getX(), basePtView.getY(), gridSizeView);
}
g.setStroke(new BasicStroke());
g.setColor(ColorUtil.gray(GRID_MAJOR_LINE_CLR));
drawGridLines(g, basePt10View.getX(), basePt10View.getY(), gridSize10View);
/**
* Major-Major Grid (100x)
*/
Stroke strokeMajor2 = new BasicStroke(1, // Width of stroke
BasicStroke.CAP_SQUARE, // End cap style
BasicStroke.JOIN_MITER, // Join style
10, // Miter limit
new float[] {4, 6}, // Dash pattern
0); // Dash phase
g.setStroke(strokeMajor2);
g.setColor(ColorUtil.gray(100));
drawGridLines(g, basePt100View.getX(), basePt100View.getY(), gridSize100View);
/**
* Semi-Major dots (10x + 5)
*/
float dash10Offset = ((int) basePt10View.getY()) % (int) gridSize10View;
dash10Offset = (float) (gridSize10View - dash10Offset + gridSize10View/2);
g.setColor(Color.BLACK);
Stroke strokeMid = new BasicStroke(1, // Width of stroke
BasicStroke.CAP_SQUARE, // End cap style
BasicStroke.JOIN_MITER, // Join style
10, // Miter limit
new float[] {0, (float) gridSize10View/2}, // Dash pattern
dash10Offset); // Dash phase
g.setStroke(strokeMid);
drawGridLines(g,
basePt10View.getX() - gridSize10View/2,
0,
gridSize10View/2);
}
public static BufferedImage getBufferedImage(String imagePath, int width, int height, int cornersWidth, int cornerHeight) {
try {
File imageFile = new File(imagePath);
if (imageFile.exists()) {
BufferedImage image = ImageIO.read(imageFile);
int w = image.getWidth();
int h = image.getHeight();
if (0 != width && 0 < width) {
w = width;
}
if (0 != height && 0 < height) {
h = height;
}
BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = output.createGraphics();
g2.setComposite(AlphaComposite.Src);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// g2.setColor(new Color(0,0,0));
// g2.setBackground(Color);
g2.setStroke(new BasicStroke(1));
g2.fillRoundRect(0, 0, w, h, cornersWidth, cornerHeight);
// g2.setComposite(AlphaComposite.Src);
// g2.fill(new RoundRectangle2D.Float(0, 0, w, h, cornersWidth,
// cornerHeight));
// g2.setComposite(AlphaComposite.SrcAtop);
// g2.setColor(Color.white);//这里设置背景颜色
// g2.fillRect(0, 0, w, h);//这里填充背景颜色
g2.drawImage(image.getScaledInstance(w, h, java.awt.Image.SCALE_SMOOTH), 0, 0, w, h, null);
g2.dispose();
return output;
// int w = image.getWidth();
// int h = image.getHeight();
// if (0 != width && 0 < width) {
// w = width;
// }
// if (0 != height && 0 < height) {
// h = height;
// }
// BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
// Graphics2D g2 = (Graphics2D) output.createGraphics();
// g2.setComposite(AlphaComposite.Src);
// g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// g2.setStroke(new BasicStroke(1));
//
// g2.setComposite(AlphaComposite.SrcAtop);
// g2.drawImage(image.getScaledInstance(w, h, java.awt.Image.SCALE_SMOOTH), 0, 0, null);
// return output;
}
} catch (IOException ex) {
Logger.getLogger(ImageUtil.class.getName()).log(Level.SEVERE, null, ex);
}
return new BufferedImage(20, 20, BufferedImage.TYPE_INT_ARGB);
}