/** ~mohammad/public_html/classes/csc241/Tank/tankApplet.java
**/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class tankApplet extends Applet implements ActionListener{
displayTank t;
Button incByHundred_;
Button decByHundred_;
TextField textField_;
public tankApplet() {
t = new displayTank(80,200);
incByHundred_ = new Button("+100");
decByHundred_ = new Button("-100");
incByHundred_.addActionListener(this);
decByHundred_.addActionListener(this);
textField_ = new TextField(30);
textField_.setText("Tank is Empty!");
}
public void init() {
setLayout( new BorderLayout() );
Panel p = new Panel();
p.setLayout(new FlowLayout());
p.add(incByHundred_);
p.add(decByHundred_);
p.setSize(400,100);
add("South", textField_);
add("North", p);
}
public void paint (Graphics g) {
t.display(g);
}
public void actionPerformed(ActionEvent event) {
try{
if ((event.getSource() == incByHundred_)) {
if((t.content()+100) == t.capacity) {
textField_.setText("Tank is Full!");
t.add(100);
}
else if ((t.content()+100) > t.capacity)
textField_.setText("Tank Overflow Error!");
else {
t.add(100);
textField_.setText("Tank is " +
(int) (t.content() / t.capacity*100) +
"% Full");
}
}
else {
if( (t.content()-100) == 0 ) {
textField_.setText("Tank is Empty!");
t.remove(100);
}
else if( (t.content()-100) < 0 )
textField_.setText("Tank Underflow Error!");
else {
t.remove(100);
textField_.setText("Tank is " +
(int)(t.content() / t.capacity*100.0) +
"% Full");
}
}
repaint();
}
catch (Exception e) {}
}
}