//-------------------------------------------------------------------------------------------------
//
// MovingComponent.java
//
// written by:    Simon Parsons
// last modified: 30th July 2012
//

// An object that prints a string and moves its position on the screen. based on
// the example from "Learning Java" by Niemeyer and Knudsen, page 49-51.

import java.awt.*;                                                 // Use the awt library
import javax.swing.*;                                              // Use the swing library
import java.awt.event.*;

// The Moving Component class is a subclass of JComponent and defines an object that listens
// for actions (in this case a button press).

public class MovingComponent extends JComponent 
	implements ActionListener
{   
	int msgX = 125, msgY = 95;                                     // Message coordinates
	JButton theButton;                                             // A button object
	
	public MovingComponent() {
		theButton = new JButton("Press me!");                      // Create button, set the layout
		setLayout(new FlowLayout());                               // type and then add the button.
		add(theButton);
		theButton.addActionListener(this);                         // Get ready for button presses
	}
	
	public void paintComponent(Graphics g){                        // Draw a string at a specific
		g.drawString("Hello, Java!", msgX, msgY);                  // location within the frame.
	}
	
	public void actionPerformed(ActionEvent e){
		// If the button is pressed, move the message
		if(e.getSource() == theButton){
			msgX++;
			msgY++;
			repaint();
		}
	}
	
}