ADVANCED
PROGRAMMING
PRACTICE
ASSIGNMENT-9
RA2211026010486
G.Abiram cse-Aiml
Programs:
GRAPHICAL USER INTERFACE BASED PROGRAMMING
PARADIGM
1.Write a java program using swing by inheritance
Ans:
import javax.swing.*;
public class SwingInheritanceExample {
public static void main(String[] args) {
MyFrame frame = new MyFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLO
SE);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
class MyFrame extends JFrame {
MyFrame() {
setTitle("Swing Inheritance Example");
JLabel label = new JLabel("Hello, Swing!");
add(label);
}
}
2.Write a java program using swing with ActionListener
Ans:
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SwingActionListenerExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Swing ActionListener
Example");
JButton button = new JButton("Click Me");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,
"Button Clicked!");
}
});
frame.add(button);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLO
SE);
frame.setVisible(true);
}
}
3.Using Java JMenuItem and JMenu implement
application swing.
Ans:
import javax.swing.*;
public class SwingMenuItemExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Swing Menu
Example");
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem openItem = new JMenuItem("Open");
JMenuItem saveItem = new JMenuItem("Save");
openItem.addActionListener(e ->
JOptionPane.showMessageDialog(null, "Open
clicked"));
saveItem.addActionListener(e ->
JOptionPane.showMessageDialog(null, "Save
clicked"));
fileMenu.add(openItem);
fileMenu.add(saveItem);
menuBar.add(fileMenu);
frame.setJMenuBar(menuBar);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLO
SE);
frame.setVisible(true);
}
}
4.Develop a student registration form using SWING
components.
Ans:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class StudentRegistrationForm {
public static void main(String[] args) {
JFrame frame = new JFrame("Student Registration
Form");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLO
SE);
frame.setSize(400, 300);
JPanel panel = new JPanel(new GridLayout(4, 2));
JLabel nameLabel = new JLabel("Name:");
JTextField nameField = new JTextField(20);
JLabel rollNumberLabel = new JLabel("Roll
Number:");
JTextField rollNumberField = new JTextField(10);
JButton submitButton = new JButton("Submit");
submitButton.addActionListener(new
ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String rollNumber =
rollNumberField.getText();
JOptionPane.showMessageDialog(frame,
"Name: " + name + "\nRoll Number: " + rollNumber);
}
});
panel.add(nameLabel);
panel.add(nameField);
panel.add(rollNumberLabel);
panel.add(rollNumberField);
panel.add(submitButton);
frame.add(panel);
frame.setVisible(true);
}
}
5.Implement Employment registration form using
SWING components.
Ans:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class EmploymentRegistrationForm {
public static void main(String[] args) {
JFrame frame = new JFrame("Employment
Registration Form");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLO
SE);
frame.setSize(400, 300);
JPanel panel = new JPanel(new GridLayout(5, 2));
JLabel nameLabel = new JLabel("Name:");
JTextField nameField = new JTextField(20);
JLabel ageLabel = new JLabel("Age:");
JTextField ageField = new JTextField(3);
JLabel positionLabel = new JLabel("Position:");
JTextField positionField = new JTextField(15);
JButton submitButton = new JButton("Submit");
submitButton.addActionListener(new
ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String age = ageField.getText();
String position = positionField.getText();
JOptionPane.showMessageDialog(frame,
"Name: " + name + "\nAge: " + age + "\nPosition: " +
position);
}
});
panel.add(nameLabel);
panel.add(nameField);
panel.add(ageLabel);
panel.add(ageField);
panel.add(positionLabel);
panel.add(positionField);
panel.add(submitButton);
frame.add(panel);
frame.setVisible(true);
}
}
6.Write a java program to draw Oval, Rectangle,Line
and fill the color in it.and display it
on Applet.
Ans:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class DrawShapesApplet extends Applet {
public void paint(Graphics g) {
g.setColor(Color.red);
g.drawRect(10, 10, 100, 50); // Draw a rectangle
g.setColor(Color.blue);
g.drawOval(150, 10, 80, 80); // Draw an oval
g.setColor(Color.green);
g.drawLine(300, 10, 400, 80); // Draw a line
g.setColor(Color.orange);
g.fillRect(10, 100, 100, 50); // Fill a rectangle
}
}
7.Draw a chessboard in java applet
Ans:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class ChessboardApplet extends Applet {
public void paint(Graphics g) {
int row, col;
int x, y;
int squareSize = 50; // Size of each square
for (row = 0; row < 8; row++) {
for (col = 0; col < 8; col++) {
x = col * squareSize;
y = row * squareSize;
if ((row + col) % 2 == 0) {
g.setColor(Color.white);
} else {
g.setColor(Color.black);
}
g.fillRect(x, y, squareSize, squareSize);
}
}
}
}
8.Write a java program that handles all mouse events
and shows the event name at the
center of the window when mouse event is fired (Use
Adapter classes and applet).
Ans:
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MouseEventHandlingApplet extends
Applet {
private String eventName;
public void init() {
MyMouseListener listener = new
MyMouseListener();
addMouseListener(listener);
addMouseMotionListener(listener);
}
public void paint(Graphics g) {
if (eventName != null) {
g.drawString("Mouse Event: " + eventName,
getWidth() / 2, getHeight() / 2);
}
}
class MyMouseListener extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
eventName = "MouseClicked";
repaint();
}
public void mousePressed(MouseEvent e) {
eventName = "MousePressed";
repaint();
}
public void mouseReleased(MouseEvent e) {
eventName = "MouseReleased";
repaint();
}
public void mouseEntered(MouseEvent e) {
eventName = "MouseEntered";
repaint();
}
public void mouseExited(MouseEvent e) {
eventName = "MouseExited";
repaint();
}
public void mouseDragged(MouseEvent e) {
eventName = "MouseDragged";
repaint();
}
public void mouseMoved(MouseEvent e) {
eventName = "MouseMoved";
repaint();
}
}
}
9.Implement java MVC pattern application with
Student object Model, StudentView and
StudentController.
Ans:
1. Student Model (StudentModel.java):
public class StudentModel {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
2. Student View (StudentView.java):
public class StudentView {
public void printStudentDetails(String name, int age) {
System.out.println("Student Details:");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
3. Student Controller (StudentController.java):
public class StudentController {
private StudentModel model;
private StudentView view;
public StudentController(StudentModel model,
StudentView view) {
this.model = model;
this.view = view;
}
public void setStudentName(String name) {
model.setName(name);
}
public void setStudentAge(int age) {
model.setAge(age);
}
public String getStudentName() {
return model.getName();
}
public int getStudentAge() {
return model.getAge();
}
public void updateView() {
view.printStudentDetails(model.getName(),
model.getAge());
}
}
4. Main Application (MainApp.java):
public class MainApp {
public static void main(String[] args) {
// Create the Model, View, and Controller
StudentModel model = new StudentModel();
StudentView view = new StudentView();
StudentController controller = new
StudentController(model, view);
// Set student details
controller.setStudentName("John");
controller.setStudentAge(20);
// Update and display student details
controller.updateView();
}
}
10.Implement java MVC to display Employee details.
Ans:
public class MVCEmployeeDetails {
public static void main(String[] args) {
// Create the Model, View, and Controller
EmployeeModel model = new EmployeeModel();
EmployeeView view = new EmployeeView();
EmployeeController controller = new
EmployeeController(model, view);
// Set employee details
controller.setEmployeeName("John Doe");
controller.setEmployeeId(1001);
controller.setEmployeeDepartment("HR");
// Update and display employee details
controller.updateView();
}
}
class EmployeeModel {
private String name;
private int employeeId;
private String department;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
}
class EmployeeView {
public void printEmployeeDetails(String name, int
employeeId, String department) {
System.out.println("Employee Details:");
System.out.println("Name: " + name);
System.out.println("Employee ID: " + employeeId);
System.out.println("Department: " + department);
}
}
class EmployeeController {
private EmployeeModel model;
private EmployeeView view;
public EmployeeController(EmployeeModel model,
EmployeeView view) {
this.model = model;
this.view = view;
}
public void setEmployeeName(String name) {
model.setName(name);
}
public void setEmployeeId(int employeeId) {
model.setEmployeeId(employeeId);
}
public void setEmployeeDepartment(String department)
{
model.setDepartment(department);
}
public void updateView() {
view.printEmployeeDetails(model.getName(),
model.getEmployeeId(), model.getDepartment());
}
}