As you can see the Java implementation of Step 1 of 15 Exercises for Learning a new Programming Language was more complicated than the C# implementation. This was largely due to one factor. You can't poll whether someone is typing from the console in Java.
The second problem is that you can't use a KeyEvent handler in the console, which would the second easiest solution to attempt. Finally a third problem is that by using an event handler you essentially need to use threading so that the loop doesn't interfere with the handler and you are able to interrupt properly. If there are better solutions I'd be very interested to see them. package learn2Java1;
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class Program extends JFrame implements KeyListener
{
private JTextArea displayArea;
private static final long serialVersionUID = 1;
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater( new Runnable( ){
public void run( )
{
createAndShowGUI( );
}
});
}
public Program( String name )
{
super( name );
}
private static void createAndShowGUI( )
{
Program frame = new Program( "Press ESC to exit program." );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.addComponentsToPane( );
frame.pack( );
frame.setVisible( true );
Thread t = new Thread( new MessageLoop( frame ) );
t.start( );
}
private void addComponentsToPane( )
{
displayArea = new JTextArea( 0 );
displayArea.setEditable( false );
displayArea.addKeyListener( this );
getContentPane( ).add( displayArea, BorderLayout.CENTER );
}
public void keyPressed(KeyEvent ke) {
checkKey( ke, "KeyPressed: " );
}
public void keyReleased(KeyEvent ke) {
checkKey( ke, "KeyReleased: " );
}
public void keyTyped(KeyEvent ke) {
checkKey( ke, "KeyTyped: " );
}
private void checkKey( KeyEvent ke, String message )
{
System.out.println( message + ke.getKeyCode() );
if ( ke.getID() != KeyEvent.KEY_TYPED && ke.getKeyCode() == KeyEvent.VK_ESCAPE )
System.exit( 0 );
}
public void updateArea( String text )
{
displayArea.setText( text );
}
private static class MessageLoop implements Runnable
{
Program messageSpace;
public MessageLoop( Program thisFrame )
{
messageSpace = thisFrame;
}
public void run( )
{
for ( int i = 0; ; i++ )
{
messageSpace.updateArea( i + " " );
try
{
Thread.sleep( 250 );
} catch ( InterruptedException ie )
{
}
}
}
}
}
posted by dharh 7:15 PM Feb 10th, 2008
Part of the passion in my life, besides life itself, is programming and computers. I especially like the idea of smart computing through smart programs and smart OSes. Which is what I want to focus on in my career, creating intelligence to various degrees to help create better and more efficient user experiences using computers.
Bellow is a table of various code snippets and articles organized by language.
15 Exercises for Learning a new Programming LanguageThis is an exercise to both learn a language, but also to compare different languages. The original idea can be found here.
posted by dharh 2:06 PM Mar 15th, 2006
« Previous 1 Next »
|
AI airships america batteries blogs books browser C# c++ chatGPT computers conversation copyright covid cpp cr-48 CSharp dharh disaster DIY DRM economy energy environment FCC gaming google government history HTML humor idt internet interview japan java javascript linkjack linux lrn2program MLP moving music nature nefeedeater neThing neTodo networking news opensource philosophy podcasts poverty programming projects python reading religion science sick simple software space sparce tagging technology twitter unbirthday video wiki
|