Reputation: 23017
How can I draw a character or a sequence of characters only partially (e.g. only the upper half)?
The method drawString()
of the java.awt.Graphics
class only allows you to draw the characters entirety, but I want to show only the lower half for example.
Upvotes: 2
Views: 814
Reputation: 43504
Set the Graphics objects clip.
The code below produces this screenshot:
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.add(new JComponent() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
String text = "Hello World!";
g.setFont(g.getFont().deriveFont(40f));
Rectangle2D b = g.getFontMetrics().getStringBounds(text, g);
Rectangle2D bounds = new Rectangle2D.Double(100 + b.getX(),
100 + b.getY(),
b.getWidth(),
b.getHeight() / 2);
g.setClip(bounds);
g.drawString(text, (int) bounds.getX(),
(int) (bounds.getMaxY() + b.getMaxY()));
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
}
Upvotes: 1