import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.FontMetrics.*;
public class rotatePictures extends Applet implements ActionListener
{
private Button back,forward;
private String [] imageArray;
private String [] descriptionArray;
private String currentDescription;
private int index = 0;
private Image image;
private int iCount = 1;
public void init()
{
setLayout(new BorderLayout());
Panel bottom = new Panel();
back = new Button("Back");
forward = new Button("Forward");
bottom.add(back);
bottom.add(forward);
add("South", bottom);
back.addActionListener(this);
forward.addActionListener(this);
iCount = Integer.parseInt(getParameter("imageCount"));
imageArray = new String[iCount];
descriptionArray = new String[iCount];
for (int i=1; i<=iCount; i++)
{
imageArray[i-1] = getParameter(("loadPictures"+i));
descriptionArray[i-1] = getParameter(("description"+i));
}
image = getImage(getDocumentBase(),imageArray[index]);
currentDescription = descriptionArray[index];
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == back)
{
if (index!=0)
{
index--;
image = getImage(getDocumentBase(),imageArray[index]);
currentDescription = descriptionArray[index];
System.out.println(index);
}
else
{
index = (imageArray.length-1);
image = getImage(getDocumentBase(), imageArray[index]);
currentDescription = descriptionArray[index];
System.out.println(index);
}
}
else if (e.getSource() == forward)
{
if (index<(imageArray.length-1))
{
index++;
image = getImage(getDocumentBase(),imageArray[index]);
currentDescription = descriptionArray[index];
System.out.println(index);
}
else
{
index = 0;
image = getImage(getDocumentBase(),imageArray[index]);
currentDescription = descriptionArray[index];
System.out.println(index);
}
}
repaint();
}
public void paint(Graphics g)
{
int appletW = Integer.parseInt(getParameter("appletWidth"));
int appletH = Integer.parseInt(getParameter("appletHeight"));
Graphics2D g2;
g2 = (Graphics2D) g;
setBackground(Color.white);
FontRenderContext frc = g2.getFontRenderContext();
Font f = new Font("Comic Sans MS", Font.BOLD, 16);
String theTitle = getParameter("title");
TextLayout t1 = new TextLayout(theTitle, f, frc);
g2.setColor(Color.red);
FontMetrics fMeasure = getFontMetrics(f);
int fontWidth = fMeasure.stringWidth(theTitle);
int titleStartW = (appletW-fontWidth)/2;
t1.draw(g2, titleStartW, 40);
int width = image.getWidth(this);
int height = image.getHeight(this);
if (width>=appletW || (height+50)>=appletH)
{
g.drawString("The image is too big to be drawn", 20, appletH/2);
g.drawString("in the space provided by the applet!!", 20, appletH/2+15);
}
else
{
int centerWidth = (appletW-width)/2;
int centerHeight = (appletH-50-height)/2;
g.drawImage(image, centerWidth, centerHeight, this);
Font f2 = new Font("Comic Sans MS", Font.PLAIN, 10);
TextLayout t2 = new TextLayout(currentDescription, f2, frc);
FontMetrics fMeasure2 = getFontMetrics(f2);
int descriptionWidth = fMeasure2.stringWidth(currentDescription);
int descriptionHeight = fMeasure2.getHeight();
int descriptionStartW = (appletW - width)/2 + width + 10;
int descriptionStartH = appletH/2;
if(descriptionWidth+descriptionStartW>appletW)
{
int newHeightIndex = 0;
while(fMeasure2.stringWidth(currentDescription)+descriptionStartW>appletW)
{
String theCurrentString = currentDescription.substring(0,1);
currentDescription = currentDescription.substring(1,currentDescription.length());
int tempWidth = fMeasure2.stringWidth(theCurrentString);
while(tempWidth+descriptionStartW<appletW)
{
theCurrentString += currentDescription.substring(0,1);
currentDescription = currentDescription.substring(1,currentDescription.length());
tempWidth = fMeasure2.stringWidth(theCurrentString);
}
TextLayout t3 = new TextLayout(theCurrentString, f2, frc);
t3.draw(g2, descriptionStartW, descriptionStartH+(newHeightIndex*15));
newHeightIndex++;
}
TextLayout t4 = new TextLayout(currentDescription, f2, frc);
t4.draw(g2, descriptionStartW, descriptionStartH+(newHeightIndex*15));
}
else
{
t2.draw(g2, descriptionStartW, descriptionStartH);
}
}
}
};