Posts

Showing posts from April, 2023

Practical-11 Develop a database application that uses any JDBC driver

Image
  import java.sql.*; public class DbDemo {      public static void main(String[] args)      {           Connection conn;           Statement stmt;           try           {                Class.forName("com.mysql.cj.jdbc.Driver");                System.out.println("Trying to connect with Database Server..");             con = DriverManager.getConnection("jdbc:mysql://localhost:3306/","root", "");             System.out.println("Connection Established Successfully...");             System.out.println("Trying to create Database..");             stmt = con.createStatement();             String sql = "CREATE DATABASE STUDENTDB";             stmt.executeUpdate(sql);             System.out.println("Database Created Successfully...");             sql = "USE STUDENTDB";             stmt.executeUpdate(sql);             System.out.println("Trying to create Table..");                    

Practical-10 Develop a program that draws two sets of ever-decreasing rectangles one in outline form and one filled alternately in black and white.

Image
  import java.awt.*; public class Practical10 extends Canvas {     Frame f;     Practical10()     {         f = new Frame("Practical - 10");         f.setSize(500,300);         f.setVisible(true);         f.setLayout(new FlowLayout());         setSize(400,200);         setVisible(true);         f.add(this);     }     public static void main(String a[])     {         Practical10 p=new Practical10();     }     public void paint(Graphics g)     {         g.setColor(Color.RED);         g.fillRect(0,0,400,200);         int x=0;         int y=0;         int width=400;         int height=200;         int i=0;         while(i<19)         {             width=width-10;             height=height-10;             x=(400-width)/2;             y=(200-height)/2;             if(i%2==1)             {                 g.setColor(Color.WHITE);             }             else             {                 g.setColor(Color.BLACK);             }             g.fillRect(x,y,width,height);          

Practical-9 Create an application that displays a frame with a menu bar. When a user selects any menu or menu item, display that selection on a text area in the center of the frame

Image
  import java.awt.*; import java.awt.event.*; import javax.swing.*; public class AJPProgram extends JFrame implements ActionListener {     JMenuItem NewOne;     JMenuItem Save;     JMenuItem SaveAs;     JMenuItem Exit;     JTextArea t;     AJPProgram()     {         FlowLayout f=new FlowLayout();                  JMenuBar menubar=new JMenuBar();         JMenu menu=new JMenu("File");         t=new JTextArea(null,1,5);                  JPanel p=new JPanel();         add(p);                  t.setLayout(new FlowLayout());         p.add(t);                  NewOne=new JMenuItem("New");         Save=new JMenuItem("Save");         SaveAs=new JMenuItem("SaveAs");         Exit=new JMenuItem("Exit");                  menubar.add(menu);         menubar.add(NewOne);         menubar.add(Save);         menubar.add(SaveAs);         menubar.add(Exit);                  NewOne.addActionListener(this);         Save.addActionListener(this);         SaveAs

Practical-8 Develop an program that contains three check boxes and 30 x 30 pixel canvas.The three checkboxes should be labeled “Red”, “Green”,”Blue”. The selection of the check boxes determine the color of the canvas. For example, if the user selects both “Red” and “Blue”, the canvas should be purple.

Image
  import java.awt.*; import java.awt.event.*; public class Prac8 implements ItemListener {     Frame f;     Checkbox c1,c2,c3;     Canvas c;     int red=0,blue=0,green=0;     Prac8()     {         f=new Frame("Practical-8");         c1=new Checkbox("Red");         c2=new Checkbox("Green");         c3=new Checkbox("Blue");                  c1.addItemListener(this);         c2.addItemListener(this);         c3.addItemListener(this);                  c=new Canvas();         c.setSize(30,30);         c.setBackground(Color.BLACK);             f.setLayout(new FlowLayout());                  f.add(c1);         f.add(c2);         f.add(c3);         f.add(c);                  f.setSize(300,300);         f.setVisible(true);     }          public static void main(String[] args)     {         Prac8 f=new Prac8();     }     public void itemStateChanged(ItemEvent e)     {         if(c1.getState())             red=255;         else             red=0;         if

Practical-7 Develop a program that has only one button in the frame, clicking on the button cycles through the colors: red->green->blue and so on. One color changes per click.(use getBackGround() method to get the current color)

Image
  import java.awt.*; import java.awt.event.*; public class Practical7 implements ActionListener {     Frame f;     Button b;     public Practical7()     {         f=new Frame("Practical-7");         b=new Button("Click");         b.setBackground(Color.red);         b.addActionListener(this);         f.setLayout(new FlowLayout());         f.add(b);         f.setSize(300,300);         f.setVisible(true);     }     public static void main(String[] args)     {         Practical7 f1=new Practical7();     }     public void actionPerformed(ActionEvent e)     {         if(b.getBackground() == Color.RED)         {         b.setBackground(Color.GREEN);         }         else if(b.getBackground() == Color.GREEN)         {         b.setBackground(Color.BLUE);         }         else         {         b.setBackground(Color.RED);         }     } } Output:

Practical-6 Develop an applet that uses the mouse listener, which overrides only two methods which are mousePressed and mouseReleased.

Image
  import java.applet.*; import java.awt.*; import java.awt.event.*; public class Practical6 extends Applet implements MouseListener {     String msg="";     public void init() {         setSize(400,400);         addMouseListener(this);     }     public void paint(Graphics g)     {         showStatus(msg);     }     public void mousePressed(MouseEvent e)     {         msg="Mouse Pressed";         repaint();     }     public void mouseReleased(MouseEvent e)     {         msg="Mouse Released";         repaint();     }     public void mouseExited(MouseEvent e) {              }     public void mouseEntered(MouseEvent e) {             }     public void mouseClicked(MouseEvent e) {             } } Output:

Practical-5 Develop an applet that contains one button. Initialize the label on the button to “start”, when the user presses the button, which changes the label between these two values each time the button is pressed.

Image
  import java.applet.*; import java.awt.*; import java.awt.event.*; public class Practical5 extends Applet implements ActionListener {     Button b;     public void init() {         b=new Button("Start");         b.addActionListener(this);         add(b);     }     public void actionPerformed(ActionEvent e)     {         if(b.getLabel()=="Start")         {             b.setLabel("Stop");         }         else         {             b.setLabel("Start");         }     } } Output:

Practical-4 Develop an applet that display the position of the mouse at the upper left corner of the applet when it is dragged or moved. Draw a 10x10 pixel rectangle filed with black at the current mouse position.

Image
  import java.applet.*; import java.awt.*; import java.awt.event.*; public class Practical4 extends Applet implements MouseMotionListener {     static int x,y;     public void init()     {         addMouseMotionListener(this);     }     public void paint(Graphics g)     {         g.drawString("X=" + x + ", Y=" + y,15, 15);         g.drawRect(x, y, 10, 10);     }     public void mouseMoved(MouseEvent e)     {         x=e.getX();         y=e.getY();         repaint();     }     public void mouseDragged(MouseEvent e)     {        x=getX();        y=getY();        repaint();     }     } Output:

Practical-3 Built an applet that displays a horizontal rectangle in its center. Let the rectangle fill with color from left to right.

Image
  import java.applet.*; import java.awt.*; public class Practical3 extends Applet { public void paint(Graphics g) {     int width=10, height=50;     g.drawRect(30, 70, 240, 50);     g.setColor(Color.red);     for (int i=1;i<25;i++)     {         try         {         Thread.sleep(1000);         }         catch(InterruptedException e)         {             System.out.println("Exception generated");         }         g.fillRect(30, 70, width, height);         width=width+10;     } } } Output:

Practical-2 Draw ten red circles in a vertical column in the center of the applet.

Image
import java.applet.*; import java.awt.*; public class Practical2 extends Applet {     public void paint(Graphics g)     {         g.setColor(Color.red);         int x=230,y=0;         for(int i=1;i<=10;i++)         {             g.fillOval(x, y, 40, 40);             y=y+50;         }     } } Output:

Practical-1 Develop an applet that draws a circle. The dimension of the applet should be 500 x 300 pixels. The circle should be centered in the applet and have a radius of 100 pixels. Display your name centered in a circle.( using drawOval() method)

Image
  SimpleApplet.java import java.awt.*; import java.applet.*; public class SimpleApplet extends Applet { public void paint(Graphics g) { g.drawOval(150,50,200,200); g.drawString("Divyarajsinh",215,150); } } SimpleHtml.html <applet code= "SimpleApplet.class" width = "500" height= "300" > </applet> Output: