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 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); } }
public void paintNode(Graphics g, Node n, FontMetrics fm) { String addr=n.addr != null? n.addr.toString() : null; int x=(int)n.x; int y=(int)n.y; g.setColor((n == pick)? selectColor : (n.fixed? fixedColor : nodeColor)); int w=fm.stringWidth(n.lbl) + 10; if(addr != null) w=Math.max(w, fm.stringWidth(addr) + 10); if(addr == null) addr="<no address>"; int h=(fm.getHeight() + 4) * 2; n.width=w; n.height=h; n.xloc=x - w / 2; n.yloc=y - h / 2; g.fillRect(x - w / 2, y - h / 2, w, h); g.setColor(Color.black); g.drawRect(x - w / 2, y - h / 2, w - 1, h - 1); g.drawString(n.lbl, x - (w - 10) / 2, (y - (h - 4) / 2) + fm.getAscent()); g.drawString(addr, x - (w - 10) / 2, (y - (h - 4) / 2) + 2 * fm.getAscent() + 4); }
g.setColor(Color.WHITE); g.setFont(new Font("serif", Font.BOLD, 60)); g.drawString(s, getWidth() / 2 - g.getFontMetrics().stringWidth(s) / 2, getHeight() / 2 + g.getFontMetrics().getHeight() / 2); JFrame f = new JFrame(); f.setSize(500, 500); f.setTitle("Sometimes Red, Sometimes Blue"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setContentPane(new TestPanel()); f.setVisible(true);
public static void main(String args[]) { FrameTestBase t = new FrameTestBase(); t.add(new JComponent() { public void paintComponent(Graphics g) { String str = "hello world!"; int y = 50; FontMetrics fm = g.getFontMetrics(); Rectangle2D rect = fm.getStringBounds(str, g); g.setColor(bgColor); g.fillRect(x, y - fm.getAscent(), (int) rect.getWidth(), (int) rect.getHeight()); g.setColor(textColor); g.drawString(str, x, y); t.setDefaultCloseOperation(EXIT_ON_CLOSE); t.setSize(400, 200); t.setVisible(true);
/** * test image generator */ private static RenderedImage generateTestImage(int width, int height, Random r, double angle) { BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = img.getGraphics(); g.setColor(Color.white); g.fillRect(0, 0, width, height); g.setColor(Color.BLUE); int x = width / 2; int y = height / 2; int radius = Math.min(x, y); g.drawLine(x, y, x + (int) (radius * Math.cos(angle)), y + (int) (radius * Math.sin(angle))); g.drawString("giftest", r.nextInt(width), r.nextInt(height)); return img; }
public void paintComponent(Graphics g) { super.paintComponent(g); int w2 = g.getFontMetrics().stringWidth(TITLE) / 2; g.drawString(TITLE, textPt.x - w2, textPt.y); JFrame f = new JFrame(TITLE); f.add(new MouseDragTest()); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true);
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); } }
JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new TestPane()); String text = "This is a test xyx"; g.setColor(Color.RED); g.drawLine(0, height / 2, getWidth(), height / 2); FontMetrics fm = g.getFontMetrics(); int totalWidth = (fm.stringWidth(text) * 2) + 4; g.setColor(Color.BLACK); g.drawString(text, x, y + ((fm.getDescent() + fm.getAscent()) / 2)); x += fm.stringWidth(text) + 2; y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent(); g.drawString(text, x, y);
void drawNumbers(Graphics g) { Point p; String num; FontMetrics fm=g.getFontMetrics(); int len=0; synchronized(array) { for(int i=0; i < num_fields; i++) for(int j=0; j < num_fields; j++) { num=String.valueOf(array[i][j]); len=fm.stringWidth(num); p=index2Coord(i, j); g.drawString(num, p.x - (len / 2), p.y); } } }
public static void main(String[] args) throws IOException { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setSize(800, 600); BufferedImage modifiedImg = ImageIO.read(new File("c:\\test.png")); Graphics graphics = modifiedImg.getGraphics(); graphics.setColor(Color.red); graphics.drawString("Label", 100, 100);// draw text graphics.drawLine(1, 100, 100, 100);// draw line JLabel label = new JLabel(); label.setIcon(new ImageIcon(modifiedImg)); frame.getContentPane().add(label, BorderLayout.CENTER); frame.setVisible(true); }
public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.YELLOW); g.fillOval(10, 10, grootte, grootte); String str = ""+grootte; FontMetrics fm = g.getFontMetrics(); Rectangle2D strBounds = fm.getStringBounds(str, g); g.setColor(Color.BLACK); g.drawString(str, 10 + grootte/2 - (int)strBounds.getWidth()/2, 10 + grootte/2 + (int)strBounds.getHeight()/2); }
public class Foo extends JPanel { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawString("hello", 0, 20); } public static void main(String[] args) { JFrame frame = new JFrame("Foo"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.add(new Foo()); frame.pack(); frame.setVisible(true); } }
try { if (isCurrentLine(rowStartOffset)) g.setColor(getCurrentLineForeground()); else g.setColor(getForeground()); int stringWidth = fontMetrics.stringWidth(lineNumber); int x = getOffsetX(availableWidth, stringWidth) + insets.left; int y = getOffsetY(rowStartOffset, fontMetrics); g.drawString(lineNumber, x, y);
@Override protected void paintEnabledText(JLabel l, Graphics g, String s, int textX, int textY) { if (l instanceof JShadowedLabel) { JShadowedLabel ll = (JShadowedLabel) l; g.setColor(ll.getShadow()); g.drawString(s, textX + ll.getShadowSize().x, textY + ll.getShadowSize().y); } g.setColor(l.getForeground()); g.drawString(s, textX, textY); }
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); EventQueue.invokeLater(new Runnable() { public void run() { new JavaPaintUI().setVisible(true); super.paintComponent(g); g.drawString("BLAH", 20, 20); g.drawRect(200, 200, 200, 200);
@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); } } }
private void draw (Graphics g, Array<TextureRegion> regions, Color color, boolean drawIndex) { int i=0; for(TextureRegion region : regions){ int x = region.getRegionX(), y = region.getRegionY(), h = region.getRegionHeight(); if(drawIndex){ String indexString = ""+i; Rectangle bounds = g.getFontMetrics().getStringBounds(indexString, g).getBounds(); g.setColor(indexBackgroundColor); g.fillRect(x, y+h-bounds.height, bounds.width, bounds.height); g.setColor(indexColor); g.drawString(indexString, x, y+h); ++i; } g.setColor(color); g.drawRect(x, y, region.getRegionWidth(), h); } } }
g.drawImage(background, 0, 0, this); double theta = 2 * Math.PI * index++ / 64; g.setColor(Color.blue); rect.setRect( (int) (Math.sin(theta) * w / 3 + w / 2 - RADIUS), 2 * RADIUS, 2 * RADIUS); g.fillOval(rect.x, rect.y, rect.width, rect.height); g.setColor(Color.white); if (frameCount == FRAMES) { averageTime = totalTime / FRAMES; g.drawString(s, 5, 16); Dimension d = field.getPreferredSize(); field.setBounds(e.getX(), e.getY(), d.width, d.height); add(field); Graphics2D g = background.createGraphics(); g.clearRect(0, 0, w, h); g.setColor(Color.green.darker()); for (int i = 0; i < 128; i++) { g.drawLine(w / 2, h / 2, r.nextInt(w), r.nextInt(h));