import java.awt.*; import javax.swing.*; import java.awt.event.*; // Ejemplo de hilos provenienten de // AnimationApplet // Por David Reilly // como ejemplo pra "hilos desenredados" // public class Ticker0 extends JPanel { private String animationString; private Font animationFont; private int animationWidth; private int xoffset; private int tickerWidth; public static final Dimension DEFAULT_SIZE = new Dimension( 400, 200 ); public Ticker0(String s) { animationString = s; setOpaque( true ); setBackground( Color.black ); setForeground( Color.white ); animationFont = new Font( "SansSerif", Font.BOLD, 16); animationWidth = getFontMetrics( animationFont ).stringWidth( animationString ); setFont( animationFont ); xoffset = 15; tickerWidth = DEFAULT_SIZE.width; addMouseListener( new MouseAdapter() { public void mouseClicked( MouseEvent e ) { // se mueve 5 píxels a la izquierda con cada clic del ratón xoffset -= 5; // si la cadena queda fuera de la pantalla, retrocede a la derecha if (xoffset <= -animationWidth) xoffset = tickerWidth; repaint(); } } ); } public Dimension getPreferredSize() { return DEFAULT_SIZE; } public void paintComponent(Graphics g) { super.paintComponent( g ); int yoffset = getHeight() / 2; tickerWidth = getWidth(); // permite reajustar tamaño g.drawString( animationString, xoffset, yoffset ); } public static void main( String [] args ) { JFrame aFrame = new JFrame( "Ticker" ); aFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); Ticker0 theTicker = new Ticker0( "Haga clic para que avance." ); aFrame.getContentPane().add( theTicker, BorderLayout.CENTER ); aFrame.pack(); aFrame.setVisible( true ); } }