0% found this document useful (0 votes)
5 views15 pages

Lab On JAVA Programming II-programs

The document outlines a series of Java programming practicals for a computer science course, covering various topics such as displaying messages in different fonts, drawing geometric shapes, handling window events, and demonstrating keyboard events. Each practical includes sample code and a brief description of its functionality. The final practical focuses on creating a menu interface component in a Java Swing application.

Uploaded by

laita nikam
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)
5 views15 pages

Lab On JAVA Programming II-programs

The document outlines a series of Java programming practicals for a computer science course, covering various topics such as displaying messages in different fonts, drawing geometric shapes, handling window events, and demonstrating keyboard events. Each practical includes sample code and a brief description of its functionality. The final practical focuses on creating a menu interface component in a Java Swing application.

Uploaded by

laita nikam
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/ 15

S.S. and S.

S’s
Vidhyadhan Commerce college Wadel Road Dhule
Department of Computer Science
Class :- TYBSC SEM-II Subject: DSC (UG-CS-509 B): Lab on JAVA Programming II
Student Name :-
Practical 1 Write a program in Java to display messages in various fonts in a frame .
******************************************************************************************************

import java.awt.*;
import javax.swing.*;
class MyFrame extends JFrame {
MyFrame() {
setSize(400, 400);
setTitle("Font Applications");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void paint(Graphics g)
{
super.paint(g);

Font f1 = new Font("Georgia", Font.BOLD, 14);


g.setFont(f1);
g.setColor(Color.RED);
g.drawString("good morning", 100, 100);

Font f2 = new Font("Serif", Font.ITALIC, 16);


g.setFont(f2);
g.setColor(Color.BLUE);
g.drawString("good morning", 100, 200);

Font f3 = new Font("Monospaced", Font.PLAIN, 20);


g.setFont(f3);
g.setColor(Color.black);
g.drawString("good morning", 100, 300);
}
}
public class FontApp {
public static void main(String[] args) {
new MyFrame();
}}
OUTPUT
S.S. and S.S’s
Vidhyadhan Commerce college Wadel Road Dhule
Department of Computer Science
Class :- TYBSC SEM-II Subject: DSC (UG-CS-509 B): Lab on JAVA Programming II
Student Name :-
Practical 2 Write a program in Java to draw various geometric shapes like circle, line,
rectangle
******************************************************************************************************

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class simpleframe extends JFrame
{
public simpleframe()
{
setSize(500,500);
setTitle("simple frame");
panel p=new panel();
add(p);
}
}
class panel extends JPanel
{
public void paintComponent(Graphics g)
{
g.drawOval(120,150,30,30);
g.drawRect(150,100,190,130);
g.drawRoundRect(130,120,160,140,100,90);
g.drawLine(180,120,180,180);
}
}
class Jpract2
{
public static void main(String args[])
{
simpleframe f=new simpleframe();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}

OUTPUT
S.S. and S.S’s
Vidhyadhan Commerce college Wadel Road Dhule
Department of Computer Science
Class :- TYBSC SEM-II Subject: DSC (UG-CS-509 B): Lab on JAVA Programming II
Student Name :-
Practical 3 Write a program in Java to demonstrate paintmode.
******************************************************************************************************

import java.awt.*;
import java.awt.event.*;
public class Jpract3 {
enum PaintMode {
DRAW, ERASE
}
static PaintMode currentMode = PaintMode.DRAW;
public static void main(String[] args) {
// Create a Frame
Frame frame = new Frame("Paint Mode Example");
Canvas canvas = new Canvas() {
@Override
public void paint(Graphics g) {
super.paint(g);

if (currentMode == PaintMode.DRAW) {
// Draw a circle if in DRAW mode
g.setColor(Color.RED);
g.fillOval(100, 100, 100, 100);
} else if (currentMode == PaintMode.ERASE) {
g.setColor(Color.WHITE);
g.fillRect(100, 100, 100, 100);
}
}
};
canvas.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_D) {
currentMode = PaintMode.DRAW;
canvas.repaint();
}
if (e.getKeyCode() == KeyEvent.VK_E) {
currentMode = PaintMode.ERASE;
canvas.repaint();
}
}
});
frame.setLayout(new BorderLayout());
frame.add(canvas, BorderLayout.CENTER);
frame.setSize(400, 400);

frame.setVisible(true);
canvas.setFocusable(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
}
OUTPUT
S.S. and S.S’s
Vidhyadhan Commerce college Wadel Road Dhule
Department of Computer Science
Class :- TYBSC SEM-II Subject: DSC (UG-CS-509 B): Lab on JAVA Programming II
Student Name :-
Practical 4 - Write a program in Java to demonstrate window events.
********************************************************************************
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class Jpract4 {
public static void main(String[] args) {
JFrame frame = new JFrame("All Window Events Example");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
System.out.println("Window Opened");
}
@Override
public void windowClosing(WindowEvent e) {
int choice = JOptionPane.showConfirmDialog(frame, "Do you want
to close this window?", "Confirm Exit", JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) {
frame.dispose();
}
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("Window Closed");
}
@Override
public void windowIconified(WindowEvent e) {
System.out.println("Window Minimized");
}
@Override
public void windowDeiconified(WindowEvent e) {
System.out.println("Window Restored");
}
@Override
public void windowActivated(WindowEvent e) {
System.out.println("Window Activated");
}
@Override
public void windowDeactivated(WindowEvent e) {
System.out.println("Window Deactivated");
}
});
frame.setVisible(true);
}
}
OUTPUT
Window Activated
Window Opened
Window Minimized
Window Deactivated
Window Restored
Window Activated
Window Deactivated
Window Closed
S.S. and S.S’s
Vidhyadhan Commerce college Wadel Road Dhule
Department of Computer Science
Class :- TYBSC SEM-II Subject: DSC (UG-CS-509 B): Lab on JAVA Programming II
Student Name :-
Practical 5 - Write a program in Java to demonstrate Keyboard events.(key
pressed, key released)
******************************************************************************************************

import java.awt.*;
import java.awt.event.*;
public class Jpract5 extends Frame implements KeyListener {
Label l;
TextArea area;
Jpract5() {
l = new Label();
l.setBounds (20, 50, 100, 20);
area = new TextArea();
area.setBounds (20, 80, 300, 300);
area.addKeyListener(this);
add(l);
add(area);
setSize (400, 400);
setLayout (null);
setVisible (true);
}
public void keyPressed (KeyEvent e) {
l.setText ("Key Pressed");
}
public void keyReleased (KeyEvent e) {
l.setText ("Key Released");
}
public void keyTyped (KeyEvent e) {
l.setText ("Key Typed");
}
public static void main(String[] args) {
new Jpract5();
}
}

OUTPUT
S.S. ands .S’s
Vidhyadhan Commerce college Wadel Road Dhule
Department of Computer Science
Class :- TYBSC SEM-II Subject: DSC (UG-CS-509 B): Lab on JAVA Programming II
Student Name :-
Practical 6 - Write a program in Java to demonstrate multicasting.
******************************************************************************************************

import java.io.IOException;
import java.net.InetAddress;
import java.net.MulticastSocket;
public class Jpract6
{
public static void main(String[] args) throws IOException
{
try
{
int MULTICAST_LISTENING_PORT = 0;
MulticastSocket multicastSocket = new
MulticastSocket(MULTICAST_LISTENING_PORT );
String MULTICAST_ADDRESS = null;
InetAddress multicastGroup = InetAddress.getByName(
MULTICAST_ADDRESS );
System.out.println("Group joined using joinGroup method....");
multicastSocket.joinGroup( multicastGroup );
}
catch ( IOException ioException )
{
}
}
}

OUTPUT
Group joined using joinGroup method....
S.S. and S.S’s
Vidhyadhan Commerce college Wadel Road Dhule
Department of Computer Science
Class :- TYBSC SEM-II Subject: DSC (UG-CS-509 B): Lab on JAVA Programming II
Student Name : -
Practical 7 - Write a program in Java to demonstrate user interface component
list boxes and combo box
******************************************************************************************************

import javax.swing.*;
public class Jpract7 {
JFrame f;
Jpract7(){
f=new JFrame("ComboBox Example");
String country[]={"India","Aus","U.S.A","England","Newzealand"};
JComboBox cb=new JComboBox(country);
cb.setBounds(50, 50,90,20);
f.add(cb);
f.setLayout(null);
f.setSize(400,500);
f.setVisible(true);
}
public static void main(String[] args) {
new Jpract7();
}
}
OUTPUT
S.S. and S.S’s
Vidhyadhan Commerce college Wadel Road Dhule
Department of Computer Science
Class :- TYBSC SEM-II Subject: DSC (UG-CS-509 B): Lab on JAVA Programming II
Student Name : -
Practical 8 – Write a program in Java to demonstrate user interface component
radio button and check box.
********************************************************************************
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Jpract8 extends JFrame {

private JRadioButton radioButton1, radioButton2, radioButton3;


private JCheckBox checkBox1, checkBox2, checkBox3;
private JButton submitButton;
private JTextArea resultArea;

public Jpract8() {
// Set up the frame
setTitle("Radio Button & Check Box Demo");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());

// Create radio buttons and add to a button group (to make them
mutually exclusive)
ButtonGroup radioGroup = new ButtonGroup();
radioButton1 = new JRadioButton("Option 1");
radioButton2 = new JRadioButton("Option 2");
radioButton3 = new JRadioButton("Option 3");
radioGroup.add(radioButton1);
radioGroup.add(radioButton2);
radioGroup.add(radioButton3);

// Create check boxes


checkBox1 = new JCheckBox("CheckBox 1");
checkBox2 = new JCheckBox("CheckBox 2");
checkBox3 = new JCheckBox("CheckBox 3");

// Create a button
submitButton = new JButton("Submit");

// Create a text area to display the results


resultArea = new JTextArea(5, 30);
resultArea.setEditable(false);

// Add components to the frame


add(radioButton1);
add(radioButton2);
add(radioButton3);
add(checkBox1);
add(checkBox2);
add(checkBox3);
add(submitButton);
add(new JScrollPane(resultArea));

// Action listener for submit button


submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Get selected radio button
String radioSelection = "";
if (radioButton1.isSelected()) {
radioSelection = "Option 1";
} else if (radioButton2.isSelected()) {
radioSelection = "Option 2";
} else if (radioButton3.isSelected()) {
radioSelection = "Option 3";
} else {
radioSelection = "No option selected";
}

// Get selected checkboxes


StringBuilder checkBoxSelection = new StringBuilder();
if (checkBox1.isSelected())
checkBoxSelection.append("CheckBox 1 ");
if (checkBox2.isSelected())
checkBoxSelection.append("CheckBox 2 ");
if (checkBox3.isSelected())
checkBoxSelection.append("CheckBox 3 ");

// Display the results


resultArea.setText("Radio Selection: " + radioSelection +
"\n");
resultArea.append("CheckBox Selections: " +
(checkBoxSelection.length() > 0 ? checkBoxSelection.toString() : "No
checkboxes selected"));
}
});
}

public static void main(String[] args) {


// Create and display the UI
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Jpract8().setVisible(true);
}
});
}
}

OUTPUT
S.S. and S.S’s
Vidhyadhan Commerce college Wadel Road Dhule
Department of Computer Science
Class :- TYBSC SEM-II Subject: DSC (UG-CS-509 B): Lab on JAVA Programming II
Student Name : -
Practical 9 – Write a program in Java to demonstrate menus as interface
component.
********************************************************************************
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Jpract9 extends JFrame {

public Jpract9() {
// Set the title and default close operation
setTitle("Menu Demo");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Create a menu bar


JMenuBar menuBar = new JMenuBar();

// Create menus
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenu helpMenu = new JMenu("Help");

// Create menu items for "File" menu


JMenuItem newItem = new JMenuItem("New");
JMenuItem openItem = new JMenuItem("Open");
JMenuItem saveItem = new JMenuItem("Save");
JMenuItem exitItem = new JMenuItem("Exit");

// Add action listeners to menu items


newItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(Jpract9.this, "New file
created!");
}
});

openItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(Jpract9.this, "Open file
dialog");
}
});

saveItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(Jpract9.this, "File saved!");
}
});

exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0); // Exit the program
}
});

// Add items to "File" menu


fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.addSeparator(); // Adds a separator line
fileMenu.add(exitItem);

// Create menu items for "Edit" menu


JMenuItem copyItem = new JMenuItem("Copy");
JMenuItem pasteItem = new JMenuItem("Paste");

copyItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(Jpract9.this, "Copy operation");
}
});

pasteItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(Jpract9.this, "Paste operation");
}
});

// Add items to "Edit" menu


editMenu.add(copyItem);
editMenu.add(pasteItem);

// Create menu items for "Help" menu


JMenuItem aboutItem = new JMenuItem("About");
aboutItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(Jpract9.this, "This is a simple
menu demo.");
}
});

// Add items to "Help" menu


helpMenu.add(aboutItem);

// Add menus to the menu bar


menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);

// Set the menu bar to the frame


setJMenuBar(menuBar);
}

public static void main(String[] args) {


// Create and display the menu demo
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Jpract9().setVisible(true);
}
});
}
}
S.S. and S.S’s
Vidhyadhan Commerce college Wadel Road Dhule
Department of Computer Science
Class :- TYBSC SEM-II Subject: DSC (UG-CS-509 B): Lab on JAVA Programming II
Student Name : -
Practical 10 – Write an Applet to display humanface.
********************************************************************************
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;

// This applet will display a simple human face.


public class HumanFaceApplet extends Applet {
// This method is called to draw the face
public void paint(Graphics g) {
// Set the background color
setBackground(Color.white);
// Draw the face (circle for the head)
g.setColor(Color.yellow);
g.fillOval(50, 50, 200, 200); // head
// Draw the eyes (two small white circles)
g.setColor(Color.white);
g.fillOval(100, 100, 30, 30); // left eye
g.fillOval(170, 100, 30, 30); // right eye
// Draw the eye pupils (black circles)
g.setColor(Color.black);
g.fillOval(110, 110, 10, 10); // left pupil
g.fillOval(180, 110, 10, 10); // right pupil
// Draw the nose (a simple line)
g.setColor(Color.orange);
g.drawLine(150, 130, 150, 160); // nose
// Draw the mouth (an arc)
g.setColor(Color.red);
g.drawArc(120, 160, 60, 40, 0, -180); // mouth
// Draw the hair (a simple arc)
g.setColor(Color.brown);
g.fillArc(50, 40, 200, 120, 0, 180); // hair
}
}

You might also like