Wednesday 2 March 2016

HelloJava4: Netscape’s Revenge

We have explored quite a few features of Java with the first three versions of the Hello Java application. But until now, our application has been rather passive; it has been completely event-driven, waiting patiently for events to come its way and responding to the whims of the user. Now our application is going to take some initiative—Hello Java4 will blink! Here is the code for our latest version:

 import java.awt.*;  
 import java.awt.event.*;  
 import javax.swing.*;  
 public class HelloJava4  
 {  
 public static void main( String[] args ) {  
 JFrame frame = new JFrame( "HelloJava4" );  
 frame.add( new HelloComponent4("Hello, Java!") );  
 frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );  
 frame.setSize( 300, 300 );  
 frame.setVisible( true );  
 }  
 }  
 class HelloComponent4 extends JComponent  
 implements MouseMotionListener, ActionListener, Runnable  
 {  
 String theMessage;  
 int messageX = 125, messageY = 95; // Coordinates of the message  
 JButton theButton;  
 int colorIndex; // Current index into someColors.  
 static Color[] someColors = {  
 Color.black, Color.red, Color.green, Color.blue, Color.magenta };  
 boolean blinkState;  
 public HelloComponent4( String message ) {  
 theMessage = message;  
 theButton = new JButton("Change Color");  
 setLayout( new FlowLayout() );  
 add( theButton );  
 theButton.addActionListener( this );  
 addMouseMotionListener( this );  
 Thread t = new Thread( this );  
 t.start();  
 }  
 public void paintComponent( Graphics g ) {  
 g.setColor(blinkState ? getBackground() : currentColor());  
 g.drawString(theMessage, messageX, messageY);  
 }  
 public void mouseDragged(MouseEvent e) {  
 messageX = e.getX();  
 messageY = e.getY();  
 repaint();  
 }  
 public void mouseMoved(MouseEvent e) { }  
 public void actionPerformed( ActionEvent e ) {  
 if ( e.getSource() == theButton )  
 changeColor();  
 }  
 synchronized private void changeColor() {  
 if (++colorIndex == someColors.length)  
 colorIndex = 0;  
 setForeground( currentColor() );  
 repaint();  
 }  
 synchronized private Color currentColor() {  
 return someColors[colorIndex];  
 }  
 public void run() {  
 try {  
 HelloJava4: Netscape’s Revenge | 59  
 while(true) {  
 blinkState = !blinkState; // Toggle blinkState.  
 repaint(); // Show the change.  
 Thread.sleep(300);  
 }  
 } catch (InterruptedException ie) { }  
 }  
 }  
Compile and run this version of HelloJava just like the others. You’ll see that the text does, in fact, blink. Our apologies if you find this annoying—it’s all in the name of education.

0 comments:

Post a Comment