/** MyApplet6.java: Fonts and Font Metrics ** Assignment: Modify this file by changing ** the font types, sizes, colors, and text. ** Also, take a look at the MyApplet6.html ** webpage file for displaying this applet. **/ /** USE EXISTING JAVA CLASSES **/ import java.awt.*; import java.applet.Applet; /** EXTEND THE JAVA APPLET CLASS **/ public class MyApplet6 extends Applet { Font font1 = new Font("TimesRoman",Font.PLAIN,18); // Define and initialize Font font2 = new Font("TimesRoman",Font.BOLD,18); // MyApplet6 class variables. Font font3 = new Font("TimesRoman",Font.ITALIC,18); // The different fonts. Font font4 = new Font("TimesRoman",Font.BOLD+Font.ITALIC,18); FontMetrics font1m = getFontMetrics(font1); // The different font metrics. FontMetrics font2m = getFontMetrics(font2); FontMetrics font3m = getFontMetrics(font3); FontMetrics font4m = getFontMetrics(font4); String text = "Java is densely populated."; public void init() { setBackground(Color.white); // Change the gray background to white. } /** CREATE THE CURRENT FRAME **/ public void paint(Graphics g) { int xstart1 = (size().width - font1m.stringWidth(text))/2; // Center text horizontally. int xstart2 = (size().width - font2m.stringWidth(text))/2; int xstart3 = (size().width - font3m.stringWidth(text))/2; int xstart4 = (size().width - font4m.stringWidth(text))/2; int font1ht = font1m.getHeight(); // Get font heights. int font2ht = font2m.getHeight(); int font3ht = font3m.getHeight(); int font4ht = font4m.getHeight(); // Compute spacing between lines. int yspacing = (size().height - (font1ht + font2ht +font3ht + font4ht))/5; int ystart1 = yspacing + font1m.getAscent(); // Align text vertically. int ystart2 = ystart1 + font1m.getDescent() + yspacing + font2m.getAscent(); int ystart3 = ystart2 + font2m.getDescent() + yspacing + font3m.getAscent(); int ystart4 = ystart3 + font3m.getDescent() + yspacing + font4m.getAscent(); g.setColor(Color.blue); g.setFont(font1); g.drawString(text,xstart1,ystart1); g.setFont(font2); g.drawString(text,xstart2,ystart2); g.setFont(font3); g.drawString(text,xstart3,ystart3); g.setFont(font4); g.drawString(text,xstart4,ystart4); } }