0% found this document useful (0 votes)
37 views

Event Handling (Actionperformed) - Part Ii Textarea and Image Manipulation Objectives

The document discusses displaying and manipulating TextArea components and images in Java. It provides code samples to display a TextArea and image, and resize images. It then provides instructions for a laboratory activity to create buttons to display different text in the TextArea and images, including: creating buttons for Apple, Bear, and Cat; displaying a default TextArea and image; and creating event handlers for each button to display the corresponding text and image.

Uploaded by

Kon Kon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Event Handling (Actionperformed) - Part Ii Textarea and Image Manipulation Objectives

The document discusses displaying and manipulating TextArea components and images in Java. It provides code samples to display a TextArea and image, and resize images. It then provides instructions for a laboratory activity to create buttons to display different text in the TextArea and images, including: creating buttons for Apple, Bear, and Cat; displaying a default TextArea and image; and creating event handlers for each button to display the corresponding text and image.

Uploaded by

Kon Kon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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