You are on page 1of 4

Event Handling (actionPerformed) Part II

TextArea and Image Manipulation


Objectives:
Students will be able to:
Display TextArea component into container:
Display Image in the container?
Resize images?
Manipulate TextArea Component and Image?
TextArea
import java.awt.*;
import java.awt.event.*;
public class textarea extends java.applet.Applet implements ActionListener {
String msg ="";
TextArea text = new TextArea(7,60);
Button display = new Button("SET");
Button store = new Button("GET");
public void init() {
add (display);
display.addActionListener(this);
add (store);
store.addActionListener(this);
add(text);
}
public void actionPerformed(ActionEvent ae) {
String str = ae.getActionCommand();
if(str.equals("SET")) {
text.setText("Event Handling");
}
if(str.equals("GET")) {
msg = text.getText();
repaint();
}
}
public void paint(Graphics g) {
g.drawString(msg,20,220);
}
}

Sample Output

IMAGES
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="aaaa.class" width="250" height="150">
</applet>
*/
public class images extends Applet {
Image apple;

public void init() {


apple =getImage(getDocumentBase(),"apple.jpg");
}
public void paint(Graphics g) {
g.drawImage(apple,0,0,200,200,this);
}
}
Sample Output

Whereas:
apple is the image
0,0 the x and y coordinate
200,200 size (width and height)

Laboratory Activity

Create and display three buttons: Apple, Bear & Cat


Create and display TextArea with 7 rows and 60 columns.
Default value for the TextArea : Select One
Default image to be displayed: apple.jpg
Create event handlers for the three buttons
Apple button event:
o Add and display Apple text inside TextArea.
o Change the display image to apple.jpg
Bear button event:
o Add and display Bear text inside TextArea.
o Change the display image to bear.jpg
Cat button event:
o Add and display Cat text inside TextArea.
o Change the display image to cat.jpg
Note: \n is to move the cursor down.
See sample output below:

You might also like