Java Lab Report
Java Lab Report
COLLEGE
KUMARIPATI , LALITPUR
Lab Report on
…………………………….
Checked by external
…………………………….
1
TABLE CONTENT
1. LAB 1
Write a program to draw line , rectangle and ellipse using graphics 2D
in swing…………………………………………………..
Write a program to render string " Mega college" in panel use font
style and color on it………………………………………………..
Write a program to draw a rectangle and ellipse and fill different
color on it………………………………………………………….
Write a program to display all font family of graphic Environment
……………………….
Write a program to draw line , rectangle , circle and ellipse using
graphics in swing . ……………………………………………….
.
2. LAB
Write a swing program to read two input from user input dialog and
sum those values and display it using message dialog…………….
Write a swing program to create Menus. …………………………..
Write a swing program to get two number input using two text field
and multiply it by clicking button and display it in third text
field………………………………………………………………..
Write a swing program to connect database and display student
details in Jtable ………………………………………………….
Write a database program to implementing RowSet ……………
Write a swing program to implement multiple mouse event.
…………………………………………………………………….
Write a swing program to implement layout manager
………………………………………………………………….
Write a swing prgram to check login validation by using database.
Create distributed system Using RMI. ……………………………
2
3
LAB 1
Question1. Write a program to draw line , rectangle and ellipse using graphics 2D In swing .
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
public Shape() {
getContentPane().setBackground(Color.WHITE);
setSize(480, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
void drawShapes(Graphics g) {
super.paint(g);
drawShapes(g);
4
SwingUtilities.invokeLater(new Runnable() {
@Override
new Shape().setVisible(true);
} });
}}
Question 2 Write a program to render string "KCT college" in panel use font style
Code
import javax.swing.*;
import java.awt.*;
frame.setSize(300, 300);
frame.getContentPane().setBackground(Color.white);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(obj); frame.setVisible(true);}
@Override
g.setColor(Color.black);
g.setFont(myFont);
g.setColor(Color.blue);
5
g.drawString("KCT COLLEGE", 20, 90);}}
Question 3 Write a program to draw a rectangle and ellipse and fill different color
on it.
Code
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
public Shape() {
getContentPane().setBackground(Color.WHITE);
setSize(480, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
void drawShapes(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
6
g2d.setColor(Color.blue);
super.paint(g);
drawShapes(g);
SwingUtilities.invokeLater(new Runnable() {
@Override
new Shape().setVisible(true);
} }); }}
Output
7
Question 4 .
Code
import java.awt.GraphicsEnvironment;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
}}}
OUTPUT
8
Family names: A Charming Font Italic
5. Write a program to draw line, rectangle, circle and ellipse using graphics in
swing.
Code
import java.awt.*;
import javax.swing.JFrame;
setBackground(Color.WHITE); setForeground(Color.BLACK);
g.drawLine(20,20,80,80);
g.drawRect(100,100,100,100);
f.add(m);
9
f.setSize(400,400);
f.setVisible(true);
Output
10
LAB 2
1. Write a swing program to read two input from user using input dialog and sum
Code
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
JButton submit;
Addition(){
setTitle("User input");
setSize(700,500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
label1.setBounds(20,50,100,20);
c.add(label1);
11
jt1=new JTextField();
jt1.setBounds(130,50,100,20);
c.add(jt1);
label2.setBounds(20,100,100,20);
c.add(label2);
jt2=new JTextField();
jt2.setBounds(130,100,100,20);
c.add(jt2);
submit.setBounds(130,200,80,20);
c.add(submit);
submit.addActionListener(this);
setVisible(true);
@Override
int a = Integer.parseInt(jt1.getText());
int b = Integer.parseInt(jt2.getText());
int c = 0;
if (e.getSource().equals(submit)) {
c = a + b;
label3.setText(String.valueOf(c));
12
}
class Main {
Code.
import javax.swing.*;
import java.awt.event.*;
class Main {
JMenuBar menuBar;
JMenuItem menuItem;
13
JRadioButtonMenuItem rdmi;
JCheckBoxMenuItem cbmi;
menu.setMnemonic(KeyEvent.VK_F);
menuBar.add(menu);
new ImageIcon("images/newproject.png"));
menuItem.setMnemonic(KeyEvent.VK_P);
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext().setAccessibleDescription(
"New Project");
menu.add(menuItem);
new ImageIcon("images/newfile.png"));
menuItem.setMnemonic(KeyEvent.VK_F);
menu.add(menuItem);
menu.addSeparator();
14
cbmi = new JCheckBoxMenuItem("A check box menu item");
cbmi.setMnemonic(KeyEvent.VK_C);
menu.add(cbmi);
cbmi.setMnemonic(KeyEvent.VK_H);
menu.add(cbmi);
menu.setMnemonic(KeyEvent.VK_E);
menu.getAccessibleContext().setAccessibleDescription(
"Edit Menu");
menuBar.add(menu);
return menuBar;
frame.setJMenuBar(createMenuBar());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 300);
frame.setVisible(true);
Output
15
3. Write a swing program to get two number input using two text field and
Code
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
JButton submit;
Mult(){
setTitle("User input");
setSize(700,500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
16
c.setLayout(null);
label1.setBounds(20,50,100,20);
c.add(label1);
jt1=new JTextField();
jt1.setBounds(130,50,100,20);
c.add(jt1);
label2.setBounds(20,100,100,20);
c.add(label2);
jt2=new JTextField();
jt2.setBounds(130,100,100,20);
c.add(jt2);
c.add(label3);
submit.setBounds(150,200,90,30);
c.add(submit);
submit.addActionListener(this);
setVisible(true);
@Override
if(jt1.getText().isEmpty()){
17
JOptionPane.showMessageDialog(new JFrame(), "please enter first number");
return;
if(jt2.getText().isEmpty()){
return;
int a = Integer.parseInt(jt1.getText());
int b = Integer.parseInt(jt2.getText());
int c = 0;
if (e.getSource().equals(submit)) {
c = a * b;
class Main {
Output
18
4. Write a swing program to connect database and display student details in JTable.
Code
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
try{
Class.forName("com.mysql.jdbc.Driver");
"jdbc:mysql://localhost:3306/tu","root","");
System.out.println("Database connected");
Statement stmt=con.createStatement();
while(rs.next())
19
"+rs.getString(4)+" "+rs.getString(5));
con.close();
OUTPUT
Code.
import java.sql.Connection;
import java.sql.DriverManager;
20
import java.sql.ResultSet;
import java.sql.Statement;
try{
Class.forName("com.mysql.jdbc.Driver");
"jdbc:mysql://localhost:3306/tu","root","");
System.out.println("Database connected");
Statement stmt=con.createStatement();
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+"
con.close();
21
Output
22
Code
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class Main {
public Main(){
prepareGUI();
swingListenerDemo.showMouseListenerDemo();
mainFrame.setSize(400,400);
23
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
mainFrame.addWindowListener(new WindowAdapter() {
System.exit(0);
});
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
panel.setBackground(Color.magenta);
panel.setLayout(new FlowLayout());
panel.addMouseListener(new CustomMouseListener());
JLabel msglabel =
panel.add(msglabel);
24
msglabel.addMouseListener(new CustomMouseListener());
panel.add(msglabel);
controlPanel.add(panel);
mainFrame.setVisible(true);
25
8. Write a swing program to implement layout manager.
Border layout
Code
import java.awt.*;
import javax.swing.*;
class Border
JFrame f;
Border()
f = new JFrame();
// creating buttons
26
JButton b5 = new JButton("CENTER");; // the button will be labeled as CENTER
f.setSize(300, 300);
f.setVisible(true);
new Border();
Output
Code.
Main.java file
27
package com.logintut;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
JTextField t1,t2,t3,t4;
Myframe(){
setTitle("Register");
setSize(700,500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
28
Container c = getContentPane();
c.setLayout(null);
labelName.setBounds(20,50,100,20);
c.add(labelName);
t1=new JTextField();
t1.setBounds(130,50,100,20);
c.add(t1);
labelEmail.setBounds(20,100,100,20);
c.add(labelEmail);
t2=new JTextField();
t2.setBounds(130,100,100,20);
c.add(t2);
labelFaculty.setBounds(20,150,100,20);
c.add(labelFaculty);
t3=new JTextField();
t3.setBounds(130,150,100,20);
c.add(t3);
labelPassword.setBounds(20,200,100,20);
c.add(labelPassword);
29
t4=new JTextField();
t4.setBounds(130,200,100,20);
c.add(t4);
submit.setBounds(150,350,80,20);
c.add(submit);
Login.setBounds(250,350,80,20);
c.add(Login);
submit.addActionListener(this);
Login.addActionListener(new ActionListener() {
@Override
});
setVisible(true);
@Override
30
String faculty = t3.getText();
if(name.isEmpty()|| name.isBlank()) {
return;
if(email.isEmpty() || email.isBlank()){
return;
}else if(!check_email){
format");
return;
if(faculty.isBlank() || faculty.isEmpty()){
return;
if(password.isEmpty() || password.isBlank()){
password");
return;
31
}
try{
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(dbURL,username,dbpassword);
if(conn !=null){
System.out.println("successfully connected");
"(name,email,faculty,password)
VALUES('"+name+"','"+email+"','"+faculty+"','"+password+"')";
insertTable.executeUpdate(InsertQ);
System.out.println("Inserted Successfully");
}catch(ClassNotFoundException ex){
ex.printStackTrace();
ex.printStackTrace();
32
Myframe myframe = new Myframe();
SignIn.java file
package com.logintut;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
JTextField t1,t2;
JButton submit;
public SignIn(){
setTitle("Login");
setSize(700,500);
33
setLocationRelativeTo(null);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
labelEmail.setBounds(20,50,100,20);
c.add(labelEmail);
t1=new JTextField();
t1.setBounds(130,50,100,20);
c.add(t1);
labelPassword.setBounds(20,100,100,20);
c.add(labelPassword);
t2=new JTextField();
t2.setBounds(130,100,100,20);
c.add(t2);
submit.setBounds(150,200,80,20);
c.add(submit);
setVisible(true);
submit.addActionListener(new ActionListener() {
@Override
34
if(email.isEmpty() || email.isBlank()){
email");
return;
if(password.isEmpty() || password.isBlank()){
password");
return;
try{
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(dbURL,username,dbpassword);
if(conn !=null){
System.out.println("successfully connected");
Statement st = conn.createStatement();
'"+t1.getText()+"'";
ResultSet rs = st.executeQuery(select);
while (rs.next()){
get_password = rs.getString(1);
35
}
if(get_password.equals(t2.getText())){
}else{
incorrect");
}catch(ClassNotFoundException ex){
ex.printStackTrace();
ex.printStackTrace();
});
Output
36
10. create distributed system using RMI.
Hello.java
import java.rmi.Remote;
import java.rmi.RemoteException;
ImplExample.java
ImplExample(){super();}
37
System.out.println("Sum = " + (x+y));
ServerRMI.java
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public Server() {}
try {
// Exporting the object of implementation class (here we are exporting the remote object
to the stub)
registry.bind("RMITest", skeleton);
System.err.println("Server ready");
} catch (Exception e) {
e.printStackTrace();
38
}
ClientRMI.java
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
private ClientRMI() {}
try {
// A Java RMI registry is a simplified name service that allows clients to get a reference
(a stub) to a
remote object. In general, a registry is used (if at all) only to locate the first remote object
a client needs to
use.
stub.Add(5,10);
39
} catch (Exception e) {
e.printStackTrace();
Output
40