use of java.awt.Graphics2D in project hudson-2.x by hudson.
the class DependencyGraph method doGraph.
/**
* Experimental visualization of project dependencies.
*/
public void doGraph(StaplerRequest req, StaplerResponse rsp) throws IOException {
// Require admin permission for now (avoid exposing project names with restricted permissions)
Hudson.getInstance().checkPermission(Hudson.ADMINISTER);
try {
// creates a dummy graphics just so that we can measure font metrics
BufferedImage emptyImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = emptyImage.createGraphics();
graphics.setFont(FONT);
final FontMetrics fontMetrics = graphics.getFontMetrics();
// TODO: timestamp check
Layout<AbstractProject> layout = new Layout<AbstractProject>(new Navigator<AbstractProject>() {
public Collection<AbstractProject> vertices() {
// only include projects that have some dependency
List<AbstractProject> r = new ArrayList<AbstractProject>();
for (AbstractProject p : Hudson.getInstance().getAllItems(AbstractProject.class)) {
if (!getDownstream(p).isEmpty() || !getUpstream(p).isEmpty())
r.add(p);
}
return r;
}
public Collection<AbstractProject> edge(AbstractProject p) {
return getDownstream(p);
}
public Dimension getSize(AbstractProject p) {
int w = fontMetrics.stringWidth(p.getDisplayName()) + MARGIN * 2;
return new Dimension(w, fontMetrics.getHeight() + MARGIN * 2);
}
}, Direction.LEFTRIGHT);
Rectangle area = layout.calcDrawingArea();
// give it a bit of margin
area.grow(4, 4);
BufferedImage image = new BufferedImage(area.width, area.height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
g2.setTransform(AffineTransform.getTranslateInstance(-area.x, -area.y));
g2.setPaint(Color.WHITE);
g2.fill(area);
g2.setFont(FONT);
g2.setPaint(Color.BLACK);
for (AbstractProject p : layout.vertices()) {
final Point sp = center(layout.vertex(p));
for (AbstractProject q : layout.edges(p)) {
Point cur = sp;
for (Point pt : layout.edge(p, q)) {
g2.drawLine(cur.x, cur.y, pt.x, pt.y);
cur = pt;
}
final Point ep = center(layout.vertex(q));
g2.drawLine(cur.x, cur.y, ep.x, ep.y);
}
}
int diff = fontMetrics.getAscent() + fontMetrics.getLeading() / 2;
for (AbstractProject p : layout.vertices()) {
Rectangle r = layout.vertex(p);
g2.setPaint(Color.WHITE);
g2.fillRect(r.x, r.y, r.width, r.height);
g2.setPaint(Color.BLACK);
g2.drawRect(r.x, r.y, r.width, r.height);
g2.drawString(p.getDisplayName(), r.x + MARGIN, r.y + MARGIN + diff);
}
rsp.setContentType("image/png");
ServletOutputStream os = rsp.getOutputStream();
ImageIO.write(image, "PNG", os);
os.close();
} catch (HeadlessException e) {
// not available. send out error message
rsp.sendRedirect2(req.getContextPath() + "/images/headless.png");
}
}
use of java.awt.Graphics2D in project openblocks by mikaelhg.
the class JComponentDragHandler method createHandCursor.
private static Cursor createHandCursor(String location, String cursorName) {
if (GraphicsEnvironment.isHeadless()) {
// return default hand cursor if headless
return Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
}
java.net.URL handURL = JComponentDragHandler.class.getResource(location);
assert handURL != null : "Can not find hand cursor image " + cursorName;
ImageIcon handicon = new ImageIcon(handURL);
Dimension cursize = Toolkit.getDefaultToolkit().getBestCursorSize(handicon.getIconWidth(), handicon.getIconHeight());
BufferedImage buffImg = GraphicsManager.gc.createCompatibleImage(cursize.width, cursize.height, Transparency.TRANSLUCENT);
Graphics2D buffImgG2 = (Graphics2D) buffImg.getGraphics();
Point cpoint = new Point(cursize.width / 2 - handicon.getIconWidth() / 2, cursize.height / 2 - handicon.getIconHeight() / 2);
buffImgG2.drawImage(handicon.getImage(), cpoint.x, cpoint.y, null);
return Toolkit.getDefaultToolkit().createCustomCursor(buffImg, new Point(cpoint.x + 5, cpoint.y), cursorName);
}
use of java.awt.Graphics2D in project openblocks by mikaelhg.
the class BlockShapeUtil method getBevelImage.
/**
* Static method to return bufferedImage of a Beveled outline of a block
*/
public static Image getBevelImage(int width, int height, Area s) {
BevelCacheKey key = new BevelCacheKey(width, height, s);
BufferedImage img;
img = bevelCache.get(key);
if (img != null) {
//System.out.println("Found cached bevel!");
return img;
}
//System.out.println("Not found cached bevel!");
//generic light vector - "chosen to look good"
float[] light = ShapeBevel.getLightVector(-1, -2, 2);
int bevelSize = 3;
//create image
img = GraphicsManager.gc.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
Graphics2D g2 = (Graphics2D) img.getGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(ShapeBevel.getFrontFaceOverlay(light));
g2.fill(s);
ShapeBevel.createShapeBevel(g2, s, 0.1, bevelSize, bevelSize, light);
// Make a copy of the Area to prevent aliasing.
BevelCacheKey key2 = new BevelCacheKey(width, height, new Area(s));
bevelCache.put(key2, img);
return img;
}
use of java.awt.Graphics2D in project openblocks by mikaelhg.
the class CArrowButton method paint.
/**
* repaints this
*/
public void paint(Graphics g) {
//super.paint(g);
int w = this.getWidth();
int h = this.getHeight();
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Shape arrow = this.getShape(this.dir);
if (focus) {
g2.setColor(Color.gray);
g2.drawRoundRect(0, 0, w - 1, h - 1, 2 * m, 2 * m);
}
if (pressed) {
//g2.setPaint(new GradientPaint(0, 0, fade, 0, this.getHeight()/2,arrowColor, true));
g2.setColor(highlight);
g2.fill(arrow);
g2.setColor(Color.yellow);
g2.draw(arrow);
} else {
g2.setColor(arrowColor);
g2.fill(arrow);
g2.setColor(Color.white);
g2.draw(arrow);
}
}
use of java.awt.Graphics2D in project openblocks by mikaelhg.
the class CBorderlessButton method paint.
/**
* re paints this
*/
@Override
public void paint(Graphics g) {
// Set up graphics and buffer
//super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Set up first layer
int buttonHeight = this.getHeight() - (INSET * 2);
int buttonWidth = this.getWidth() - (INSET * 2);
if (this.focus) {
int arc = buttonHeight / 3;
Color topColoring;
Color bottomColoring;
if (this.pressed || this.selected) {
topColoring = this.selectedColor.darker();
bottomColoring = CGraphite.blue;
} else {
topColoring = this.buttonColor;
bottomColoring = this.buttonColor;
}
// Paint the first layer
g2.setPaint(new GradientPaint(0, 0, topColoring, 0, buttonHeight, bottomColoring, false));
g2.fillRoundRect(INSET, INSET, buttonWidth, buttonHeight, arc, arc);
g2.setColor(Color.darkGray);
g2.drawRoundRect(INSET, INSET, buttonWidth, buttonHeight, arc, arc);
// set up paint data fields for second layer
int highlightHeight = buttonHeight / 2 - HIGHLIGHT_INSET;
int highlightWidth = buttonWidth - (HIGHLIGHT_INSET * 2) + 1;
if (this.pressed || this.selected) {
topColoring = Color.white;
bottomColoring = this.selectedColor;
} else {
topColoring = Color.white;
bottomColoring = Color.darkGray;
}
// Paint the second layer
g2.setPaint(new GradientPaint(0, 0, topColoring, 0, buttonHeight, bottomColoring, false));
g2.fillRoundRect(INSET + HIGHLIGHT_INSET, INSET + HIGHLIGHT_INSET + 1, highlightWidth, highlightHeight, arc, arc);
}
// Draw the text (if any)
if (this.getText() != null) {
g2.setColor(Color.white);
Font font = g2.getFont().deriveFont((float) (((float) buttonHeight) * .5));
g2.setFont(font);
FontMetrics metrics = g2.getFontMetrics();
Rectangle2D textBounds = metrics.getStringBounds(this.getText(), g2);
float x = (float) ((this.getWidth() / 2) - (textBounds.getWidth() / 2));
float y = (float) ((this.getHeight() / 2) + (textBounds.getHeight() / 2)) - metrics.getDescent();
g2.drawString(this.getText(), x, y);
}
}
Aggregations