import java.awt.font.*; import java.awt.geom.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class FillTextWithLines extends JPanel { Font font; Image image; public static void main(String[] args) { JFrame f = new JFrame("FillTextWithLines"); f.setContentPane(new FillTextWithLines()); f.setSize(500, 300); f.setVisible(true); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public FillTextWithLines() { font = new Font("Times New Roman", Font.BOLD, 100); setBackground(Color.white); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; FontRenderContext frc = g2.getFontRenderContext(); TextLayout layout = new TextLayout("Wiberg", font, frc); Shape shape = layout.getOutline( AffineTransform.getTranslateInstance(30, 150)); //Set the the clipping area to the text. g2.setClip(shape); //Make the letters blue. Rectangle r = shape.getBounds(); g2.setColor(Color.blue); g2.fill(r); //Draw yellow lines in the letters. g2.setColor(Color.yellow); for(int i = (int)r.getY(); i < (int)r.getY() + (int)r.getHeight(); i += 6) //Six pixels between the lines. //The lines are really rectangles three pixels high. g2.fill(new Rectangle2D.Double(r.getX(), i, r.getX() + r.getWidth(), 3)); } }