use of java.awt.FontMetrics in project sonarqube by SonarSource.
the class TextLineNumber method getOffsetY.
/*
* Determine the Y offset for the current row
*/
private int getOffsetY(int rowStartOffset, FontMetrics fontMetrics) throws BadLocationException {
// Get the bounding rectangle of the row
Rectangle r = component.modelToView(rowStartOffset);
int lineHeight = fontMetrics.getHeight();
int y = r.y + r.height;
int descent = 0;
if (// default font is being used
r.height == lineHeight) {
descent = fontMetrics.getDescent();
} else // We need to check all the attributes for font changes
{
if (fonts == null)
fonts = new HashMap<>();
Element root = component.getDocument().getDefaultRootElement();
int index = root.getElementIndex(rowStartOffset);
Element line = root.getElement(index);
for (int i = 0; i < line.getElementCount(); i++) {
Element child = line.getElement(i);
AttributeSet as = child.getAttributes();
String fontFamily = (String) as.getAttribute(StyleConstants.FontFamily);
Integer fontSize = (Integer) as.getAttribute(StyleConstants.FontSize);
String key = fontFamily + fontSize;
FontMetrics fm = fonts.get(key);
if (fm == null) {
Font font = new Font(fontFamily, Font.PLAIN, fontSize);
fm = component.getFontMetrics(font);
fonts.put(key, fm);
}
descent = Math.max(descent, fm.getDescent());
}
}
return y - descent;
}
use of java.awt.FontMetrics in project sonarqube by SonarSource.
the class TextLineNumber method paintComponent.
/**
* Draw the line numbers
*/
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Determine the width of the space available to draw the line number
FontMetrics fontMetrics = component.getFontMetrics(component.getFont());
Insets insets = getInsets();
int availableWidth = getSize().width - insets.left - insets.right;
// Determine the rows to draw within the clipped bounds.
Rectangle clip = g.getClipBounds();
int rowStartOffset = component.viewToModel(new Point(0, clip.y));
int endOffset = component.viewToModel(new Point(0, clip.y + clip.height));
while (rowStartOffset <= endOffset) {
try {
if (isCurrentLine(rowStartOffset))
g.setColor(getCurrentLineForeground());
else
g.setColor(getForeground());
// Get the line number as a string and then determine the
// "X" and "Y" offsets for drawing the string.
String lineNumber = getTextLineNumber(rowStartOffset);
int stringWidth = fontMetrics.stringWidth(lineNumber);
int x = getOffsetX(availableWidth, stringWidth) + insets.left;
int y = getOffsetY(rowStartOffset, fontMetrics);
g.drawString(lineNumber, x, y);
// Move to the next row
rowStartOffset = Utilities.getRowEnd(component, rowStartOffset) + 1;
} catch (Exception e) {
break;
}
}
}
use of java.awt.FontMetrics in project OpenNotebook by jaltekruse.
the class Graph method drawErrorMessage.
public void drawErrorMessage(Graphics g, int xSize, int ySize, int xPicOrigin, int yPicOrigin) {
FontMetrics fm = g.getFontMetrics();
int errorWidth = fm.stringWidth("error");
g.setColor(Color.WHITE);
g.fillRect((xPicOrigin + xSize / 2) - errorWidth / 2, (yPicOrigin + ySize / 2) - fm.getHeight() / 2, errorWidth + 4, fm.getHeight() + 4);
g.setColor(Color.BLACK);
g.drawRect((xPicOrigin + xSize / 2) - errorWidth / 2, (yPicOrigin + ySize / 2) - fm.getHeight() / 2, errorWidth + 4, fm.getHeight() + 4);
g.setColor(Color.RED);
g.drawString("error", (xPicOrigin + xSize / 2) - errorWidth / 2 + 2, (yPicOrigin + ySize / 2) + fm.getHeight() / 2);
}
use of java.awt.FontMetrics 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.FontMetrics 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