import java.awt.*; import java.applet.*; /** * This class reads PARAM tags from its HTML host page and sets * the color and label properties of the applet. Program execution * begins with the init() method. */ public class fonts extends Applet implements Runnable //'Implements runnable' es requerido para crear la marquesina. { /** * The entry point for the applet. */ //protected void Paint(Graphics g) //{ // g.drawString("Hello Visual J++ World!",100,100); //} // Este codigo declara/inicializa los elementos requeridos para desplegar texto tipo marquesina private Thread m_fonts = null; int x = 740; int y = 380; Font font5 = new Font("MONOSPACED",Font.BOLD,15); String m_strMessage = "This 'Marquee' text, as well as all other lines in this applet, were drawn using the 'Graphics' class. This line uses 'font 5' (MONOSPACED,Font.BOLD,15) and 'Color.red'"; //********************************************************************************************** public void init() { //initForm(); Estas dos lineas fueron comentadas para evitar desplegar //usePageParams(); El mensaje generico "This string was ..." y el color de fondo (default) // TODO: Add any constructor code after initForm call. Label label1, label2; label1 = new Label(" Java's Fonts, text and colors. ",Label.CENTER); label2 = new Label(" (by Lucio Gayosso). ",Label.CENTER); Font font = new Font("Helvetica", Font.BOLD,14); label1.setFont(font); label2.setFont(font); add(label1); add(label2); Font font4 = new Font("Serif",Font.PLAIN,13); // Creacion del cuarto Font (para la lista de fonts disponibles) txArea.setEditable(false); String txAreaText = "We are using 'font 4' (Serif,Font.PLAIN,13)" + "\n" + "This is the list of available fonts in your system for JAVA:" + "\n"; txArea.setFont(font4); txArea.append(txAreaText); // Agrega el texto descriptivo de 'txArea' populateFontList(); // Llamado al metodo que obtiene la lista de fonts dispobiles en el sistema. add(txArea); // Adiciona el control 'txArea' al applet. } TextArea txArea = new TextArea(); // Declaracion del nuevo control 'TextArea' para desplegar la lista de fonts disponibles public void populateFontList() { Toolkit toolkit = Toolkit.getDefaultToolkit(); String strFontList[] = toolkit.getFontList(); for(int i=0; i, * , * and tags from * the applet's HTML host. */ labelValue = getParameter(labelParam); backgroundValue = getParameter(backgroundParam); foregroundValue = getParameter(foregroundParam); if ((labelValue == null) || (backgroundValue == null) || (foregroundValue == null)) { /** * There was something wrong with the HTML host tags. * Generate default values. */ labelValue = defaultLabel; backgroundValue = defaultBackground; foregroundValue = defaultForeground; } /** * Set the applet's string label, background color, and * foreground colors. */ label1.setText(labelValue); label1.setBackground(stringToColor(backgroundValue)); label1.setForeground(stringToColor(foregroundValue)); this.setBackground(stringToColor(backgroundValue)); this.setForeground(stringToColor(foregroundValue)); } /** * Converts a string formatted as "rrggbb" to an awt.Color object */ private Color stringToColor(String paramValue) { int red; int green; int blue; red = (Integer.decode("0x" + paramValue.substring(0,2))).intValue(); green = (Integer.decode("0x" + paramValue.substring(2,4))).intValue(); blue = (Integer.decode("0x" + paramValue.substring(4,6))).intValue(); return new Color(red,green,blue); } /** * External interface used by design tools to show properties of an applet. */ public String[][] getParameterInfo() { String[][] info = { { labelParam, "String", "Label string to be displayed" }, { backgroundParam, "String", "Background color, format \"rrggbb\"" }, { foregroundParam, "String", "Foreground color, format \"rrggbb\"" }, }; return info; } Label label1 = new Label(); /** * Intializes values for the applet and its components */ void initForm() { this.setBackground(Color.lightGray); this.setForeground(Color.black); label1.setText("label1"); this.setLayout(new BorderLayout()); this.add("North",label1); } public void paint(Graphics g) { // Creacion del primer Font Font font1 = new Font("TimesRoman",Font.PLAIN,22); g.setFont(font1); // Selecciona el primer font en la clase Graphics g.setColor(Color.green); // Selecciona el color verde para el primer font g.drawString("This text line uses 'font1' (TimesRoman,Font.PLAIN,22) and 'color.green'", 5,265);// Dibuja el texto a la clase Graphics Font font2 = new Font("Courier",Font.PLAIN,16); // Creacion del segundo Font g.setFont(font2); // Selecciona el segundo font en la clase Graphics g.setColor(Color.magenta); // Seleciona el color magenta para el segundo font g.drawString("This text line uses 'font2' (Courier,Font.PLAIN,16) and 'Color.magenta'", 5,285); // Dibuja el texto a la clase Graphics drawCrazyText(g); // Codigo para desplegar la marquesina empleando el font 'font5' FontMetrics fm = g.getFontMetrics(); g.setFont(font5); g.setColor(Color.red); g.drawString(m_strMessage, x, y); x -= 5; if (x < -fm.stringWidth(m_strMessage)) x = 740; } public void start() { if(m_fonts == null) { m_fonts = new Thread(this); m_fonts.start(); } } public void stop() { if(m_fonts != null) { m_fonts.stop(); m_fonts = null; } } public void run() { while(true) { try { repaint(); Thread.sleep(75); } catch(InterruptedException e) { stop(); } } } public void drawCrazyText(Graphics g) { Font font3 = new Font ("Helvetica", Font.PLAIN, 13); //Declaracion del nuevo font 'font3' g.setFont(font3); // Seleccion de 'font3' dentro de la clase Graphics g.setColor(Color.blue); char txt[] = new char[10]; // Buffer tipo caracter txt[0] = 'H'; txt[1] = 'e'; txt[2] = 'l'; txt[3] = 'l'; txt[4] = 'o'; g.drawChars(txt,0,5,5,300); // Buffer a usar + 1er caracter + # de caracteres + x + y for(int i=0; i<5; i++) { int nRnd = (int) (Math.random()*10.0); g.drawChars(txt, i, 1, 5+i * 10, 315 + nRnd); } getFontInfo(g, font3); } String getFontInfo(Graphics g, Font font3) { String m_strInfo, m_strInfo2="'font 3' is (Helvetica, Font.PLAIN, 13) and 'Color.blue'"; FontMetrics fm; fm = g.getFontMetrics(font3); m_strInfo = "These lines use 'font3' with Font Metrics: ascent " + fm.getAscent() + "."; m_strInfo += ", descent " + fm.getDescent() + "."; m_strInfo += ", leading " + fm.getLeading() + "."; m_strInfo += ", height " + fm.getHeight() + "."; m_strInfo += "The string width of 'Hello' is " + fm.stringWidth("Hello") + "."; g.drawString(m_strInfo, 5,335); g.drawString(m_strInfo2, 5,355); return(m_strInfo); } }