0% found this document useful (0 votes)
27 views7 pages

Assignment 6pdf

Uploaded by

mondalbharat13
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)
27 views7 pages

Assignment 6pdf

Uploaded by

mondalbharat13
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/ 7

Assignment 1: Creating a Simple Applet

import javax.swing.*;

import java.awt.*;

public class SimpleAppletSwing extends JFrame {

public SimpleAppletSwing() {

setTitle("Simple Applet Alternative");

setSize(500, 500);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

@Override

public void paint(Graphics g) {

super.paint(g);

g.drawString("Hello, Applet!", 100, 100);

public static void main(String[] args) {

SwingUtilities.invokeLater(() -> {

SimpleAppletSwing app = new SimpleAppletSwing();

app.setVisible(true);

});

}
Assignment 2: Drawing Shapes in an Applet

import javax.swing.JFrame;

import javax.swing.JPanel;

import java.awt.Graphics;

public class DrawingShapes extends JPanel {

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

// Drawing a rectangle

g.drawRect(20, 30, 100, 50); // (x, y, width, height)

// Drawing an oval inside the rectangle

g.drawOval(20, 30, 100, 50);

// Drawing a line

g.drawLine(20, 30, 120, 80);

public static void main(String[] args) {

JFrame frame = new JFrame("Shape Drawing");

DrawingShapes drawingShapes = new DrawingShapes();

frame.add(drawingShapes);

frame.setSize(300, 200); // Set the size of the window

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}}
Assignment 3: Applet with User Interaction (MouseEvent)

import javax.swing.*;

import java.awt.*;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

public class MouseEventSwing extends JFrame {

public MouseEventSwing() {

// Set the title for the JFrame

setTitle("Mouse Event Example");

// Set the initial size of the window

setSize(400, 400);

// Set the default close operation

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Add mouse listener to handle mouse clicks

addMouseListener(new MouseAdapter() {

public void mousePressed(MouseEvent e) {

// Change background color on mouse click

getContentPane().setBackground(new Color((int)(Math.random() * 0x1000000)));

});

public static void main(String[] args) {

// Create the frame on the Event Dispatch Thread

SwingUtilities.invokeLater(() -> {

MouseEventSwing frame = new MouseEventSwing();

frame.setLocationRelativeTo(null); // Center the window

frame.setVisible(true); // Make the frame visible

});

}}
Assignment 4: Applet with User Input (TextField and Button)

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class InputSwing extends JFrame {

private JTextField inputField;

private JButton submitButton;

private String userInput = "";

public InputSwing() {

// Set the title for the JFrame

setTitle("Input Example");

// Set the initial size of the window

setSize(300, 150);

// Set the default close operation

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Set layout manager

setLayout(new FlowLayout());

// Initialize components

inputField = new JTextField(20);

submitButton = new JButton("Submit");

// Add components to the frame

add(inputField);

add(submitButton);

// Set up button click event listener

submitButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {


userInput = inputField.getText(); // Get the text from the input field

repaint(); // Repaint the frame to show the updated input

});

@Override

public void paint(Graphics g) {

super.paint(g); // Call the superclass's paint method

// Display the entered text

g.drawString("You entered: " + userInput, 20, 80);

public static void main(String[] args) {

// Create the frame on the Event Dispatch Thread

SwingUtilities.invokeLater(() -> {

InputSwing frame = new InputSwing();

frame.setLocationRelativeTo(null); // Center the window

frame.setVisible(true); // Make the frame visible

});

}
Assignment 5: Applet with Multiple Components

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class MultiComponentSwing extends JFrame {

private JCheckBox checkbox1;

private JCheckBox checkbox2;

private JButton submitButton;

private JLabel resultLabel;

public MultiComponentSwing() {

// Set the title for the JFrame

setTitle("Multi-Component Example");

// Set the initial size of the window

setSize(300, 150);

// Set the default close operation

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Set layout manager

setLayout(new FlowLayout());

// Initialize components

checkbox1 = new JCheckBox("Option 1");

checkbox2 = new JCheckBox("Option 2");

submitButton = new JButton("Submit");

resultLabel = new JLabel("Select options and press Submit");

// Add components to the frame

add(checkbox1);

add(checkbox2);
add(submitButton);

add(resultLabel);

// Set up button click event listener

submitButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String result = "Selected: ";

if (checkbox1.isSelected()) result += "Option 1 ";

if (checkbox2.isSelected()) result += "Option 2";

resultLabel.setText(result); // Update the label with the selected options

});

public static void main(String[] args) {

// Create the frame on the Event Dispatch Thread

SwingUtilities.invokeLater(() -> {

MultiComponentSwing frame = new MultiComponentSwing();

frame.setLocationRelativeTo(null); // Center the window

frame.setVisible(true); // Make the frame visible

});

You might also like