Font Metrics Class
Program 1:
import java.awt.*;
import javax.swing.*;
public class FontTest extends JPanel{
public void paint(Graphics g){
g.setFont(new Font("Lucida Sans Regular",Font.BOLD,25));
g.setColor(Color.blue);
g.drawString("CurioVerse01",70,170);
}
public static void main(String args[]){
JFrame f = new JFrame("Font CLass Program");
f.getContentPane().add(new FontTest());
f.setSize(300,300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import javax.swing.*;
public class FontTest extends JPanel{
public void paint(Graphics g){
g.setFont(new Font("Lucida Sans Regular",Font.BOLD,25));
g.setColor(Color.blue);
g.drawString("CurioVerse01",70,170);
}
public static void main(String args[]){
JFrame f = new JFrame("Font CLass Program");
f.getContentPane().add(new FontTest());
f.setSize(300,300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Output:
Program 2:
import java.awt.*;
import javax.swing.*;
public class FontMetricsTest extends JPanel {
public void paint(Graphics g) {
String msg = "CurioVerse01";
Font f = new Font("Georgia",Font.BOLD|Font.ITALIC, 25);
FontMetrics fm = getFontMetrics(f);
g.setFont(f);
int x =(getSize().width-fm.stringWidth(msg))/2;
System.out.println("x= "+x);
int y = getSize().height/2;
System.out.println("y= "+y);
g.drawString(msg, x, y);
}
public static void main(String args[]){
JFrame test = new JFrame();
test.getContentPane().add(new FontMetricsTest());
test.setTitle("FontMetrics Test");
test.setSize(350, 275);
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setLocationRelativeTo(null);
test.setVisible(true);
}
}
import javax.swing.*;
public class FontMetricsTest extends JPanel {
public void paint(Graphics g) {
String msg = "CurioVerse01";
Font f = new Font("Georgia",Font.BOLD|Font.ITALIC, 25);
FontMetrics fm = getFontMetrics(f);
g.setFont(f);
int x =(getSize().width-fm.stringWidth(msg))/2;
System.out.println("x= "+x);
int y = getSize().height/2;
System.out.println("y= "+y);
g.drawString(msg, x, y);
}
public static void main(String args[]){
JFrame test = new JFrame();
test.getContentPane().add(new FontMetricsTest());
test.setTitle("FontMetrics Test");
test.setSize(350, 275);
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setLocationRelativeTo(null);
test.setVisible(true);
}
}
Output:
Comments
Post a Comment
If there are specific topics you'd like articles on, please don't hesitate to inform me. I'll gladly publish content tailored to your preferences.