Back to the 2019 paper
3. HTML Deployment File (
Module III: Basics of Web Programming
201914m
Write an Applet application which will display colours in list box and a scrolling banner which scrolls from left to right within a browser. When user selects any color from the list, it changes the color of scrolling banner.
Worked SolutionAI Assisted
Answer: Java Applet Application — Color List Box & Scrolling Banner
1. Problem Requirements Breakdown
- Color List Box (
java.awt.List): Displays multiple color names (e.g., Red, Green, Blue, Magenta, Orange) allowing user selection. - Scrolling Banner: A text banner that continuously scrolls horizontally from left to right across the Applet screen using a background
Thread(Runnableinterface). - Dynamic Color Change (
ItemListener): When the user selects a color from the list, the banner's text/fill color dynamically updates to the selected color.
+-------------------------------------------------------------+
| [ Color List Box ] SCROLLING BANNER TEXT -----> |
| | Red | (Updates X position in Thread) |
| | Green | |
| | Blue | (Selected color updates text) |
| | Magenta | |
+-------------------------------------------------------------+
2. Complete Java Applet Source Code
import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.List;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
/*
<applet code="ColorBannerApplet.class" width="650" height="350">
</applet>
*/
public class ColorBannerApplet extends Applet implements Runnable, ItemListener {
// GUI List Box for selecting colors
private List colorList;
// Scrolling Banner properties
private String bannerText = "Welcome to Bihar Engineering University - Web Technology Exam Archive";
private int bannerX = -300; // Starting X coordinate (left)
private int bannerY = 220; // Y coordinate
private Color bannerColor = Color.RED; // Default banner color
// Animation thread
private Thread animationThread = null;
private volatile boolean running = false;
// Applet Initialization
@Override
public void init() {
setLayout(null); // Absolute positioning layout
setBackground(new Color(245, 246, 242));
// 1. Create and populate the Color List Box
colorList = new List(5, false); // 5 visible rows, single selection
colorList.add("Red");
colorList.add("Green");
colorList.add("Blue");
colorList.add("Magenta");
colorList.add("Orange");
colorList.add("Dark Teal");
colorList.add("Black");
// Set position and dimensions of the list box
colorList.setBounds(30, 60, 140, 110);
colorList.select(0); // Select 'Red' by default
// Register ItemListener to capture user selection
colorList.addItemListener(this);
// Add component to Applet
add(colorList);
}
// Applet Start: Launch Animation Thread
@Override
public void start() {
if (animationThread == null) {
running = true;
animationThread = new Thread(this);
animationThread.start();
}
}
// Animation Loop: Scrolls Banner from Left to Right
@Override
public void run() {
while (running) {
// Move banner coordinate to the right
bannerX += 5;
// When the banner scrolls off the right edge, wrap back to the left
if (bannerX > getWidth()) {
bannerX = -gEstimateTextWidth();
}
repaint(); // Request screen redraw
try {
Thread.sleep(60); // Control scrolling speed (approx 16 FPS)
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}
}
// Rough text width estimation for wrapping
private int gEstimateTextWidth() {
return bannerText.length() * 10;
}
// Applet Stop: Stop Thread cleanly
@Override
public void stop() {
running = false;
animationThread = null;
}
// Event Handler for List Box Selection
@Override
public void itemStateChanged(ItemEvent e) {
String selected = colorList.getSelectedItem();
if ("Red".equalsIgnoreCase(selected)) {
bannerColor = new Color(178, 58, 46);
} else if ("Green".equalsIgnoreCase(selected)) {
bannerColor = new Color(34, 139, 34);
} else if ("Blue".equalsIgnoreCase(selected)) {
bannerColor = new Color(0, 102, 204);
} else if ("Magenta".equalsIgnoreCase(selected)) {
bannerColor = new Color(180, 0, 180);
} else if ("Orange".equalsIgnoreCase(selected)) {
bannerColor = new Color(230, 120, 0);
} else if ("Dark Teal".equalsIgnoreCase(selected)) {
bannerColor = new Color(31, 75, 67);
} else if ("Black".equalsIgnoreCase(selected)) {
bannerColor = Color.BLACK;
}
repaint(); // Immediately redraw with new color
}
// Screen Paint Rendering
@Override
public void paint(Graphics g) {
// UI Labels
g.setColor(new Color(27, 36, 48));
g.setFont(new Font("SansSerif", Font.BOLD, 15));
g.drawString("Select Banner Color:", 30, 45);
// Banner Track Area Box
g.setColor(new Color(235, 237, 231));
g.fillRect(10, 185, getWidth() - 20, 70);
g.setColor(new Color(219, 223, 215));
g.drawRect(10, 185, getWidth() - 20, 70);
// Draw Scrolling Banner
g.setColor(bannerColor);
g.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 20));
g.drawString(bannerText, bannerX, bannerY);
}
}
3. HTML Deployment File (banner.html)
<!DOCTYPE html>
<html>
<head>
<title>Color Banner Applet Demo</title>
</head>
<body>
<h2>Java Applet: Scrolling Banner with Color Selector</h2>
<applet code="ColorBannerApplet.class" width="650" height="350">
Java Applets are not supported in your environment.
</applet>
</body>
</html>
4. Lifecycle & Flow Explanation
init(): Creates theListcomponent, populates color names, sets absolute bounds (setBounds), and registersaddItemListener(this).start()/run(): Starts the background thread that continuously incrementsbannerXby 5 pixels and invokesThread.sleep(60)to produce smooth left-to-right scrolling.itemStateChanged(): Triggers immediately when a user clicks any color in the list, maps the selection to ajava.awt.Colorobject, and updatesbannerColor.paint(): Renders the background container and draws the text at dynamic position(bannerX, bannerY)withbannerColor.
Similar questions
Introduction To Java Programming LanguageWrite a short note on Applets and Applet Parameters.20267mWeb TechnologyWrite a program to display current cursor position of the mouse pointer on an Applet using MouseMotionListener interface.201914mWeb TechnologyWrite short notes on the following : (a) HTML commands (b) DHTML dragging and drooping (c) AWT (d) JDBC202014mIntroduction To Java Programming LanguageConstruct an AWT application containing a frame, label, text field and button. Develop an event-handling mechanism that displays a message when the button is pressed.20267m
PreviousWrite a program to display current cursor position of the mouse pointer on an Applet using MouseMotionListener interface.NextWrite short notes on any two of the following: (a) AWT (b) Exception Handling (c) DHTML and HTML (d) API
Official AppAndroid App
PYQDeck for Android
Solve BEU previous-year exam papers anywhere, anytime. Fast search, offline paper viewing, and step-by-step solutions designed specifically for your phone.