Java tehtäviä syventävät vastaus4
Mureakuha
Tehtävä:
Kirjoita kello apletti. Ota tämän sivun alussa olevasta Kello tehtävästä paneli ja käytä sitä tässä tehtävässä.
[muokkaa]
ClockAppletPanel.java
import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.event.*; import java.util.*; /** * Kellopaneli. */ public class ClockAppletPanel extends JComponent { private BasicStroke handStroke, tickStroke; public ClockAppletPanel() {} /** * Konstruktori. * @param handWidth viisarin leveys * @param tickWidth kellotaulun tuntimerkin leveys */ public ClockAppletPanel( int handWidth, int tickWidth ) { handStroke = new BasicStroke( handWidth ); tickStroke = new BasicStroke( tickWidth ); } /** * Piirtometodi. * @param grfx piirtokonteksti */ public void paintComponent( Graphics grfx ) { Graphics2D g = ( Graphics2D ) grfx; g.setStroke( tickStroke ); g.setColor( Color.black ); Dimension size = this.getSize(); for ( double x = 0; x < 2 * Math.PI; x = x + 0.5236 ) // sin() ja cos() käyttävät radiaaneja, 6.2832 == 2*PI { g.drawLine( ( int ) ( size.width / 2 + Math.sin( x ) * size.width * 0.45 ), ( int ) ( size.height / 2 - Math.cos( x ) * size.height * 0.45 ), ( int ) ( size.width / 2 + Math.sin( x ) * size.width * 0.4 ), ( int ) ( size.height / 2 - Math.cos( x ) * size.height * 0.4 ) ); // piirtää kellotaulun } Calendar cal = Calendar.getInstance(); int second = cal.get( Calendar.SECOND ); int minute = cal.get( Calendar.MINUTE ); int hour = cal.get( Calendar.HOUR ); g.setStroke( handStroke ); g.setColor( Color.blue ); g.drawLine( ( int ) size.width / 2, ( int ) size.height / 2, ( int ) size.width / 2 + ( int ) ( Math.sin( 2 * Math.PI * second / 60 ) * size.width * 0.34 ), ( int ) size.height / 2 - ( int ) ( Math.cos( 2 * Math.PI * second / 60 ) * size.height * 0.34 ) ); // sekunnit g.drawLine( ( int ) size.width / 2, ( int ) size.height / 2, ( int ) size.width / 2 + ( int ) ( Math.sin( 2 * Math.PI * minute / 60 ) * size.width * 0.3 ), ( int ) size.height / 2 - ( int ) ( Math.cos( 2 * Math.PI * minute / 60 ) * size.height * 0.3 ) ); // minuutit g.drawLine( ( int ) size.width / 2, ( int ) size.height / 2, ( int ) size.width / 2 + ( int ) ( Math.sin( 2 * Math.PI * hour / 12 ) * size.width * 0.25 ), ( int ) size.height / 2 - ( int ) ( Math.cos( 2 * Math.PI * hour / 12 ) * size.height * 0.25 ) ); // tunnit } }
[muokkaa]
ClockApplet.java
import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; /** * Kelloapletti. */ public class ClockApplet extends JApplet { private int handWidth, tickWidth; private ClockAppletPanel clockPanel; private ClockTimerThread timer; private Thread thread; /** * Get a parameter value. * @param key parametri * @param def oletusarvo * @return parametrin arvo */ public String getParameter( String key, String def ) { return getParameter( key ) != null ? getParameter( key ) : def; } /** * Construct the applet. */ public ClockApplet() {} /** * Timer thread for moving the hands. */ public class ClockTimerThread implements Runnable { boolean running = true; public void run() { while ( running ) { clockPanel.repaint(); try { Thread.sleep( 950 ); } catch ( InterruptedException e ) {} } } public void stopIt() { running = false; } } /** * Initialize the applet. */ public void init() { try { handWidth = Integer.parseInt( getParameter( "handWidth", "2" ) ); tickWidth = Integer.parseInt( getParameter( "tickWidth", "1" ) ); } catch ( Exception e ) { e.printStackTrace(); } clockPanel = new ClockAppletPanel( handWidth, tickWidth ); getContentPane().add( clockPanel, BorderLayout.CENTER ); } /** * Start the applet. */ public void start() { timer = new ClockTimerThread(); thread = new Thread( timer ); thread.setDaemon( true ); thread.start(); } /** * Stop the applet. */ public void stop() { timer.stopIt(); } /** * Destroy the applet. */ public void destroy() { timer.stopIt(); } /** * Get Applet information. * @return appletin tiedot */ public String getAppletInfo() { return "Kelloapletti, (p) 2003 Petteri Hämäläinen"; } /** * Get parameter info. * @return parametritiedot */ public String[][] getParameterInfo() { String[][] pinfo = { { "handWidth", "int", "Viisarinleveys" }, { "tickWidth", "int", "Kellotaulun tuntimerkin leveys" } }; return pinfo; } }
Tämän dokumentin kopiointi, levittäminen sekä muokkaaminen on sallittua GNU Free Documentation Licensen version 1.2 tai uudemman Free Software Foundationin julkaiseman version mukaisesti, ilman muuttumattomuuslauseketta tai kansitekstejä. Tätä koskee vastuuvapaus.
Kopio lisenssistä (englanniksi) löytyy täältä.
Kopio lisenssistä (englanniksi) löytyy täältä.
Alkuperäinen (c) Petteri Hämäläinen
