import java.awt.*; import java.awt.event.*; // OBJETIVO: mostrado rculo en ambos paneles. public class BarraLibre extends java.applet.Applet { Choice choice; Scrollbar scrollbar; AreaGrafica AG_I, AG_D; int Posicion; // Para saber la posicion del scrollbar y decidir en que panel se dibuja public void init( ) { choice = new Choice( ); // Se crea choice.add("Linea"); choice.add("Cuadro"); choice.add("Circulo"); choice.setBounds(10, 10, 100, 30); // Se posiciona en el contenedor choice.addItemListener( new EVENTO_CHOICE( ) ); // Captura del evento scrollbar = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 100); // Se crea add(scrollbar); // Se añade scrollbar.setBounds(10, 50, 510, 30); // Se posiciona scrollbar.addAdjustmentListener( new EVENTO_SCROLL( ) ); // Se captura el evento AG_I = new AreaGrafica( ); // Se crea add(AG_I); // Se añade AG_I.setBounds(10, 90, 250, 250); // Se posiciona AG_I.setBackground(Color.orange); AG_D = new AreaGrafica( ); // Se crea add(AG_D); // Se añade AG_D.setBounds(270, 90, 250, 250); // Se posiciona AG_D.setBackground(Color.pink); } // Atenciòn al evento class EVENTO_CHOICE implements ItemListener { boolean Bandera = false; public void itemStateChanged(ItemEvent e) { String OPC = (String)e.getItem( ); AreaGrafica AG; if ( Posicion > 50 ) AG = AG_D; else AG = AG_I; if (Posicion == 50) Bandera = true; if (Bandera) { AG_D.Dibuja(3); AG_I.Dibuja(3); Bandera = false; } else { if ( OPC.equals("Linea") ) AG.Dibuja( 1 ); if ( OPC.equals("Cuadro") ) AG.Dibuja( 2 ); if ( OPC.equals("Circulo") ) AG.Dibuja( 3 ); } } } class EVENTO_SCROLL implements AdjustmentListener { public void adjustmentValueChanged(AdjustmentEvent e) { Posicion = e.getValue( ); } } } // Aqui inicia el còdigo de la clase AreaGrafica class AreaGrafica extends Panel { int TipoGrafico = 4; void Dibuja ( int TipoGrafico) { this.TipoGrafico = TipoGrafico; repaint( ); // Llama al mètodo update( ), posteriormente se llama paint( Graphics G) // El mèto update( ), se encarga de limpiar el àrea. } public void paint(Graphics G) { // Funciona sòlo con int y char switch (TipoGrafico) { case 1: G.drawLine(10, 100, 100, 100); // --(x1, y1, x2, y2)-- break; case 2: G.drawRect(50, 50, 150, 150); // --(x, y, ancho, alto)-- break; case 3: G.drawOval(50, 50, 150, 150); // --(x, y, ancho, alto)-- } } }