Tuesday, October 10, 2023

BCA and B.Sc Programs - Programming in Java Lab - Bharathiar University - Practical Program 11- Write a Java Program to draw a circle, square, ellipse and rectangle at the mouse click positions

 

Bharathiar University

Programming Java Lab
Java Program 11
Write a Java Program to draw a circle, square, ellipse and rectangle at the mouse click positions




Source Code

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MouseDraw extends JFrame {
    private Point clickPoint;
    private String selectedShape;

    public MouseDraw() {
        setTitle("MouseDrawer");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());           

        addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                clickPoint = e.getPoint();
                repaint();  //calls paint method
            }
        });

        String[] shapes = {"Circle", "Square", "Ellipse", "Rectangle"};
        

        JComboBox<String> shapeCB = new JComboBox<String>(shapes);
        shapeCB.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e){
            selectedShape = (String) shapeCB.getSelectedItem();
           repaint();
}
        });


      
        add(new JLabel("Select Shape: "));
        add(shapeCB);

        
    }

    public void paint(Graphics g) {
       super.paint(g);
        if (clickPoint != null) {
            int size = 50;
            if (selectedShape.equals("Circle")) {
                g.drawOval(clickPoint.x - size / 2, clickPoint.y - size / 2, size, size);
            } else if (selectedShape.equals("Square")) {
                g.drawRect(clickPoint.x - size / 2, clickPoint.y - size / 2, size, size);
            } else if (selectedShape.equals("Ellipse")) {
                g.drawOval(clickPoint.x - size, clickPoint.y - size / 2, size * 2, size);
            } else if (selectedShape.equals("Rectangle")) {
                g.drawRect(clickPoint.x - size, clickPoint.y - size / 2, size * 2, size);
            }
        }

    }

    public static void main(String[] args) {

            MouseDraw shapes = new MouseDraw();
            shapes.setVisible(true);
    }
}

OUTPUT:








Wednesday, October 4, 2023

Button click event using Java Swing


Button click event using Java Swing





Write a Java program to create a frame with label and button. When a button clicked some text to be printed in label. 


Output





Source code
;

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

public class ButtonClick extends JFrame {

    public ButtonClick() {
        setTitle("Print Some Text");
        setSize(500, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());           

        JLabel label=new JLabel("");
        JButton click=new JButton("Click Here");

       click.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e){
        label.setText("Hello everyone! Welcome");
}
        });


        add(label);
        add(click);

    }

    public static void main(String[] args) {

            ButtonClick ck  = new ButtonClick();
            ck.setVisible(true);
    }
}

    


Tuesday, September 26, 2023

BCA and B.Sc Programs - Programming in Java Lab - Bharathiar University -Program 10 - Write a Java Program to create a frame which responds to the mouse clicks. For each events with mouse such as mouse up, mouse down, etc, the corresponding message to be displayed

 

Bharathiar University

Programming Java Lab
Java Program 10
Write a Java Program to create a frame which responds to the mouse clicks. For each events with mouse such as mouse up, mouse down, etc, the corresponding message to be displayed
 



Source Code

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

public class MouseClicks extends JFrame {
    
    public MouseClicks() {
        setTitle("Mouse Events ");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
         JLabel l=new JLabel();  

        add(l);  
        
        addMouseListener(new MouseAdapter() {
           
            public void mouseClicked(MouseEvent e) {
                l.setText("Mouse Clicked");
            }
                        
          
            public void mouseEntered(MouseEvent e) {
                l.setText("Mouse Entered");
            } 
         
            public void mousePressed(MouseEvent e) {
                l.setText("Mouse Pressed");
            }
          
            public void mouseExited(MouseEvent e) {
                l.setText("Mouse Exited");
            }

      
            public void mouseReleased(MouseEvent e) {
                l.setText("Mouse Released");
            }
        });
        setVisible(true);
    }

    public static void main(String[] args) {
       
            new MouseClicks();
            
        }
    }

OUTPUT













BCA and B.Sc Programs - Programming in Java Lab - Bharathiar University - Practical Program 8 - Write a Java Program to create a frame with three text fields for name, age and qualification and a text field for multiple line for address

 

Bharathiar University

Programming Java Lab
Java Program 8
Write a Java Program to create a frame with three text fields for name, age and qualification and a text field for multiple line for address
 



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

public class UserForm {
    public static void main(String[] args) {
        // Create the main frame
        JFrame frame = new JFrame("User Profile Form");
        frame.setSize(320, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout(FlowLayout.LEFT,20,25));

        // Create labels and text fields
        JLabel nameLabel = new JLabel("Name:");
        JTextField name = new JTextField(20);

        JLabel ageLabel = new JLabel("Age:");
        JTextField age = new JTextField(20);

        JLabel qualificationLabel = new JLabel("Qualification:");
        JTextField qualification = new JTextField(20);

        JLabel addressLabel = new JLabel("Address:");
        JTextArea address = new JTextArea(5, 20);

        // Create a button to trigger the action
        JButton submit = new JButton("Submit");

        // Add components to the frame
        frame.add(nameLabel);
        frame.add(name);
        frame.add(ageLabel);
        frame.add(age);
        frame.add(qualificationLabel);
        frame.add(qualification);
        frame.add(addressLabel);
        frame.add(new JScrollPane(address));
        frame.add(submit);

        // Create ActionListener for the button
        submit.addActionListener(new ActionListener() {
            
            public void actionPerformed(ActionEvent e) {
                
                // Display all values in a dialog box
                String message = "Name: " + name.getText() + "\nAge: " + age.getText() + "\nQualification: " + qualification.getText() + "\nAddress:\n" + address.getText();
                JOptionPane.showMessageDialog(frame, message);
            }
        });

        // Set frame visibility
        frame.setVisible(true);
    }
}

OUTPUT






Dialog box

BCA and B.Sc Programs - Programming in Java Lab - Bharathiar University - Practical Program 7 - Write a Java Program to demonstrate the Multiple Selection List-box

 

Bharathiar University

Programming Java Lab
Java Program 7
Write a Java Program to demonstrate the Multiple Selection List-box 



Source Code

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

public class MultiSelectionListbox extends JFrame  {
    public JLabel label;
    public JButton submit;
    public JList list1,list2;
    public MultiSelectionListbox()
    {
        setTitle("Multiple Selection ListBox");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 500);
        setLayout(new FlowLayout(FlowLayout.LEFT));
        String values[]={"C","C++","Java","Python","R","HTML","XML","CSS","PHP"};
        label = new JLabel("Which Languages do you know?");
        list1 = new JList(values);
        list2 = new JList();
       
        list1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        
        submit = new JButton("Submit");
        submit.addActionListener(new ActionListener() {
           
            public void actionPerformed(ActionEvent e) {
                list2.setListData(list1.getSelectedValues());
            }
        });
        add(label);
        add(new JScrollPane(list1));
        add(list1);
        add(submit);
        add(new JScrollPane(list2));
        add(list2);
        setVisible(true);
    }
    public static void main(String[] args) {
       new MultiSelectionListbox();
}
}

OUTPUT






Program 12 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 12 Implement a telephone directory using files

  BCA Object Oriented Programming using C++ Practical Madras University  Program 12  Implement a telephone directory using files SOURCE CODE...