Assignment 3
Assignment 3
Assignment 3
Assignment No: 3
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.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);
}
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 MoveCircle() {
setTitle("Shivam Teli 22203A0008");
setSize(400, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
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 Counter() {
setTitle("Shivam Teli 22203A0008");
setSize(300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
timer.start();
}
});
stopButton.addActionListener(new ActionListener() {
Output:
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.BorderLayout;
JCheckBox orangeCheckBox;
JCheckBox blueCheckBox;
JCheckBox yellowCheckBox;
public Q4() {
orangeCheckBox = new JCheckBox("Red");
blueCheckBox = new JCheckBox("Blue");
yellowCheckBox = new JCheckBox("Green");
orangeCheckBox.addItemListener(checkboxListener);
blueCheckBox.addItemListener(checkboxListener);
yellowCheckBox.addItemListener(checkboxListener);
add(checkBoxPanel, BorderLayout.NORTH);
void updateBackgroundColor() {
int red = orangeCheckBox.isSelected() ? 255 : 0;
int green = yellowCheckBox.isSelected() ? 255 : 0;
int blue = blueCheckBox.isSelected() ? 255 : 0;
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);
add(panel, BorderLayout.CENTER);
}
textField.addFocusListener(new FocusListener() {
return textField;
}
Output:
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);
}
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 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;
}
}
addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
if (dragging) {
rectX = e.getX() - mouseX;
rectY = e.getY() - mouseY;
repaint();
}
}
});
}