import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
/**
* AI: Francisco Lara-Dammer
* Lab 8
* This program receives input (an integer m) from a text field,
* and draws the letter 'L' of height m (after hitting the ENTER key).
*/
public class Lab8 extends Applet implements KeyListener{
boolean paint = false;
int letterDimension;//This is for the height of the letter.
TextField ts;
int width;//This is for the width of your applet window
int height;//This is for the width of your applet window
public void init(){
Label l = new Label("The height of your letter in pixels");
ts = new TextField(4);
//listens when your is located in the textfield area.
ts.addKeyListener(this);
add(l);
add(ts);
}
public void paint(Graphics g){
width = getSize().width;
int height = getSize().height;
if(paint){
//Let's draw the letter at the specified location.
drawLetterL(g, width/2, height/3, letterDimension);
}
}
public void keyPressed(KeyEvent evt){
int keyCode = evt.getKeyCode();
String t = "";
Graphics g;
if(keyCode == KeyEvent.VK_ENTER){
paint = true;
letterDimension = Integer.parseInt(ts.getText());
repaint();
}
}
/**
* Does nothing. Necessary to implement KeyListener.
*/
public void keyTyped(KeyEvent evt){
}
/**
* Does nothing. Necessary to implement KeyListener.
*/
public void keyReleased(KeyEvent evt){
}
/**
* Draws the letter L.
*/
private void drawLetterL(Graphics g, int x, int y, int h){
g.setColor(Color.red);
g.fillRect(x,y, h/8, h);
g.fillRect(x-h/8, y, 3*h/8, h/8);
g.fillRect(x-h/8, y +h-h/8, (3*h)/4, h/8);
g.fillRect(x-h/8+(3*h)/4-h/8, y +h-h/8-h/8, h/8, 2*h/8);
}
}