I would very much like a more experienced programmer to critique my
applet. It was my first project(I have done some
servlets since then).
It evolved over time gaining functionality. Although it works fine I am sure it could be better written. Things like using different modifiers for example. Just whatever you would have done differently. Im sure even I might have written it differently if all funtionality were planned originally.
/*
* farm.java
*
* Created on May 30, 2000, 9:39 AM
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/**
*
* @author frontoffice
* @version
*/
public class Farm extends java.applet.Applet implements ItemListener {
Animals an;
Text text;
Thread runner = null;
public static int selection;
/** Initializes the applet farm */
public void init () {
selection = 6;
an = new Animals(this);
text = new Text();
initComponents ();
picts.add("sheep.jpg");
picts.add("dog.gif");
picts.add("bird.jpg");
picts.add("cow.jpg");
picts.add("rooster.jpg");
picts.add("donkey.jpg");
add(BorderLayout.SOUTH,text);
add(BorderLayout.CENTER,an);
picts.addItemListener(this);
}
/** This method is called from within the init() method to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the FormEditor.
*/
private void initComponents () {//GEN-BEGIN:initComponents
picts = new java.awt.Choice ();
setLayout (new java.awt.BorderLayout ());
picts.setFont (new java.awt.Font ("Dialog", 0, 11));
picts.setBackground (java.awt.Color.white);
picts.setForeground (java.awt.Color.black);
add (picts, java.awt.BorderLayout.WEST);
}//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
private java.awt.Choice picts;
// End of variables declaration//GEN-END:variables
synchronized public void itemStateChanged(final java.awt.event.ItemEvent p1) {
selection = picts.getSelectedIndex();
Image im = this.getImage(this.getDocumentBase(),picts.getSelectedItem());
an.updateImage(im);
}
public void start() {
if (runner == null) {
runner = new Thread(text);
runner.start();
}
else if (runner.isAlive()) {
runner.resume();
}
}
public void stop() {
if (runner.isAlive())
runner.suspend();
}
public void destroy() {
if (runner.isAlive()) {
runner.stop();
runner = null;
}
}
}
class Animals extends Canvas implements MouseListener {
Image im;
AudioClip sheep;
AudioClip dog;
AudioClip bird;
AudioClip
cow;
AudioClip rooster;
AudioClip donkey;
public Animals(Farm newHome) {
addMouseListener(this);
Farm fm = newHome;
sheep = fm.getAudioClip(fm.getDocumentBase(),"sheep.au");
dog = fm.getAudioClip(fm.getDocumentBase(),"dog.au");
bird = fm.getAudioClip(fm.getDocumentBase(),"bird.au");
cow = fm.getAudioClip(fm.getDocumentBase(),"cow.au");
rooster = fm.getAudioClip(fm.getDocumentBase(),"rooster.au");
donkey = fm.getAudioClip(fm.getDocumentBase(),"donkey.au");
}
synchronized public void paint(Graphics g)
{
if (im != null)
g.drawImage(im,0,0,this);
}
public void updateImage(Image im)
{
this.im = im;
repaint();
}
public void mouseReleased(final java.awt.event.MouseEvent p1) {
}
public void mouseEntered(final java.awt.event.MouseEvent p1) {
this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
public void mouseClicked(final java.awt.event.MouseEvent p1) {
}
public void mousePressed(final java.awt.event.MouseEvent p1) {
switch(Farm.selection) {
case 0:
sheep.play();
break;
case 1:
dog.play();
break;
case 2:
bird.play();
break;
case 3:
cow.play();
break;
case 4:
rooster.play();
break;
case 5:
donkey.play();
}
}
public void mouseExited(final java.awt.event.MouseEvent p1) {
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
}
class Text extends Canvas implements Runnable {
String message = "Select an animal";
int message_x, message_y = 16;
int min_x, max_x;
int scroll_frames = 100;
int sleepTime = 100;
Image OSC = null;
Graphics OSC_graphics;
int width = 400;
int height = 20;
public Text() {
setSize(400,20);
}
synchronized public void paint(Graphics g) {
if (OSC != null) // if off-screen image exists, copy it to screen
g.drawImage(OSC,0,0,this);
else { // otherwise, fill the canvas with the background color
g.setColor(getBackground());
g.fillRect(0,0,size().width,size().height);
}
}
public void update(Graphics g) {
// redefine update() so that it doesn't erase the applet;
// this will avoid flickering when the applet is redrawn
paint(g);
}
synchronized void drawNextFrame(int scroll_frame) {
OSC = createImage(width,height);
OSC_graphics = OSC.getGraphics();
Font font = getFont();
font = new Font("TimesRoman",Font.PLAIN,15
);
OSC_graphics.setFont(font);
FontMetrics fm = getFontMetrics(font);
min_x = -fm.stringWidth(message);
max_x = width;
message_x = max_x - ((scroll_frame * (max_x-min_x)) / scroll_frames);
OSC_graphics.setColor(Color.white);
OSC_graphics.fillRect(0,0,width,height);
OSC_graphics.setColor(Color.black);
OSC_graphics.drawString(message,message_x,message_y);
}
public void run() {
int scroll_frame = 0;
while(true) {
switch(Farm.selection) {
case 0:
message = "Click the sheep";
break;
case 1:
message = "Click the dog";
break;
case 2:
message = "Click the bird";
break;
case 3:
message = "Click the cow";
break;
case 4:
message = "Click the rooster";
break;
case 5:
message = "Click the donkey";
}
drawNextFrame(scroll_frame);
repaint();
scroll_frame++;
if (scroll_frame > scroll_frames)
scroll_frame = 0;
try { Thread.sleep(sleepTime); }
catch (InterruptedException e) { }
}
}
}