public static void main(String[] args) throws Exception { final BufferedImage image = ImageIO.read(new URL( "http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png")); Graphics g = image.getGraphics(); g.setFont(g.getFont().deriveFont(30f)); g.drawString("Hello World!", 100, 100); g.dispose(); ImageIO.write(image, "png", new File("test.png")); }
/** * Draws the empty board, no pieces on it yet, just grid lines */ void drawEmptyBoard(Graphics g) { int x=x_offset, y=y_offset; Color old_col=g.getColor(); g.setFont(def_font2); old_col=g.getColor(); g.setColor(checksum_col); g.drawString(("Checksum: " + checksum), x_offset + field_size, y_offset - 20); g.setFont(def_font); g.setColor(old_col); for(int i=0; i < num_fields; i++) { for(int j=0; j < num_fields; j++) { // draws 1 row g.drawRect(x, y, field_size, field_size); x+=field_size; } g.drawString((String.valueOf((num_fields - i - 1))), x + 20, y + field_size / 2); y+=field_size; x=x_offset; } for(int i=0; i < num_fields; i++) { g.drawString((String.valueOf(i)), x_offset + i * field_size + field_size / 2, y + 30); } }
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class Testing { public static void main(String arg[]) throws IOException { String key = "Sample"; BufferedImage bufferedImage = new BufferedImage(170, 30, BufferedImage.TYPE_INT_RGB); Graphics graphics = bufferedImage.getGraphics(); graphics.setColor(Color.LIGHT_GRAY); graphics.fillRect(0, 0, 200, 50); graphics.setColor(Color.BLACK); graphics.setFont(new Font("Arial Black", Font.BOLD, 20)); graphics.drawString(key, 10, 25); ImageIO.write(bufferedImage, "jpg", new File( "C:/Users/admin/desktop/image.jpg")); System.out.println("Image Created"); } }
public void drawTopology(Graphics g) { int x=20, y=50; String label; Dimension box=getSize(); Color old=g.getColor(); if(coordinator) { g.setColor(Color.cyan); g.fillRect(11, 31, box.width - 21, box.height - 61); g.setColor(old); } g.drawRect(10, 30, box.width - 20, box.height - 60); g.setFont(myFont); for(int i=0; i < members.size(); i++) { label=members.get(i).toString(); drawNode(g, x, y, label, NormalStyle); y+=50; } }
Graphics2D g2d = (Graphics2D) g.create(); g2d.setColor(Color.RED); g2d.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight()); g2d.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2); Font font = new Font("Arial", Font.BOLD, 48); TextLayout txt = new TextLayout(text, font, context); y += txt.getAscent() - txt.getDescent(); g2d.setFont(font); g2d.setColor(Color.BLACK); g2d.drawString(text, x, y); g2d.setColor(Color.BLUE); g2d.translate(x, y); g2d.draw(bounds);
g.setColor(hazAlfa(fila)); g.fillPolygon(carp); g.setColor(darkShadow.darker()); g.drawPolygon(carp); carp.addPoint(0, lines[i + 1]); carp.addPoint(0, lines[i]); g.setColor(hazAlfa(fila + 2)); g.fillPolygon(carp); g.setColor(darkShadow.darker()); protected void paintText(Graphics g, int tabPlacement, Font font, FontMetrics metrics, int tabIndex, String title, Rectangle textRect, boolean isSelected) { super.paintText(g, tabPlacement, font, metrics, tabIndex, title, textRect, isSelected); g.setFont(font); View v = getTextViewForTab(tabIndex); if (v != null) {
/** * Paint a selected line * @param g Graphics instance. * @param x int. x axis value. * @param y int. y axis value. */ private void paintInSelected(Graphics g, int x, int y) { g.setFont(HexView.BOLD_FONT); char[] content = model.getLineChars(selectedLine); g.drawChars(content, 0, selectedIndexInLine - 0, x, y); g.setColor(HexView.SELECTED_COLOR); x += g.getFontMetrics().charsWidth(content, 0, selectedIndexInLine-0); g.drawChars(content, selectedIndexInLine, 1, x, y); g.setColor(Color.black); x += g.getFontMetrics().charWidth(content[selectedIndexInLine]); g.drawChars(content, selectedIndexInLine+1, (content.length-1)-selectedIndexInLine, x, y); g.setFont(HexView.FONT); }
import java.awt.*; public class TestComponent extends JPanel { private void drawString(Graphics g, String text, int x, int y) { for (String line : text.split("\n")) g.drawString(line, x, y += g.getFontMetrics().getHeight()); } public void paintComponent(Graphics g) { super.paintComponent(g); drawString(g, "hello\nworld", 20, 20); g.setFont(g.getFont().deriveFont(20f)); drawString(g, "part1\npart2", 120, 120); } public static void main(String s[]) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new TestComponent()); f.setSize(220, 220); f.setVisible(true); } }
graphics.setFont(font); FontMetrics fm = graphics.getFontMetrics(font); int width = SwingUtilities.computeStringWidth(fm, header); ((Graphics2D) graphics).drawString(header,
g.setColor(getRandColor(200, 250)); g.fillRect(0, 0, width, height); Font font = new Font("Fixedsys", Font.BOLD, fontHeight); g.setFont(font); int xe = xs + random.nextInt(width); int ye = ys + random.nextInt(height); g.setColor(getRandColor(1, 255)); g.drawLine(xs, ys, xe, ye); for (int i = 0; i < codeCount; i++) { String strRand = str1.substring(i, i + 1); g.setColor(getRandColor(1, 255)); g.drawString(strRand, i * fontWidth + 3, codeY);
public void update(Graphics g) { Dimension d=getSize(); if(off_graphics == null || d.width != off_dimension.width || d.height != off_dimension.height) { off_dimension=d; off_image=createImage(d.width, d.height); off_graphics=off_image.getGraphics(); } //Erase the previous image. off_graphics.setColor(getBackground()); off_graphics.fillRect(0, 0, d.width, d.height); off_graphics.setColor(Color.black); off_graphics.setFont(def_font); drawEmptyBoard(off_graphics); drawNumbers(off_graphics); g.drawImage(off_image, 0, 0, this); }
@Override public void paint(@NotNull Inlay inlay, @NotNull Graphics g, @NotNull Rectangle targetRegion, @NotNull TextAttributes textAttributes) { Editor editor = inlay.getEditor(); FontMetrics fontMetrics = getFontMetrics(editor).metrics; LineMetrics lineMetrics = fontMetrics.getLineMetrics(text, g); g.setColor(JBColor.GRAY); g.setFont(fontMetrics.getFont()); g.drawString(text, 0, targetRegion.y + (int)(lineMetrics.getHeight() - lineMetrics.getDescent())); g.setColor(JBColor.LIGHT_GRAY); g.drawRect(targetRegion.x, targetRegion.y, targetRegion.width, targetRegion.height); }
private void paintInEdit(Graphics g, byte content, int x, int y) { g.setFont(HexView.BOLD_FONT); g.setColor(Color.white); char[] chars = getChars(content); if (selectedChar == 0) { g.setColor(HexView.SELECTED_COLOR); g.drawChars(chars, 0, 1, x, y); g.setColor(Color.black); g.drawChars(chars, 1, 1, x + g.getFontMetrics().charWidth(chars[0]), y); } else { g.setColor(Color.black); g.drawChars(chars, 0, 1, x, y); g.setColor(HexView.SELECTED_COLOR); g.drawChars(chars, 1, 1,x + g.getFontMetrics().charWidth(chars[0]), y); } setDefault(g); }
@Override public void paintComponent(Graphics g) { super.paintComponent(g); // starting position in document int start = textEditor.viewToModel(getViewport().getViewPosition()); // end position in document int end = textEditor.viewToModel(new Point(10, getViewport().getViewPosition().y + (int) textEditor.getVisibleRect().getHeight()) ); // translate offsets to lines Document doc = textEditor.getDocument(); int startline = doc.getDefaultRootElement().getElementIndex(start) + 1; int endline = doc.getDefaultRootElement().getElementIndex(end) + 1; Font f = textEditor.getFont(); int fontHeight = g.getFontMetrics(f).getHeight(); int fontDesc = g.getFontMetrics(f).getDescent(); int startingY = -1; try { startingY = textEditor.modelToView(start).y + fontHeight - fontDesc; } catch (BadLocationException e1) { System.err.println(e1.getMessage()); } g.setFont(f); for (int line = startline, y = startingY; line <= endline; y += fontHeight, line++) { String lineNumber = StringGroovyMethods.padLeft(Integer.toString(line), 4, " "); g.drawString(lineNumber, 0, y); } } }
g.setFont(f); FontMetrics fm = g.getFontMetrics(f); txtY += fm.getMaxAscent();
private void drawProgessBar(Graphics g, int intValue, int totalValue) { if (totalValue == 0) { return; } g.setFont(font); final String label = "" + intValue + "/" + totalValue; final Rectangle2D rect = getUsed(g, label); final int x = 10; final int y = 33; final int barWidth = 170; final int barHeight = (int) (rect.getHeight() + 2); final int grey = 230; g.setColor(new Color(grey, grey, grey)); final int value = barWidth * intValue / totalValue; g.fillRect(x, y, value, barHeight); g.setColor(Color.BLACK); g.drawRect(x, y, barWidth, barHeight); final double xLabel = x + (barWidth - rect.getWidth()) / 2; final double yLabel = y - rect.getY() + (barHeight - rect.getHeight()) / 2; g.drawString(label, (int) xLabel, (int) yLabel); }
g.setColor(new Color(198, 198, 198)); g.fillRect(r.x, r.y, r.width, r.height); g.setColor(new Color(166, 166, 166)); g.drawRect(r.x + 1, r.y + 1, r.width - 2, r.height - 2); g.setColor(new Color(119, 119, 119)); g.drawLine(r.x, r.y, r.x + r.width, r.y); g.drawLine(r.x, r.y, r.x, r.y + r.height); g.setFont(Theme.SMALL_BOLD_FONT); g.setFont(Theme.SMALL_BOLD_FONT); if (isSelected()) { g.setColor(Theme.TEXT_NORMAL_COLOR);
public void paint(Graphics g){ Graphics g2 = (Graphics2D) g; Iterator textIt = textObjects.iterator(); while(textIt.hasNext()){ DiagramText text = (DiagramText) textIt.next(); g2.setFont(text.getFont()); if(text.hasOutline()){ g2.setColor(text.getOutlineColor()); g2.drawString(text.getText(), text.getXPos() + 1, text.getYPos()); g2.drawString(text.getText(), text.getXPos() - 1, text.getYPos()); g2.drawString(text.getText(), text.getXPos(), text.getYPos() + 1); g2.drawString(text.getText(), text.getXPos(), text.getYPos() - 1); } g2.setColor(text.getColor()); g2.drawString(text.getText(), text.getXPos(), text.getYPos()); } } }