Défilement horizontal d'un texte dans une applet [nouveau!! applet 100% configurable]

Description

Le code fait défiler un texte dans une Applet Java. Le code a été testé sous java 5.0.
L'applet permet de changer facilement les paramètres à travers une page HTML, il est possible de changer les couleurs, la police, le sens de défilement ainsi que la vitesse.

Source / Exemple :


/**

  • @author bouba331
  • /
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GraphicsEnvironment; import java.awt.font.TextLayout; import javax.swing.JApplet; /**
  • An applet class used to display a scrolling message
  • /
public class AppletDefil extends JApplet implements Runnable { /**
  • The serial version UID used for deserialization
  • /
private static final long serialVersionUID = 2780848656726242957L; /**
  • The animation speed in milliseconds.
  • /
private int animationSpeed = 20; /**
  • Thread used to animate the applet (make pauses)
  • /
private Thread animationThread; /**
  • Object used to describe the applet background color
  • /
private Color appletBackgroundColor; /**
  • Blue value for the applet background color
  • /
private int appletBackgroundColorBlue = 0; /**
  • Green value for the applet background color
  • /
private int appletBackgroundColorGreen = 0; /**
  • Blue value for the applet background color
  • /
private int appletBackgroundColorRed = 0; /**
  • Font object for our message
  • /
private Font messagefont; /**
  • The name of the font used to display the message
  • /
private String messagefontName = "Times"; /**
  • The size of the font used to display the message
  • /
private int messageFontSize = 12; /**
  • The style of the font used to display the message
  • /
private int messageFontStyle = Font.PLAIN; /**
  • The message to display
  • /
private String message = "Message"; /**
  • Object used to describe the color of the message
  • /
private Color messageColor; /**
  • Blue value for the color of the message
  • /
private int messageColorBlue = 255; /**
  • Green value for color of the message
  • /
private int messageColorGreen = 255; /**
  • Red value for the color of the message
  • /
private int messageColorRed = 255; /**
  • The scroll direction, positive value indicate left to right, negative
  • value right to left
  • /
private int scrollDirection; /**
  • Object used to describe the scroll zone background color
  • /
private Color scrollZoneBackgroundColor; /**
  • Blue value for the scroll zone background color
  • /
private int scrollZoneBackgroundColorBlue = 0; /**
  • Green value for the scroll zone background color
  • /
private int scrollZoneBackgroundColorGreen = 0; /**
  • Red value for the scroll zone background color
  • /
private int scrollZoneBackgroundColorRed = 0; /**
  • X bottom right corner coordinate of the scroll zone
  • /
private int scrollZoneBottomRightCornerX = 390; /**
  • Y bottom right corner coordinate of the scroll zone
  • /
private int scrollZoneBottomRightCornerY = 50; /**
  • X top left corner coordinate of the scroll zone
  • /
private final int scrollZoneTopLeftCornerX = 10; /**
  • Y top left corner coordinate of the scroll zone
  • /
private final int scrollZoneTopLeftCornerY = 10; /**
  • The height of our message in pixels, will be computed in the init()
  • function.
  • /
private int textHeight; /**
  • The width of our message in pixels, will be computed in the init()
  • function.
  • /
private int textWidth; /**
  • An x position updated to animate the message.
  • /
private int x; /**
  • @see java.applet.Applet#destroy()
  • /
@Override public void destroy() { // Free the animation Thread animationThread = null; } /**
  • @see java.applet.Applet#init()
  • /
@Override public void init() { getAppletParameters(); // initialize the scroll zone background color scrollZoneBackgroundColor = new Color(scrollZoneBackgroundColorRed, scrollZoneBackgroundColorGreen, scrollZoneBackgroundColorBlue); // initialize the applet background color appletBackgroundColor = new Color(appletBackgroundColorRed, appletBackgroundColorGreen, appletBackgroundColorBlue); // initialize the foreground color (text color) of the applet messageColor = new Color(messageColorRed, messageColorGreen, messageColorBlue); // Initialize the font messagefont = new Font(messagefontName, messageFontStyle, messageFontSize); // Compute the width of the message Graphics2D g = (Graphics2D) this.getGraphics(); TextLayout tl = new TextLayout(message, messagefont, g .getFontRenderContext()); textWidth = (int) tl.getBounds().getWidth(); textHeight = (int) tl.getBounds().getHeight(); } /**
  • @see java.awt.Container#paint(java.awt.Graphics)
  • /
@Override public void paint(Graphics graphics) { Graphics2D g = (Graphics2D) graphics; // Sets the background color and feels the rectangle g.setColor(scrollZoneBackgroundColor); g.fillRect(scrollZoneTopLeftCornerX, scrollZoneTopLeftCornerY, scrollZoneBottomRightCornerX, scrollZoneBottomRightCornerY); // Sets the text properties, color and font. g.setColor(messageColor); g.setFont(messagefont); // Draw our message g.drawString(message, x, scrollZoneTopLeftCornerY + textHeight); // Fill the borders g.setColor(appletBackgroundColor); g.fillRect(0, 0, this.getWidth(), scrollZoneTopLeftCornerY); g.fillRect(0, scrollZoneBottomRightCornerY, this.getWidth(), this .getHeight()); g.fillRect(0, 0, scrollZoneTopLeftCornerX, this.getHeight()); g.fillRect(scrollZoneBottomRightCornerX, 0, this.getWidth(), this .getHeight()); } /**
  • @see java.lang.Runnable#run()
  • /
public void run() { while (animationThread != null) updatePosition(); } /**
  • @see java.applet.Applet#start()
  • /
@Override public void start() { // Initialize and start our annimation thread if (animationThread == null) animationThread = new Thread(this); animationThread.start(); } /**
  • @see java.applet.Applet#stop()
  • /
@Override public void stop() { // Stop the animation Thread animationThread = null; } /**
  • Get an integer parameter from the applet parameters
  • @param parameterName
  • The name of the parameter to retrieve
  • @param parameter
  • The value of the default parameter
  • @return The value of the parameter retrieved from the <param> tag or the
  • default parameter value if no <param name=parameterName/> has
  • been specified in the <applet> tag.
  • /
private int getAppletParameterInt(String parameterName, int parameter) { int appletParameterInt; String appletParameterString = getParameter(parameterName); try { appletParameterInt = Integer.parseInt(appletParameterString); } catch (NumberFormatException e) { return parameter; } return appletParameterInt; } /**
  • Retrieve the applet parameters
  • /
private void getAppletParameters() { // Retrieve the message if (getParameter("message") != null) message = getParameter("message"); // Retrieve the animation speed animationSpeed = getAppletParameterInt("animation_speed", animationSpeed); // Retrieve the scroll direction scrollDirection = Integer.parseInt(getParameter("scroll_direction")); // Retrieve the scroll zone background colors scrollZoneBackgroundColorRed = getAppletParameterInt( "scroll_zone_background_color_red", scrollZoneBackgroundColorRed); scrollZoneBackgroundColorGreen = getAppletParameterInt( "scroll_zone_background_color_green", scrollZoneBackgroundColorGreen); scrollZoneBackgroundColorBlue = getAppletParameterInt( "scroll_zone_background_color_blue", scrollZoneBackgroundColorBlue); // Retrieve the applet background colors appletBackgroundColorRed = getAppletParameterInt( "applet_background_color_red", appletBackgroundColorRed); appletBackgroundColorGreen = getAppletParameterInt( "applet_background_color_green", appletBackgroundColorGreen); appletBackgroundColorBlue = getAppletParameterInt( "applet_background_color_blue", appletBackgroundColorBlue); // Retrieve the text message colors messageColorRed = getAppletParameterInt("message_color_red", messageColorRed); messageColorGreen = getAppletParameterInt("message_color_green", messageColorGreen); messageColorBlue = getAppletParameterInt("message_color_blue", messageColorBlue); // Retrieve font properties if (getParameter("message_font_name") != null) messagefontName = getParameter("message_font_name"); String fontStyleString = getParameter("message_font_style"); if (fontStyleString != null) if (fontStyleString.equals("BOLD")) messageFontStyle = Font.BOLD; else if (fontStyleString.equals("ITALIC")) messageFontStyle = Font.ITALIC; else if (fontStyleString.equals("PLAIN")) messageFontStyle = Font.PLAIN; else if (fontStyleString.equals("BOLD_ITALIC")) messageFontStyle = Font.BOLD | Font.ITALIC; messageFontSize = getAppletParameterInt("message_font_size", messageFontSize); } /**
  • Retrieve informations about applet parameters.
  • @see java.applet.Applet#getParameterInfo()
  • /
public String[][] getParameterInfo() { String[][] info = { // Parameter Name Kind of Value Description { "animation_speed", "int", "Speed of the animation (pause between applet updates) in milliseconds" }, { "applet_background_color_blue", "int", "Blue value for the applet background color" }, { "applet_background_color_green", "int", "Green value for the applet background color" }, { "applet_background_color_red", "int", "Red value for the applet background color" }, { "message_font_name", "String", "Font name for the message to display" }, { "message_font_size", "int", "Font size for the message to display" }, { "message_font_type", "String", "Font type for the message to display" }, { "message", "String", "The message to display" }, { "message_color_blue", "int", "Blue value for the message text color" }, { "message_color_green", "int", "Green value for the message texte color" }, { "message_color_red", "int", "Red value for the message text color" }, { "scroll_direction", "int", "Direction of the scrolling, a positive" + " value indicate left to right scrolling," + " negative value right from left" }, { "scroll_zone_background_color_blue", "int", "Blue value for the scroll zone backrgound color" }, { "scroll_zone_background_color_green", "int", "Green value for the scroll zone background color" }, { "scroll_zone_background_color_red", "int", "Red value for the scroll zone backrgound color" }, }; return info; } /**
  • Wait for 'animationSpeed' milliseconds and redraw the scene.
  • /
private void pauseAnimation() { try { Thread.sleep(animationSpeed); repaint(); } catch (InterruptedException ie) { } } /**
  • Update the position of our message and redraw the the scene.
  • /
private void updatePosition() { if (scrollDirection >= 0) for (x = (scrollZoneTopLeftCornerX - textWidth); x < scrollZoneBottomRightCornerX; x++) pauseAnimation(); else for (x = scrollZoneBottomRightCornerX; x > (scrollZoneTopLeftCornerX - textWidth); x--) pauseAnimation(); } }

Conclusion :


Tout ce dont vous avez besoin pour éxécuter cet applet se trouve dans le zip.

Pour ce qui est des noms de polices ils dépendent du système sur lequel est exécutée l'applet. Vous pourrez obtenir une liste des polices disponibles grâce à cette fonction. Cependant, si vous souhaitez mettre l'applet sur votre site je vous conseil d'utiliser des polices connues.

private void listFontNames() {

GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();

System.out.println("----- Font families -----");
String[] fontFamilyNames = ge.getAvailableFontFamilyNames();
for (int i = 0; i < fontFamilyNames.length; ++i)
System.out.println(fontFamilyNames[i]);

System.out.println();

Font[] allFonts = ge.getAllFonts();

System.out.println("----- Font names----- ");
for (int i = 0; i < allFonts.length; ++i)
System.out.println(allFonts[i].getName());
}

Voilà, si vous avez des critiques, suggestions ou idées d'amélioration n'hésitez pas.

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.