Assignment 3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

DEPARTMENT OF COMPUTER ENGINEERING

Subject: Advance Java Programming Subject Code:22517


Semester:5 th Semester Course: Computer Engineering
Laboratory No: Name of Subject Teacher: Prof. Prasad
Koyande
Name of Student: Shivam Teli Roll Id: 22203A0008

Assignment No: 3

1. Create a Java application where clicking on a specific part of the screen


changes the background color of a JPanel. Use MouseListener to capture
mouse clicks.

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.BorderLayout;
public class Changer extends JFrame {

JPanel panel;

public Changer() {
setTitle("Shivam Teli 22203A0008");
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());

panel = new JPanel();


panel.setBackground(Color.WHITE);

panel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
changeColor();
}
});

add(panel, BorderLayout.CENTER);
}

void changeColor() {
Color newColor = new Color((float) Math.random(), (float)
Math.random(), (float) Math.random());
panel.setBackground(newColor);
}

public static void main(String[] args) {


Changer app = new Changer();
app.setVisible(true);
}
}

Output:
2. Develop a Java program that listens for arrow key presses (KeyListener)
and moves a circle up, down, left, or right in a JPanel.

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class MoveCircle extends JFrame {


JPanel panel;
int x = 100;
int y = 100;

public MoveCircle() {
setTitle("Shivam Teli 22203A0008");
setSize(400, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);

panel = new JPanel() {


@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.ORANGE);
g.fillOval(x, y, 30, 30);
}
};

panel.setFocusable(true);
panel.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) y -= 5;
if (e.getKeyCode() == KeyEvent.VK_DOWN) y += 5;
if (e.getKeyCode() == KeyEvent.VK_LEFT) x -= 5;
if (e.getKeyCode() == KeyEvent.VK_RIGHT) x += 5;
panel.repaint();
}
});

add(panel);
}
public static void main(String[] args) {
MoveCircle moveCircle = new MoveCircle();
moveCircle.setVisible(true);
}
}

Output:
3. Write a Java program with two buttons: "Start" and "Stop". Clicking
"Start" begins a timer that updates a counter on the screen every second,
and clicking "Stop" pauses the timer.

Code:
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Counter extends JFrame {


JButton startButton;
JButton stopButton;
JLabel counterLabel;
Timer timer;
int counter = 0;

public Counter() {
setTitle("Shivam Teli 22203A0008");
setSize(300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);

counterLabel = new JLabel("Counter: 0", SwingConstants.CENTER);


add(counterLabel, "North");

startButton = new JButton("Start");


stopButton = new JButton("Stop");

JPanel buttonPanel = new JPanel();


buttonPanel.add(startButton);
buttonPanel.add(stopButton);
add(buttonPanel, "South");

timer = new Timer(1000, new ActionListener() {


@Override
public void actionPerformed(ActionEvent e) {
counter++;
counterLabel.setText("Counter: " + counter);
}
});

startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
timer.start();
}
});

stopButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {


timer.stop();
}
});
}

public static void main(String[] args) {


Counter app = new Counter();
app.setVisible(true);
}
}

Output:

4. Create a Java form that contains three checkboxes: "Red", "Green",


and "Blue". Use ItemListener to change the background color of the form
based on the selected checkboxes.

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.BorderLayout;

public class Q4 extends JFrame {

JCheckBox orangeCheckBox;
JCheckBox blueCheckBox;
JCheckBox yellowCheckBox;

public Q4() {
orangeCheckBox = new JCheckBox("Red");
blueCheckBox = new JCheckBox("Blue");
yellowCheckBox = new JCheckBox("Green");

ItemListener checkboxListener = new ItemListener() {


public void itemStateChanged(ItemEvent e) {
updateBackgroundColor();
}
};

orangeCheckBox.addItemListener(checkboxListener);
blueCheckBox.addItemListener(checkboxListener);
yellowCheckBox.addItemListener(checkboxListener);

JPanel checkBoxPanel = new JPanel();


checkBoxPanel.add(orangeCheckBox);
checkBoxPanel.add(blueCheckBox);
checkBoxPanel.add(yellowCheckBox);

add(checkBoxPanel, BorderLayout.NORTH);

setTitle("Shivam Teli 222203A0008");


setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}

void updateBackgroundColor() {
int red = orangeCheckBox.isSelected() ? 255 : 0;
int green = yellowCheckBox.isSelected() ? 255 : 0;
int blue = blueCheckBox.isSelected() ? 255 : 0;

Color newColor = new Color(red, green, blue);


getContentPane().setBackground(newColor);
}

public static void main(String[] args) {


SwingUtilities.invokeLater(() -> {
new Q4().setVisible(true);
});
}
}
Output:

5. Create a Java form that contains three checkboxes: "Red", "Green",


and "Blue". Use ItemListener to change the background color of the form
based on the selected checkboxes.

Code:

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;
import java.awt.BorderLayout;
public class Focus extends JFrame {
JTextField nameField;
JTextField departmentField;
JTextField rollNoField;
Border defaultBorder;
Border focusedBorder;

public Focus() {
setTitle("Shivam Teli 22203A0008");
setSize(400, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());

defaultBorder = BorderFactory.createLineBorder(Color.GRAY);
focusedBorder = BorderFactory.createLineBorder(Color.ORANGE, 5);

JPanel panel = new JPanel();


panel.setLayout(new GridLayout(3, 2));

JLabel nameLabel = new JLabel("Name:");


nameField = createTextField();
panel.add(nameLabel);
panel.add(nameField);

JLabel departmentLabel = new JLabel("Department:");


departmentField = createTextField();
panel.add(departmentLabel);
panel.add(departmentField);

JLabel rollNoLabel = new JLabel("Roll No:");


rollNoField = createTextField();
panel.add(rollNoLabel);
panel.add(rollNoField);

add(panel, BorderLayout.CENTER);
}

private JTextField createTextField() {


JTextField textField = new JTextField();
textField.setBorder(defaultBorder);

textField.addFocusListener(new FocusListener() {

public void focusGained(FocusEvent e) {


textField.setBorder(focusedBorder);
}
public void focusLost(FocusEvent e) {
textField.setBorder(defaultBorder);
}
});

return textField;
}

public static void main(String[] args) {


Focus example = new Focus();
example.setVisible(true);
}
}

Output:

6. Create a Java application that logs messages in the console when a


window is minimized, maximized, closed, or opened. Use WindowListener
to handle these events.

Code:
import java.awt.*;
import java.awt.event.*;
public class WindowExample extends Frame implements WindowListener {

WindowExample() {
addWindowListener(this);
setSize (400, 400);
setLayout (null);
setVisible (true);
}

public static void main(String[] args) {


new WindowExample();
}

public void windowActivated (WindowEvent arg0) {


System.out.println("activated");
}

public void windowClosed (WindowEvent arg0) {


System.out.println("closed");
}

public void windowClosing (WindowEvent arg0) {


System.out.println("closing");
dispose();
}

public void windowDeactivated (WindowEvent arg0) {


System.out.println("deactivated");
}

public void windowDeiconified (WindowEvent arg0) {


System.out.println("deiconified");
}

public void windowIconified(WindowEvent arg0) {


System.out.println("iconified");
}

public void windowOpened(WindowEvent arg0) {


System.out.println("opened");
}
}
Output:

7. Write a program where the user can drag and drop a rectangle inside a
JFrame. Use MouseListener and MouseMotionListener to handle the
dragging.

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class DragRectangle extends JFrame {


int rectX = 50;
int rectY = 50;
int rectWidth = 100;
int rectHeight = 50;
boolean dragging = false;
int mouseX;
int mouseY;

public DragRectangle() {
setTitle("Shivam Teli 22203A0008");
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);

addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (e.getX() >= rectX && e.getX() <= rectX + rectWidth &&
e.getY() >= rectY && e.getY() <= rectY + rectHeight) {
dragging = true;
mouseX = e.getX() - rectX;
mouseY = e.getY() - rectY;
}
}

public void mouseReleased(MouseEvent e) {


dragging = false;
}
});

addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
if (dragging) {
rectX = e.getX() - mouseX;
rectY = e.getY() - mouseY;
repaint();
}
}
});
}

public void paint(Graphics g) {


super.paint(g);
g.setColor(Color.BLUE);
g.fillRect(rectX, rectY, rectWidth, rectHeight);
}

public static void main(String[] args) {


SwingUtilities.invokeLater(() -> {
DragRectangle dragRectangle = new DragRectangle();
dragRectangle.setVisible(true);
});
}
}
Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy