import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class StringGraidentPaint extends JFrame {
public StringGraidentPaint() {
getContentPane().add(new DrawingCanvas());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
}
public static void main(String arg[]) {
new StringGraidentPaint();
}
}
class DrawingCanvas extends JPanel {
Font font = new Font("Dialog", Font.BOLD, 40);
FontMetrics fontMetrics;
DrawingCanvas() {
setSize(300, 300);
setBackground(Color.white);
fontMetrics = getFontMetrics(font);
}
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
int w = getSize().width;
int h = getSize().height;
g2D.setFont(font);
GradientPaint gp = new GradientPaint(
30.0f, 50.0f,
Color.blue,
fontMetrics.stringWidth("Hello!"),
fontMetrics.getHeight(),
Color.red);
g2D.setPaint(gp);
g2D.drawString("Hello!", 20,200);
}
}
|