Wednesday, January 31, 2018

Programming With Java Unit 4 : Applets and Layout Managers for B.C.A.,M.C.A. and all IT Students


--------------------------------------------------------------------------------------------------------------------------
Prepared By : Uday Shah (HOD - IT)
Contact No   : 7600044051
E-Mail          : rupareleducation@gmail.com



Programming With Java Unit 4
Introduction to Applet
An applet is a Java program that runs in a Web browser. An applet can be a fully functional Java application because it has the entire Java API at its disposal.
There are some important differences between an applet and a standalone Java application, including the following −
·        An applet is a Java class that extends the java.applet.Applet class.
·        A main() method is not invoked on an applet, and an applet class will not define main().
·        Applets are designed to be embedded within an HTML page.
·        When a user views an HTML page that contains an applet, the code for the applet is downloaded to the user's machine.
·        A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser or a separate runtime environment.
·        The JVM on the user's machine creates an instance of the applet class and invokes various methods during the applet's lifetime.
·        Applets have strict security rules that are enforced by the Web browser.
·        The security of an applet is often referred to as sandbox security, comparing the applet to a child playing in a sandbox with various rules that must be followed.
·        Other classes that the applet needs can be downloaded in a single Java Archive (JAR) file.

Life Cycle of an Applet

Four methods in the Applet class gives you the framework on which you build any serious applet −
·        init − This method is intended for whatever initialization is needed for your applet. It is called after the param tags inside the applet tag have been processed.
·        start − This method is automatically called after the browser calls the init method. It is also called whenever the user returns to the page containing the applet after having gone off to other pages.
·        stop − This method is automatically called when the user moves off the page on which the applet sits. It can, therefore, be called repeatedly in the same applet.
·        destroy − This method is only called when the browser shuts down normally. Because applets are meant to live on an HTML page, you should not normally leave resources behind after a user leaves the page that contains the applet.
·        paint − Invoked immediately after the start() method, and also any time the applet needs to repaint itself in the browser. The paint() method is actually inherited from the java.awt.

A "Hello, World" Applet

Following is a simple applet named HelloWorldApplet.java −
import java.applet.*;
import java.awt.*;
public class HelloWorldApplet extends Applet 
{
            public void paint (Graphics g) 
            {
                        g.drawString ("Hello World", 25, 50);
            }
}


Parameter in Applet

We can get any information from the HTML file as a parameter. For this purpose, Applet class provides a method named getParameter(). Syntax:
public String getParameter(String parameterName)  

Example of using parameter in Applet:

import java.applet.Applet;  
import java.awt.Graphics;  
public class UseParam extends Applet
{  
public void paint(Graphics g)
{  
String str=getParameter("msg");  
g.drawString(str,5050);  
}
}  

myapplet.html
<html>  
<body>  
<applet code="UseParam.class" width="300" height="300">  
<param name="msg" value="Welcome to applet">  
</applet>  
</body>  
</html>



Graphics Class

The Graphics class is the abstract super class for all graphics contexts which allow an application to draw onto components that can be realized on various devices, or onto off-screen images as well.
A Graphics object encapsulates all state information required for the basic rendering operations that Java supports.
1.     public abstract void drawString(String str, int x, int y): is used to draw the specified string.
2.     public void drawRect(int x, int y, int width, int height): draws a rectangle with the specified width and height.
3.     public abstract void fillRect(int x, int y, int width, int height): is used to fill rectangle with the default color and specified width and height.
4.     public abstract void drawOval(int x, int y, int width, int height): is used to draw oval with the specified width and height.
5.     public abstract void fillOval(int x, int y, int width, int height): is used to fill oval with the default color and specified width and height.
6.     public abstract void drawLine(int x1, int y1, int x2, int y2): is used to draw line between the points(x1, y1) and (x2, y2).
7.     public abstract void setColor(Color c): is used to set the graphics current color to the specified color.
8.     public abstract void setFont(Font font): is used to set the graphics current font to the specified font.


import java.applet.Applet;  
import java.awt.*;  
  
public class GraphicsDemo extends Applet
{    
public void paint(Graphics g)
{  
g.setColor(Color.red);  
g.drawString("Welcome",5050);  
g.drawLine(20,30,20,300);  
g.drawRect(70,100,30,30);  
g.fillRect(170,100,30,30);  
g.drawOval(70,200,30,30);   
g.setColor(Color.pink);  
g.fillOval(170,200,30,30);  
g.drawArc(90,150,30,30,30,270);  
g.fillArc(270,150,30,30,0,180);  
}  
}
  


Layout Manager

The layout manager automatically positions all the components within the container. If we do not use layout manager then also the components are positioned by the default layout manager. It is possible to layout the controls by hand but it becomes very difficult because of the following two reasons.
·        It is very tedious to handle a large number of controls within the container.
·        Oftenly the width and height information of a component is not given when we need to arrange them.
Java provide us with various layout manager to position the controls. The properties like size, shape and arrangement varies from one layout manager to other layout manager. When the size of the applet or the application window changes the size, shape and arrangement of the components also changes in response i.e. the layout managers adapt to the dimensions of appletviewer or the application window.
The layout manager is associated with every Container object. Each layout manager is an object of the class that implements the LayoutManager interface.

1
The borderlayout arranges the components to fit in the five regions: east, west, north, south and center. Each region is can contain only one component and each component in each region is identified by the corresponding constant NORTH, SOUTH, EAST, WEST, and CENTER.
2
The CardLayout object treats each component in the container as a card. Only one card is visible at a time.  and the container acts as a stack of cards.
3
The FlowLayout is the default layout.It layouts the components in a directional flow.
4
The GridLayout manages the components in form of a rectangular grid.
5
This is the most flexible layout manager class.The object of GridBagLayout aligns the component vertically,horizontally or along their baseline without requiring the components of same size.

GridxGridy
Specify the row and column at the upper left of the component. The leftmost column has address gridx=0, and the top row has address gridy=0.

GridwidthGridheight
Specify the number of columns (for gridwidth) or rows (for gridheight) in the component's display area.

Fill
Used when the component's display area is larger than the component's requested size to determine whether and how to resize the component.

IpadxIpady
Specifies the internal padding: how much to add to the minimum size of the component. The default value is zero.

Insets
Specifies the external padding of the component  the minimum amount of space between the component and the edges of its display area.

Anchor
Used when the component is smaller than its display area to determine where (within the area) to place the component.

WeightxWeighty
Specifying weights is an art that can have a significant impact on the appearance of the components a GridBagLayout controls. Weights are used to determine how to distribute space among columns (weightx) and among rows (weighty);
6
Java BoxLayout
The BoxLayout is used to arrange the components either vertically or horizontally. For this purpose, BoxLayout provides four constants.
The BoxLayout manager is constructed with an axis parameter that specifies the type of layout that will be done. There are four choices:
X_AXIS - Components are laid out horizontally from left to right.
Y_AXIS - Components are laid out vertically from top to bottom.
LINE_AXIS - Components are laid out the way words are laid out in a line, based on the container's ComponentOrientation property.
PAGE_AXIS - Components are laid out the way text lines are laid out on a page, based on the container's ComponentOrientation property.
7

Java SpringLayout

SpringLayout arranges the children of its associated container according to a set of constraints. Constraints are nothing but horizontal and vertical distance between two component edges. Every constrains are represented by a SpringLayout.Constraint object.
Each child of a SpringLayout container, as well as the container itself, has exactly one set of constraints associated with them.
Each edge position is dependent on the position of the other edge. If a constraint is added to create new edge than the previous binding is discarded. SpringLayout doesn't automatically set the location of the components it manages.
8

GroupLayout

GroupLayout groups its components and places them in a Container hierarchically. The grouping is done by instances of the Group class.
Group is an abstract class and two concrete classes which implement this Group class are SequentialGroup and ParallelGroup.
SequentialGroup positions its child sequentially one after another where as ParallelGroup aligns its child on top of each other.
The GroupLayout class provides methods such as createParallelGroup() and createSequentialGroup() to create groups.
GroupLayout treats each axis independently. That is, there is a group representing the horizontal axis, and a group representing the vertical axis. Each component must exists in both a horizontal and vertical group, otherwise an IllegalStateException is thrown during layout, or when the minimum, preferred or maximum size is requested.
9
Using NO LAYOUT Manager (Absolute Manager)
Although it is possible to do without a layout manager, you should use a layout manager if at all possible. A layout manager makes it easier to adjust to look-and-feel-dependent component appearances, to different font sizes, to a container's changing size, and to different locales. Layout managers also can be reused easily by other containers, as well as other programs.
We know that in Java Layout Managers  that two styles of placing components in containers – pixel format and position format.
We also know that, the default layout manager for Frame is BorderLayout and for Panel and Applet, it is FlowLayout. To use pixel format, first we must set the default layout manager of the container to null. To give the size and location, setBounds() method is used.

Tuesday, January 30, 2018

Android Short Questions For B.C.A.,M.C.A. and all IT students


--------------------------------------------------------------------------------------------------------------------------
Prepared By : Uday Shah (HOD-IT)
Contact No  : 7600044051
E-Mail         : rupareleducation@gmail.com


Android Short Questions for all IT students

·       What is Android?

Android is an open-source, linux-based operating system that is used in mobiles, tablets, televisions etc.

·       Who is the founder of Android?

Andy Rubin.

·       Which kernal is used in android?
Android is customized Linux 3.6 kernel.

·       Explain the Android application Architecture.

Following is a list of components of Android application architecture:
  • Services: Used to perform background functionalities.
  • Intent: Used to perform the inter connection between activities and the data passing mechanism.
  • Resource Externalization: strings and graphics.
  • Notification: light, sound, icon, notification, dialog box and toast.
  • Content Providers: It will share the data between applications.

·       What are the code names of android?



1.                      Aestro
2.                      Blender
3.                     Cupcake
4.                      Donut
5.                     Eclair
6.                     Froyo
7.                    Gingerbread
8.                    Honycomb
9.                    Ice Cream Sandwitch
10.                  Jelly Bean
11.                  Kitkat
12.                   Lollipop
13.                   Marshmallow
14.                   Orario



·       What are the advantages of android?

Open-source: It means no licence, distribution and development fee.
Platform-independent: It supports windows, mac and linux platforms.
Supports various technologies: It supports camera, bluetooth, wifi, speech, EDGE etc. technologies.
Highly optimized Virtual Machine: Android uses highly optimized virtual machine for mobile devices, called DVM (Dalvik Virtual Machine).

·       Does android support other language than java?

Yes, android app can be developed in C/C++ also using android NDK (Native Development Kit). It makes the performance faster. It should be used with android SDK

·       What are the core building blocks of android?

The core building blocks of android are:
  • Activity
  • View
  • Intent
  • Service
  • Content Provider
  • Fragment etc.

·       What is activity?

Activity is like a frame or window in java that represents GUI. It represents one screen of android.

·       What are the life cycle methods of android activity?

There are 7 life-cycle methods of activity. They are as follows:
1.     onCreate()
2.     onStart()
3.     onResume()
4.     onPause()
5.     onStop()
6.     onRestart()
  1. onDestroy()

·       What is intent?

It is a kind of message or information that is passed to the components. It is used to launch an activity, display a web page, send sms, send email etc. There are two types of intents in android:
1.     Implicit Intent
  1. Explicit Intent

·       What is implicit intent in android?

Implicit intent is used to invoke the system components.

·       What is explicit intent in android?

Explicit intent is used to invoke the activity class.

·       What is content provider?

Content providers are used to share information between android applications.

·       What is ADB?

ADB stands for Android Debug Bridge. It is a command line tool that is used to communicate with the emulator instance.

·       What is an APK format?

APK is a short form stands for Android Packaging Key. It is a compressed key with classes,UI's, supportive assets and manifest. All files are compressed to a single file is called APK.

·       What is ADT in Android?

ADT stands for Android Development Tool. It is used to develop the applications and test the applications.

·       What is View Group in Android?

View Group is a collection of views and other child views. It is an invisible part and the base class for layouts.

·       What is DDMS?

DDMS stands for Dalvik Debug Monitor Server. It gives the wide array of debugging features:
1.     Port forwarding services
2.     Screen capture
3.     Thread and heap information
4.     Network traffic tracking
  1. Location data spoofing

·       What are the basic tools used to develop an android app?

  • JDK
  • Eclipse+ADT plugin
  • SDK Tools
·       What is the AndroidManifest.xml?
This file is essential in every application. It is declared in the root directory and contains information about the application that the Android system must know before the codes can be executed.

·       What is shared preferences in android?
Shared preferences are the simplest mechanism to store the data in XML documents.

·       What does the intent filter do in android?
Intent filters are filter out the intents.

·       What is drawable folder in android?
A compiled visual resource that can used as a backgrounds, banners, icons, splash screen etc.

·       What is a service in android?
The Service is like as an activity to do background functionalities without UI interaction.

·       How is the use of web view in Android?
WebView is UI component that can display either remote web-pages or static HTML



·       What is Context?
The context is the central command center for an Android application. All application-specific functionality can be accessed through the context.

·        What is an Activity?

An Android application is a collection of tasks, each of which is called an Activity. Each Activity within an application has a unique task or purpose.


·       What is an Intent?

The Android operating system uses an asynchronous messaging mechanism to match task requests with the appropriate Activity. Each request is packaged as an
Intent. You can think of each such request as a message stating an intent to do something.

·        What is a Service?
Tasks that do not require user interaction can be encapsulated in a service. A service is most useful when the operations are lengthy, offloading time-consuming processing  or  need  to  be  done  regularly  such  as  checking a server for new mail.