// 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.*;
public class banner1 extends JApplet implements ActionListener {
Timer timer;
int xPos,step=1,appletWidth,appletHeight;
TextLayout bannerLayout;
Shape bannerShape;
BufferedImage bufImage;
Graphics2D bufGraphics;
String msg = "Hello my student";
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
if (bannerText == null) bannerText=msg;
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) {
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.lightGray);
g2.draw3DRect(0, 0, appletWidth-1, appletHeight-1, true); //raised
g2.draw3DRect(5, 5, appletWidth-11, appletHeight-11, false); //lowered
g2.fill(new Rectangle2D.Double(6,6,appletWidth-13,appletHeight-13));
g2.setClip(new Rectangle2D.Double(6,6,appletWidth-13,appletHeight-13));
if ( (xPos+bannerShape.getBounds().width+step) < 6 ) xPos = appletWidth-6;
g2.setPaint(Color.red);
g2.translate(xPos,yPos);
g2.fill(bannerShape);
g2.translate(-xPos,-yPos);
g2.setClip(null);
g.drawImage(bufImage,0,0,null);
}
public String getAppletInfo() {
return "Title: Banner2\nAuthor: Simon Huggins\n"+
"Example to scroll text (as graphics) across the screen using double-buffering";
}
}