FileDocCategorySizeDatePackage
HelloJava3.javaAPI DocExample1922Mon May 01 14:41:38 BST 2000None

HelloJava3

public class HelloJava3 extends JComponent implements ActionListener, MouseMotionListener

Fields Summary
int
messageX
int
messageY
String
theMessage
JButton
theButton
int
colorIndex
static Color[]
someColors
Constructors Summary
public HelloJava3(String message)


     
    theMessage = message;
    theButton = new JButton("Change Color");
    setLayout(new FlowLayout(  ));
    add(theButton);
    theButton.addActionListener(this);
    addMouseMotionListener(this);
  
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent e)

    // Did somebody push our button?
    if (e.getSource(  ) == theButton)
      changeColor(  );
  
private synchronized voidchangeColor()

    // Change the index to the next color.
    if (++colorIndex == someColors.length)
      colorIndex = 0;
    setForeground(currentColor(  )); // Use the new color.
    repaint(  ); // Paint again so we can see the change.
  
private synchronized java.awt.ColorcurrentColor()

    return someColors[colorIndex];
  
public static voidmain(java.lang.String[] args)

    JFrame f = new JFrame("HelloJava3");
    // Make the application exit when the window is closed.
    f.addWindowListener(new WindowAdapter(  ) {
      public void windowClosing(WindowEvent we) { System.exit(0); }
    });
    f.setSize(300, 300);
    f.getContentPane(  ).add(new HelloJava3("Hello, Java!"));
    f.setVisible(true);
  
public voidmouseDragged(java.awt.event.MouseEvent e)

    // Save the mouse coordinates and paint the message.
    messageX = e.getX(  );
    messageY = e.getY(  );
    repaint(  );
  
public voidmouseMoved(java.awt.event.MouseEvent e)

public voidpaintComponent(java.awt.Graphics g)

    g.drawString(theMessage, messageX, messageY);