// AurinkoApplet.java Copyright (c) Kari Laitinen // http://www.naturalprogramming.com // 2009-12-03 File created. // 2009-12-04 Latest modification. /* Tämä ohjelma piirtää näytölle kuvan jossa aurinko paistaa taivaalla. Ohjelmassa on nappia painamalla mahdollisuus laskea aurinko, ja toisella napilla sen puolestaan saa takaisin ylös. Ohjelmassa käynnistetään säie kun aurinkoa on tarve liikuttaa. Säie päättyy kun aurinko on liikkunut haluttuun paikkaan. VAROITUS: Säikeen käyttöä ei ole tässä mietitty viimeisen päälle. On mahdollista että jos ohjelman nappeja painellaan tiuhaan, ylimääräisiä säikeitä jää aktiiviseksi, ja ohjelman toiminta muuttuu järjettömäksi. */ import java.awt.* ; import javax.swing.* ; import java.awt.event.* ; class AurinkoPanel extends JPanel implements ActionListener, Runnable { static final int AURINGON_LAKIPISTE_X = 400 ; static final int AURINGON_LAKIPISTE_Y = 120 ; static final int AURINGON_HALKAISIJA = 120 ; int auringon_keskipiste_x = AURINGON_LAKIPISTE_X ; int auringon_keskipiste_y = AURINGON_LAKIPISTE_Y ; Thread saie_auringon_liikutteluun ; static final int AURINKO_LAKIPISTEESSA = 0 ; static final int AURINKO_LASKEMASSA = 1 ; static final int AURINKO_NOUSEMASSA = 2 ; static final int AURINKO_PIILOSSA = 3 ; int auringon_tila = AURINKO_LAKIPISTEESSA ; JButton up_button, down_button ; String viestiteksti_naytolle = "Ohjelma käynnistynyt" ; int applet_width, applet_height ; public AurinkoPanel( int given_applet_width, int given_applet_height ) { applet_width = given_applet_width ; applet_height = given_applet_height ; setLayout( new BorderLayout() ) ; JPanel operations_panel = new JPanel() ; up_button = new JButton( " Up " ) ; down_button = new JButton( " Down " ) ; up_button.addActionListener( this ) ; down_button.addActionListener( this ) ; operations_panel.add( up_button ) ; operations_panel.add( down_button ) ; add( "South", operations_panel ) ; } public void run() { viestiteksti_naytolle = "Method run() started." ; up_button.setEnabled( false ) ; down_button.setEnabled( false ) ; while ( auringon_tila == AURINKO_LASKEMASSA ) { auringon_keskipiste_x = auringon_keskipiste_x + 1 ; auringon_keskipiste_y = auringon_keskipiste_y + 1 ; if ( auringon_keskipiste_y - AURINGON_HALKAISIJA / 2 > applet_height / 2 ) { auringon_tila = AURINKO_PIILOSSA ; } try { Thread.sleep( 50 ) ; } catch ( InterruptedException caught_exception ) { } repaint() ; } while ( auringon_tila == AURINKO_NOUSEMASSA ) { auringon_keskipiste_x = auringon_keskipiste_x + 1 ; auringon_keskipiste_y = auringon_keskipiste_y - 1 ; if ( auringon_keskipiste_y <= AURINGON_LAKIPISTE_Y ) { auringon_tila = AURINKO_LAKIPISTEESSA ; } try { Thread.sleep( 50 ) ; } catch ( InterruptedException caught_exception ) { } repaint() ; } up_button.setEnabled( true ) ; down_button.setEnabled( true ) ; viestiteksti_naytolle = "Method run() stopped. " ; } public void actionPerformed( ActionEvent event ) { if ( event.getSource() instanceof JButton ) { if ( event.getSource() == up_button ) { // Aurinko on nyt sitten oikeassa reunassa piilossa. // Siirretään se vasempaan reunaan piiloon. auringon_keskipiste_x = 2 * AURINGON_LAKIPISTE_X - auringon_keskipiste_x ; auringon_tila = AURINKO_NOUSEMASSA ; saie_auringon_liikutteluun = new Thread( this ) ; saie_auringon_liikutteluun.start() ; } else if ( event.getSource() == down_button ) { saie_auringon_liikutteluun = new Thread( this ) ; auringon_tila = AURINKO_LASKEMASSA ; saie_auringon_liikutteluun.start() ; } repaint() ; } } public void paintComponent( Graphics graphics ) { super.paintComponent( graphics ) ; graphics.setColor( Color.BLUE ) ; // Taivaan piiroväri graphics.fillRect( 0, 0, applet_width, applet_height / 2 ) ; graphics.setColor( Color.YELLOW ) ; // Color for the "sun" graphics.fillOval( auringon_keskipiste_x - AURINGON_HALKAISIJA / 2, auringon_keskipiste_y - AURINGON_HALKAISIJA / 2, AURINGON_HALKAISIJA, AURINGON_HALKAISIJA ) ; graphics.setColor( Color.GREEN ) ; // Maan piirtoväri graphics.fillRect( 0, applet_height / 2, applet_width, applet_height / 2 ) ; graphics.setColor( Color.BLACK ) ; graphics.drawString( viestiteksti_naytolle, 5, applet_height - 40 ) ; } } public class AurinkoApplet extends JApplet { public void init() { getContentPane().add( new AurinkoPanel( getSize().width, getSize().height ) ) ; } }