// Run this file in html on web browser //
// Source code from http://www.simonhuggins.com/courses/javaweb/course_notes/


import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.font.*;
import java.awt.image.*;
import java.util.Random;

public class writeo1 extends JApplet implements ActionListener {

  Timer timer;
  int xPos,step=1,appletWidth,appletHeight,tempc;
  TextLayout bannerLayout;
  Shape bannerShape;
  BufferedImage bufImage;
  Graphics2D bufGraphics;
  String msg, idirection="R";

  public void init() {
    setBackground(Color.lightGray);   // Set background colour to light grey
    setForeground(Color.black);   // Sets foreground colour of Applet's canvas to Black

    String stepString = getParameter("step"); // get size of x step as a string from HTML
    if (stepString != null)  // check if the parameter was used
      step = Integer.parseInt(stepString);  // if so, convert it to an integer

    String speedString = getParameter("speed"); // get speed in ms as a string from HTML
    int speed=50;  // default speed
    if (speedString != null)  // check if the parameter was used
      speed = Integer.parseInt(speedString);  // if so, convert it to an integer

    String fontSizeString = getParameter("fontSize"); // get font size as a string from HTML
    int fontSize=26;  // default font size
    if (fontSizeString != null)  // check if the parameter was used
      fontSize = Integer.parseInt(fontSizeString);  // if so, convert it to an integer

    String bannerText = getParameter("bannerText"); // Get text to display
    Random r = new Random();   
    tempc = r.nextInt();
    if (tempc < 0) { tempc = tempc * -1; }
    tempc = tempc % 26 + 65;
    if (bannerText == null) bannerText="|" + (char)tempc + "|";
    bannerLayout = new TextLayout( ":"+bannerText+":",
                                   new Font("fixedsys", Font.BOLD, fontSize),
                                   new FontRenderContext(null,false,false) );
    bannerShape = bannerLayout.getOutline(null);

    setUpImage();

    xPos = getSize().width-6;
    appletWidth = getSize().width;
    appletHeight = getSize().height;
    timer = new Timer(speed, this); // Define a timer so actionPerformed gets called
  }

  public void setUpImage() {
    bufImage = (BufferedImage)createImage(getSize().width,getSize().height);
    bufGraphics = bufImage.createGraphics();
  }

  public void start() {
    if (!timer.isRunning()) timer.start(); // start the timer running
  }

  public void stop() {
    if (timer.isRunning()) timer.stop(); // and stop it again
  }

  public void actionPerformed(ActionEvent e) {
    if (idirection != "R") {  
      xPos-=step;       
    } else { 
      xPos+=step;
    }
    repaint(); 
  }

  public void paint (Graphics g) {

    Graphics2D g2=bufGraphics;

    if ((appletWidth!=getSize().width) || (appletHeight!=getSize().height)) setUpImage();

    appletWidth = getSize().width;
    appletHeight = getSize().height;
    double yPos = (appletHeight + bannerShape.getBounds().height)/2.0;

    // Draw border and background colour
    g2.setPaint(Color.yellow);
    g2.fill(new Rectangle2D.Double(6,6,appletWidth-13,appletHeight-13));

    if (idirection != "L") {
        if  (xPos+bannerShape.getBounds().width+step > appletWidth-6 ) { 
            xPos = appletWidth-6 ;
            idirection = "L";
        }
    } else { 
        if (xPos+bannerShape.getBounds().width+step < 6)  { 
            xPos = 6;
            idirection = "R";
        }
    }
    g2.setPaint(Color.red);
    g2.translate(xPos,yPos);
    g2.fill(bannerShape);
    g2.translate(-xPos,-yPos);
    g2.setClip(null);
    g.drawImage(bufImage,0,0,null);
    g.drawOval(10,10,100,30);
  }
}