hereral/Clara-Training-Data huggingface dataset:
Question: What are the Fields of BoxLayout Class?
Answer: 1.public static final int X_AXIS: Alignment of the components are horizontal from left to right.
2.public static final int Y_AXIS: Alignment of the components are vertical from top to bottom.
3.public static final int LINE_AXIS: Alignment of the components is similar to the way words are aligned in a line, which is based on the ComponentOrientation property of the container. If the ComponentOrientation property of the container is horizontal, then the components are aligned horizontally; otherwise, the components are aligned vertically. For horizontal orientations, we have two cases: left to right, and right to left. If the value ComponentOrientation property of the container is from left to right, then the components are rendered from left to right, and for right to left, the rendering of components is also from right to left. In the case of vertical orientations, the components are always rendered from top to bottom.
4.public static final int PAGE_AXIS: Alignment of the components is similar to the way text lines are put on a page, which is based on the ComponentOrientation property of the container. If the ComponentOrientation property of the container is horizontal, then components are aligned vertically; otherwise, the components are aligned horizontally. For horizontal orientations, we have two cases: left to right, and right to left. If the value ComponentOrientation property of the container is also from left to right, then the components are rendered from left to right, and for right to left, the rendering of components is from right to left. In the case of vertical orientations, the components are always rendered from top to bottom.
Question: What is Constructor of BoxLayout class?
Answer: BoxLayout(Container c, int axis): creates a box layout that arranges the components with the given axis.
Question: Can you provide an Example of BoxLayout class with Y-AXIS:?
Answer: FileName: BoxLayoutExample1.java

import java.awt.*;    
import javax.swing.*;    
    
public class BoxLayoutExample1 extends Frame {    
 Button buttons[];    
    
 public BoxLayoutExample1 () {    
   buttons = new Button [5];    
      
   for (int i = 0;i<5;i++) {    
      buttons[i] = new Button ("Button " + (i + 1));    
      // adding the buttons so that it can be displayed  
      add (buttons[i]);    
    }    
  // the buttons will be placed horizontally  
setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));    
setSize(400,400);    
setVisible(true);    
}    
  // main method  
public static void main(String args[]){    
BoxLayoutExample1 b=new BoxLayoutExample1();    
}    
}
Question: Can you provide an Example of BoxLayout class with X-AXIS?
Answer: FileName: BoxLayoutExample2.java

import java.awt.*;    
import javax.swing.*;    
    
public class BoxLayoutExample2 extends Frame {    
 Button buttons[];    
    
 public BoxLayoutExample2() {    
   buttons = new Button [5];    
      
   for (int i = 0;i<5;i++) {    
      buttons[i] = new Button ("Button " + (i + 1));    
      // adding the buttons so that it can be displayed  
      add (buttons[i]);    
    }    
 // the buttons in the output will be aligned vertically   
setLayout (new BoxLayout(this, BoxLayout.X_AXIS));    
setSize(400,400);    
setVisible(true);    
}    
 // main method   
public static void main(String args[]){    
BoxLayoutExample2 b=new BoxLayoutExample2();    
}    
}
Question: Can you provide an Example of BoxLayout Class with LINE_AXIS?
Answer: The following example shows the effect of setting the value of ComponentOrientation property of the container to RIGHT_TO_LEFT. If we do not set the value of ComponentOrientation property, then the components would be laid out from left to right. Comment line 11, and see it yourself.

FileName: BoxLayoutExample3.java

// import statements  
import java.awt.*;    
import javax.swing.*;    
    
public class BoxLayoutExample3 extends Frame   
{    
Button buttons[];    
  
// constructor of the class    
public BoxLayoutExample3()   
{    
buttons = new Button[5];  
setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);  // line 11  
  
for (int i = 0; i < 5; i++)   
{    
  buttons[i] = new Button ("Button " + (i + 1));   
    
  // adding the buttons so that it can be displayed    
  add (buttons[i]);    
}    
  
// the ComponentOrientation is set to RIGHT_TO_LEFT. Therefore,  
// the added buttons will be rendered from right to left   
setLayout (new BoxLayout(this, BoxLayout.LINE_AXIS));    
setSize(400, 400);    
setVisible(true);    
}    
    
// main method  
public static void main(String argvs[])  
{    
// creating an object of the class BoxLayoutExample3  
BoxLayoutExample3 obj = new BoxLayoutExample3();    
}    
}
Question: Can you provide an Example of BoxLayout Class with PAGE_AXIS?
Answer: The following example shows how to use PAGE_AXIS.

FileName: BoxLayoutExample4.java

// import statements  
import java.awt.*;    
import javax.swing.*;    
    
public class BoxLayoutExample4 extends Frame   
{    
Button buttons[];    
// constructor of the class  
public BoxLayoutExample4()   
{    
JFrame f = new JFrame();  
JPanel pnl = new JPanel();  
buttons = new Button[5];  
GridBagConstraints constrntObj = new GridBagConstraints();  
    
constrntObj.fill = GridBagConstraints.VERTICAL;  
for (int i = 0; i < 5; i++)   
{    
  buttons[i] = new Button ("Button " + (i + 1));   
    
  // adding the buttons so that it can be displayed  
  add(buttons[i]);    
}    
  
// the components will be displayed just like the line is present on a page  
setLayout (new BoxLayout(this, BoxLayout.PAGE_AXIS));    
setSize(400, 400);    
setVisible(true);    
}    
    
// main method  
public static void main(String argvs[])  
{    
// creating an object of the class BoxLayoutExample4  
BoxLayoutExample4 obj = new BoxLayoutExample4();  
}    
}
Question: What is Java CardLayout?
Answer: The Java CardLayout class manages the components in such a manner that only one component is visible at a time. It treats each component as a card that is why it is known as CardLayout.
Question: What are the Constructors of CardLayout Class?
Answer: 1.CardLayout(): creates a card layout with zero horizontal and vertical gap.
2.CardLayout(int hgap, int vgap): creates a card layout with the given horizontal and vertical gap.
Question: What are the Commonly Used Methods of CardLayout Class?
Answer: -public void next(Container parent): is used to flip to the next card of the given container.
-public void previous(Container parent): is used to flip to the previous card of the given container.
-public void first(Container parent): is used to flip to the first card of the given container.
-public void last(Container parent): is used to flip to the last card of the given container.
-public void show(Container parent, String name): is used to flip to the specified card with the given name.
Question: Can you provide an Example of CardLayout Class: Using Default Constructor
Answer: The following program uses the next() method to move to the next card of the container.

FileName: CardLayoutExample1.java

// import statements  
import java.awt.*;    
import javax.swing.*;   
import java.awt.event.*;    
  
public class CardLayoutExample1 extends JFrame implements ActionListener  
{    
  
CardLayout crd;    
  
// button variables to hold the references of buttons  
JButton btn1, btn2, btn3;    
Container cPane;   
  
// constructor of the class  
CardLayoutExample1()  
{    
  
cPane = getContentPane();    
  
//default constructor used  
// therefore, components will   
// cover the whole area  
crd = new CardLayout();    
  
cPane.setLayout(crd);    
  
// creating the buttons  
btn1 = new JButton("Apple");    
btn2 = new JButton("Boy");    
btn3 = new JButton("Cat");    
  
// adding listeners to it  
btn1.addActionListener(this);    
btn2.addActionListener(this);    
btn3.addActionListener(this);    
  
cPane.add("a", btn1); // first card is the button btn1  
cPane.add("b", btn2); // first card is the button btn2  
cPane.add("c", btn3);  // first card is the button btn3  
            
}    
public void actionPerformed(ActionEvent e)   
{    
// Upon clicking the button, the next card of the container is shown  
// after the last card, again, the first card of the container is shown upon clicking  
crd.next(cPane);    
}    
  
// main method  
public static void main(String argvs[])   
{     
// creating an object of the class CardLayoutExample1  
CardLayoutExample1 crdl = new CardLayoutExample1();   
  
// size is 300 * 300          
crdl.setSize(300, 300);    
crdl.setVisible(true);    
crdl.setDefaultCloseOperation(EXIT_ON_CLOSE);    
}    
}
Question: What is Java GridBagLayout?
Answer: The Java GridBagLayout class is used to align components vertically, horizontally or along their baseline.

The components may not be of the same size. Each GridBagLayout object maintains a dynamic, rectangular grid of cells. Each component occupies one or more cells known as its display area. Each component associates an instance of GridBagConstraints. With the help of the constraints object, we arrange the component's display area on the grid. The GridBagLayout manages each component's minimum and preferred sizes in order to determine the component's size. GridBagLayout components are also arranged in the rectangular grid but can have many different sizes and can occupy multiple rows or columns.
Question: What is GridBagLayout Fields?
Answer: Modifier and Type: double[]
Field: columnWeights
Description: It is used to hold the overrides to the column weights.

Modifier and Type: int[]
Field: columnWidths
Description: It is used to hold the overrides to the column minimum width.

Modifier and Type: protected Hashtable<Component,GridBagConstraints>
Field: comptable
Description: It is used to maintains the association between a component and its gridbag constraints.

Modifier and Type: protected GridBagConstraints
Field: defaultConstraints
Description: It is used to hold a gridbag constraints instance containing the default values.

Modifier and Type: protected GridBagLayoutInfo
Field: layoutInfo
Description: It is used to hold the layout information for the gridbag.

Modifier and Type: protected static int
Field: MAXGRIDSIZE
Description: No longer in use just for backward compatibility



Modifier and Type: protected static int
Field: MINSIZE
Description: It is smallest grid that can be laid out by the grid bag layout.

Modifier and Type: protected static int
Field: PREFERREDSIZE
Description: It is preferred grid size that can be laid out by the grid bag layout.

Modifier and Type: int[]
Field: rowHeights
Description: It is used to hold the overrides to the row minimum heights.

Modifier and Type: double[]
Field: rowWeights
Description: It is used to hold the overrides to the row weights.
Question: Can you give me GridBagLayout Methods?
Answer: Modifier and Type: void
Method: addLayoutComponent(Component comp, Object constraints)
Description: It adds specified component to the layout, using the specified constraints object.

Modifier and Type: void
Method: addLayoutComponent(String name, Component comp)
Description: It has no effect, since this layout manager does not use a per-component string.


Modifier and Type: protected void
Method: adjustForGravity(GridBagConstraints constraints, Rectangle r)
Description: It adjusts the x, y, width, and height fields to the correct values depending on the constraint geometry and pads.


Modifier and Type: protected void
Method: AdjustForGravity(GridBagConstraints constraints, Rectangle r)
Description: This method is for backwards compatibility only


Modifier and Type: protected void
Method: arrangeGrid(Container parent)
Description: Lays out the grid.




Modifier and Type: protected void
Method: ArrangeGrid(Container parent)
Description: This method is obsolete and supplied for backwards compatibility

Modifier and Type: GridBagConstraints
Method: getConstraints(Component comp)
Description: It is for getting the constraints for the specified component.

Modifier and Type: float
Method: getLayoutAlignmentX(Container parent)
Description: It returns the alignment along the x axis.

Modifier and Type: float
Method: getLayoutAlignmentY(Container parent)
Description: It returns the alignment along the y axis.

Modifier and Type: int[][]
Method: getLayoutDimensions()
Description: It determines column widths and row heights for the layout grid.

Modifier and Type: protected GridBagLayoutInfo
Method: getLayoutInfo(Container parent, int sizeflag)
Description: This method is obsolete and supplied for backwards compatibility.



Modifier and Type: protected GridBagLayoutInfo
Method: GetLayoutInfo(Container parent, int sizeflag)
Description: This method is obsolete and supplied for backwards compatibility.


Modifier and Type: Point
Method: getLayoutOrigin()
Description: It determines the origin of the layout area, in the graphics coordinate space of the target container.

Modifier and Type: double[][]
Method: getLayoutWeights()
Description: It determines the weights of the layout grid's columns and rows.

Modifier and Type: protected Dimension
Method: getMinSize(Container parent, GridBagLayoutInfo info)
Description: It figures out the minimum size of the master based on the information from getLayoutInfo.

Modifier and Type: protected Dimension
Method: GetMinSize(Container parent, GridBagLayoutInfo info)
Description: This method is obsolete and supplied for backwards compatibility only
Question: What is GroupLayout?
Answer: 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 whereas 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 exist in both a horizontal and vertical group, otherwise an IllegalStateException is thrown during layout or when the minimum, preferred, or maximum size is requested.
Question: What is Java SpringLayout?
Answer: A 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 constraint is 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 a new edge, than the previous binding is discarded. SpringLayout doesn't automatically set the location of the components it manages.
Question: What is pringLayout Fields?
Answer: Modifier and Type: static String
Field: BASELINE
Description: It specifies the baseline of a component.

Modifier and Type: static String
Field: EAST
Description: It specifies the right edge of a component's bounding rectangle.

Modifier and Type: static String
Field: HEIGHT
Description: It specifies the height of a component's bounding rectangle.

Modifier and Type: static String
Field: HORIZONTAL_CENTER
Description: It specifies the horizontal center of a component's bounding rectangle.

Modifier and Type: static String
Field: NORTH
Description: It specifies the top edge of a component's bounding rectangle.

Modifier and Type: static String
Field: SOUTH
Description: It specifies the bottom edge of a component's bounding rectangle.



Modifier and Type: static String
Field: VERTICAL_CENTER
Description: It specifies the vertical center of a component's bounding rectangle.

Modifier and Type: static String
Field: WEST
Description: It specifies the left edge of a component's bounding rectangle.

Modifier and Type: static String
Field: WIDTH
Description: It specifies the width of a component's bounding rectangle.
Question: Can you give SpringLayout Methods?
Answer: SpringLayout Methods
Modifier and Type: void
Method: addLayoutComponent(Component component, Object constraints)
Description: If constraints is an instance of SpringLayout. Constraints, associates the constraints with the specified component.

Modifier and Type: void
Method: addLayoutComponent(String name, Component c)
Description: Has no effect, since this layout manager does not use a per-component string.

Modifier and Type: Spring
Method: getConstraint(String edgeName, Component c)
Description: It returns the spring controlling the distance between the specified edge of the component and the top or left edge of its parent.

Modifier and Type: SpringLayout.Constraints
Method: getConstraints(Component c)
Description: 
It returns the constraints for the specified component.


Modifier and Type: float
Method: getLayoutAlignmentX(Container p)
Description: It returns 0.5f (centered).

Modifier and Type: float
Method: getLayoutAlignmentY(Container p)
Description: It returns 0.5f (centered).



Modifier and Type: void
Method: invalidateLayout(Container p)
Description: It Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.

Modifier and Type: void
Method: layoutContainer(Container parent)
Description: It lays out the specified container.

Modifier and Type: Dimension
Method: maximumLayoutSize(Container parent)
Description: It is used to calculates the maximum size dimensions for the specified container, given the components it contains.

Modifier and Type: Dimension
Method: minimumLayoutSize(Container parent)
Description: It is used to calculates the minimum size dimensions for the specified container, given the components it contains.

Modifier and Type: Dimension
Method: preferredLayoutSize(Container parent)
Description: It is used to calculates the preferred size dimensions for the specified container, given the components it contains.
Question: What is ScrollPaneLayout?
Answer: The layout manager is used by JScrollPane. JScrollPaneLayout is responsible for nine components: a viewport, two scrollbars, a row header, a column header, and four "corner" components.
Question: FieldScrollPanelLayout Field
Answer: Modifier and Type: protected JViewport
Field: colHead
Description: It is column header child.

Modifier and Type: protected JScrollBar
Field: hsb
Description: It is scrollpane's horizontal scrollbar child.

Modifier and Type: protected int
Field: hsbPolicy
Description: It displays policy for the horizontal scrollbar.


Modifier and Type: protected Component
Field: lowerLeft
Description: This displays the lower left corner.


Modifier and Type: protected Component
Field: lowerRight
Description: This displays in the lower right corner.


Modifier and Type: protected JViewport
Field: rowHead
Description: It is row header child.


Modifier and Type: protected Component
Field: upperLeft
Description: This component displays in the upper left corner.


Modifier and Type: protected Component
Field: upperRight
Description: This component displays in the upper right corner.


Modifier and Type: protected JViewport
Field: viewport
Description: It is scrollpane's viewport child.


Modifier and Type: protected JScrollBar
Field: vsb
Description: It is scrollpane's vertical scrollbar child.


Modifier and Type: protected int
Field: vsbPolicy
Description: It is the display policy for the vertical scrollbar.
Question: Can you give me ScrollPanelLayout Methods?
Answer: ScrollPanelLayout Methods


Modifier and Type: void
Method: addLayoutComponent(String s, Component c)
Description: It adds the specified component to the layout.

Modifier and Type: protected Component
Method: addSingletonComponent(Component oldC, Component newC)
Description: It removes an existing component.

Modifier and Type: JViewport
Method: getColumnHeader()
Description: It returns the JViewport object that is the column header.

Modifier and Type: Component
Method: getCorner(String key)
Description: It returns the Component at the specified corner.

Modifier and Type: JScrollBar
Method: getHorizontalScrollBar()
Description: It returns the JScrollBar object that handles horizontal scrolling.

Modifier and Type: int
Method: getHorizontalScrollBarPolicy()
Description: It returns the horizontal scrollbar-display policy.

Modifier and Type: JViewport
Method: getRowHeader()
Description: It returns the JViewport object that is the row header.

Modifier and Type: JScrollBar
Method: getVerticalScrollBar()
Description: It returns the JScrollBar object that handles vertical scrolling.

Modifier and Type: int
Method: getVerticalScrollBarPolicy()
Description: It returns the vertical scrollbar-display policy.

Modifier and Type: JViewport
Method: getViewport()
Description: It returns the JViewport object that displays the scrollable contents.
Question: What is Java Applet?
Answer: Applet is a special type of program that is embedded in the webpage to generate the dynamic content. It runs inside the browser and works at client side.
Question: What is the Advantage of Applet?
Answer: There are many advantages of applet. They are as follows:

-It works at client side so less response time.
-Secured
-It can be executed by browsers running under many plateforms, including Linux, Windows, Mac Os etc.
Question: What is Drawback of Applet?
Answer: Plugin is required at client browser to execute applet.
Question: What is Lifecycle of Java Applet?
Answer: 1.Applet is initialized.
2.Applet is started.
3.Applet is painted.
4.Applet is stopped.
5.Applet is destroyed.
Question: What is Lifecycle methods for Applet?
Answer: The java.applet.Applet class 4 life cycle methods and java.awt.Component class provides 1 life cycle methods for an applet.
Question: What is java.applet.Applet class?
Answer: For creating any applet java.applet.Applet class must be inherited. It provides 4 life cycle methods of applet.

1.public void init(): is used to initialized the Applet. It is invoked only once.
2.public void start(): is invoked after the init() method or browser is maximized. It is used to start the Applet.
3.public void stop(): is used to stop the Applet. It is invoked when Applet is stop or browser is minimized.
4.public void destroy(): is used to destroy the Applet. It is invoked only once.
Question: What is java.awt.Component class?
Answer: The Component class provides 1 life cycle method of applet.

1.public void paint(Graphics g): is used to paint the Applet. It provides Graphics class object that can be used for drawing oval, rectangle, arc etc.
Question: Who is responsible to manage the life cycle of an applet?
Answer: Java Plug-in software.
Question: How to run an Applet?
Answer: There are two ways to run an applet

1.By html file.
2.By appletViewer tool (for testing purpose).
Question: Can you provide an Simple example of Applet by html file
Answer: To execute the applet by html file, create an applet and compile it. After that create an html file and place the applet code in html file. Now click the html file.

//First.java  
import java.applet.Applet;  
import java.awt.Graphics;  
public class First extends Applet{  
  
public void paint(Graphics g){  
g.drawString("welcome",150,150);  
}  
  
}  
Note: class must be public because its object is created by Java Plugin software that resides on the browser.
myapplet.html
<html>  
<body>  
<applet code="First.class" width="300" height="300">  
</applet>  
</body>  
</html>
Question: What is Displaying Graphics in Applet?
Answer: java.awt.Graphics class provides many methods for graphics programming.
Commonly used methods of Graphics class:
Question: What is Commonly used methods of Graphics class?
Answer: 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 boolean drawImage(Image img, int x, int y, ImageObserver observer): is used draw the specified image.
8.public abstract void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle): is used draw a circular or elliptical arc.
9.public abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle): is used to fill a circular or elliptical arc.
10.public abstract void setColor(Color c): is used to set the graphics current color to the specified color.
11.public abstract void setFont(Font font): is used to set the graphics current font to the specified font.
Question: Can you provide an Example of Graphics in applet?
Answer: import java.applet.Applet;  
import java.awt.*;  
  
public class GraphicsDemo extends Applet{  
  
public void paint(Graphics g){  
g.setColor(Color.red);  
g.drawString("Welcome",50, 50);  
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);  
  
}  
}  
myapplet.html
<html>  
<body>  
<applet code="GraphicsDemo.class" width="300" height="300">  
</applet>  
</body>  
</html>
Question: What is Displaying Image in Applet?
Answer: Applet is mostly used in games and animation. For this purpose image is required to be displayed. The java.awt.Graphics class provide a method drawImage() to display the image.
Question: Can you give me Syntax of drawImage() method?
Answer: public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer): is used draw the specified image.
Question: How to get the object of Image?
Answer: The java.applet.Applet class provides getImage() method that returns the object of Image. Syntax:
public Image getImage(URL u, String image){}
Question: Can you give me Other required methods of Applet class to display image?
Answer: 1.public URL getDocumentBase(): is used to return the URL of the document in which applet is embedded.
2.public URL getCodeBase(): is used to return the base URL.
Question: Can you provide an Example of displaying image in applet?
Answer: import java.awt.*;  
import java.applet.*;  
  
  
public class DisplayImage extends Applet {  
  
  Image picture;  
  
  public void init() {  
    picture = getImage(getDocumentBase(),"sonoo.jpg");  
  }  
    
  public void paint(Graphics g) {  
    g.drawImage(picture, 30,30, this);  
  }  
      
  }  
In the above example, drawImage() method of Graphics class is used to display the image. The 4th argument of drawImage() method of is ImageObserver object. The Component class implements ImageObserver interface. So current class object would also be treated as ImageObserver because Applet class indirectly extends the Component class.
myapplet.html
<html>  
<body>  
<applet code="DisplayImage.class" width="300" height="300">  
</applet>  
</body>  
</html>
Question: What is Animation in Applet?
Answer: Applet is mostly used in games and animation. For this purpose image is required to be moved.
Question: Can you provide an Example of animation in applet?
Answer: import java.awt.*;  
import java.applet.*;  
public class AnimationExample extends Applet {  
  
  Image picture;  
  
  public void init() {  
    picture =getImage(getDocumentBase(),"bike_1.gif");  
  }  
    
  public void paint(Graphics g) {  
    for(int i=0;i<500;i++){  
      g.drawImage(picture, i,30, this);  
  
      try{Thread.sleep(100);}catch(Exception e){}  
    }  
  }  
}  
In the above example, drawImage() method of Graphics class is used to display the image. The 4th argument of drawImage() method of is ImageObserver object. The Component class implements ImageObserver interface. So current class object would also be treated as ImageObserver because Applet class indirectly extends the Component class.
myapplet.html
<html>  
<body>  
<applet code="DisplayImage.class" width="300" height="300">  
</applet>  
</body>  
</html>
Question: What is EventHandling in Applet?
Answer: As we perform event handling in AWT or Swing, we can perform it in applet also. Let's see the simple example of event handling in applet that prints a message by click on the button.
Question: Can you provide an Example of EventHandling in applet?
Answer: import java.applet.*;  
import java.awt.*;  
import java.awt.event.*;  
public class EventApplet extends Applet implements ActionListener{  
Button b;  
TextField tf;  
  
public void init(){  
tf=new TextField();  
tf.setBounds(30,40,150,20);  
  
b=new Button("Click");  
b.setBounds(80,150,60,50);  
  
add(b);add(tf);  
b.addActionListener(this);  
  
setLayout(null);  
}  
  
 public void actionPerformed(ActionEvent e){  
  tf.setText("Welcome");  
 }   
}  
In the above example, we have created all the controls in init() method because it is invoked only once.
myapplet.html
<html>  
<body>  
<applet code="EventApplet.class" width="300" height="300">  
</applet>  
</body>  
</html>
Question: What is JApplet class in Applet?
Answer: As we prefer Swing to AWT. Now we can use JApplet that can have all the controls of swing. The JApplet class extends the Applet class.
Question: Example of EventHandling in JApplet
Answer: import java.applet.*;  
import javax.swing.*;  
import java.awt.event.*;  
public class EventJApplet extends JApplet implements ActionListener{  
JButton b;  
JTextField tf;  
public void init(){  
  
tf=new JTextField();  
tf.setBounds(30,40,150,20);  
  
b=new JButton("Click");  
b.setBounds(80,150,70,40);  
  
add(b);add(tf);  
b.addActionListener(this);  
  
setLayout(null);  
}  
  
public void actionPerformed(ActionEvent e){  
tf.setText("Welcome");  
}  
}  
In the above example, we have created all the controls in init() method because it is invoked only once.
myapplet.html
<html>  
<body>  
<applet code="EventJApplet.class" width="300" height="300">  
</applet>  
</body>  
</html>
Question: Painting in Applet
Answer: We can perform painting operation in applet by the mouseDragged() method of MouseMotionListener.
Question: Can you provide an Example of Painting in Applet?
Answer: import java.awt.*;  
import java.awt.event.*;  
import java.applet.*;  
public class MouseDrag extends Applet implements MouseMotionListener{  
  
public void init(){  
addMouseMotionListener(this);  
setBackground(Color.red);  
}  
  
public void mouseDragged(MouseEvent me){  
Graphics g=getGraphics();  
g.setColor(Color.white);  
g.fillOval(me.getX(),me.getY(),5,5);  
}  
public void mouseMoved(MouseEvent me){}  
  
}  
In the above example, getX() and getY() method of MouseEvent is used to get the current x-axis and y-axis. The getGraphics() method of Component class returns the object of Graphics.
myapplet.html
<html>  
<body>  
<applet code="MouseDrag.class" width="300" height="300">  
</applet>  
</body>  
</html>
Question: What is Digital clock in Applet?
Answer: Digital clock can be created by using the Calendar and SimpleDateFormat class. Let's see the simple example:
Question: Can you provide an Example of Digital clock in Applet?
Answer: import java.applet.*;  
import java.awt.*;  
import java.util.*;  
import java.text.*;  
  
public class DigitalClock extends Applet implements Runnable {  
  
   Thread t = null;  
   int hours=0, minutes=0, seconds=0;  
   String timeString = "";  
  
   public void init() {  
      setBackground( Color.green);  
   }  
  
   public void start() {  
        t = new Thread( this );  
        t.start();  
   }  
  
    
   public void run() {  
      try {  
         while (true) {  
  
            Calendar cal = Calendar.getInstance();  
            hours = cal.get( Calendar.HOUR_OF_DAY );  
            if ( hours > 12 ) hours -= 12;  
            minutes = cal.get( Calendar.MINUTE );  
            seconds = cal.get( Calendar.SECOND );  
  
            SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");  
            Date date = cal.getTime();  
            timeString = formatter.format( date );  
  
            repaint();  
            t.sleep( 1000 );  // interval given in milliseconds  
         }  
      }  
      catch (Exception e) { }  
   }  
  
    
  public void paint( Graphics g ) {  
      g.setColor( Color.blue );  
      g.drawString( timeString, 50, 50 );  
   }  
}  
In the above example, getX() and getY() method of MouseEvent is used to get the current x-axis and y-axis. The getGraphics() method of Component class returns the object of Graphics.
myapplet.html
<html>  
<body>  
<applet code="DigitalClock.class" width="300" height="300">  
</applet>  
</body>  
</html>
Question: What is Analog clock in Applet?
Answer: Analog clock can be created by using the Math class. Let's see the simple example:
Question: What is Parameter in Applet?
Answer: 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)
Question: Can you provide an Example of using parameter in Applet?
Answer: 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,50, 50);  
}  
  
}  
myapplet.html
<html>  
<body>  
<applet code="UseParam.class" width="300" height="300">  
<param name="msg" value="Welcome to applet">  
</applet>  
</body>  
</html>
Question: What is Applet Communication?
Answer: java.applet.AppletContext class provides the facility of communication between applets. We provide the name of applet through the HTML file. It provides getApplet() method that returns the object of Applet. Syntax:

public Applet getApplet(String name){}
Question: Can you provide an Example of Applet Communication?
Answer: import java.applet.*;  
import java.awt.*;  
import java.awt.event.*;  
public class ContextApplet extends Applet implements ActionListener{  
Button b;  
  
public void init(){  
b=new Button("Click");  
b.setBounds(50,50,60,50);  
  
add(b);  
b.addActionListener(this);  
}  
  
public void actionPerformed(ActionEvent e){  
  
AppletContext ctx=getAppletContext();  
Applet a=ctx.getApplet("app2");  
a.setBackground(Color.yellow);  
}  
}  
myapplet.html
<html>  
<body>  
<applet code="ContextApplet.class" width="150" height="150" name="app1">  
</applet>  
  
<applet code="First.class" width="150" height="150" name="app2">  
</applet>  
</body>  
</html>
Question: What is Java Reflection API?
Answer: Java Reflection is a process of examining or modifying the run time behavior of a class at run time.

The java.lang.Class class provides many methods that can be used to get metadata, examine and change the run time behavior of a class.

The java.lang and java.lang.reflect packages provide classes for java reflection.
Question: What is java.lang.Class class?
Answer: The java.lang.Class class performs mainly two tasks:

provides methods to get the metadata of a class at run time.
provides methods to examine and change the run time behavior of a class.
Question: Can you give me Commonly used methods of Class class?
Answer: Commonly used methods of Class class:

Method: public String getName()
Description: returns the class name

Method: public static Class forName(String className)throws ClassNotFoundException
Description: loads the class and returns the reference of Class class.

Method: public Object newInstance()throws InstantiationException,IllegalAccessException
Description: creates new instance.

Method: public boolean isInterface()
Description: checks if it is interface.

Method: public boolean isArray()
Description: checks if it is array.

Method: public boolean isPrimitive()
Description: checks if it is primitive.

Method: public Class getSuperclass()
Description: returns the superclass class reference.

Method: public Field[] getDeclaredFields()throws SecurityException
Description: returns the total number of fields of this class.

Method: public Method[] getDeclaredMethods()throws SecurityException
Description: returns the total number of methods of this class.
Method: public Constructor[] getDeclaredConstructors()throws SecurityException
Description: returns the total number of constructors of this class.

Method: public Method getDeclaredMethod(String name,Class[] parameterTypes)throws NoSuchMethodException,SecurityException
Description: returns the method class instance.
Question: How to get the object of Class class?
Answer: There are 3 ways to get the instance of Class class. They are as follows:

-forName() method of Class class
-getClass() method of Object class
-the .class syntax
Question: What is Determining the class object?
Answer: The following methods of Class class are used to determine the class object:

1) public boolean isInterface(): determines if the specified Class object represents an interface type.

2) public boolean isArray(): determines if this Class object represents an array class.

3) public boolean isPrimitive(): determines if the specified Class object represents a primitive type.
Question: What are the Pros and Cons of Reflection?
Answer: Java reflection should always be used with caution. While the reflection provides a lot of advantages, it has some disadvantages too. Let's discuss the advantages first.

Pros: Inspection of interfaces, classes, methods, and fields during runtime is possible using reflection, even without using their names during the compile time. It is also possible to call methods, instantiate a clear or to set the value of fields using reflection. It helps in the creation of Visual Development Environments and class browsers which provides aid to the developers to write the correct code.

Cons: Using reflection, one can break the principles of encapsulation. It is possible to access the private methods and fields of a class using reflection. Thus, reflection may leak important data to the outside world, which is dangerous. For example, if one access the private members of a class and sets null value to it, then the other user of the same class can get the NullReferenceException, and this behaviour is not expected.

Another demerit is the overhead in performance. Since the types in reflection are resolved dynamically, JVM (Java Virtual Machine) optimization cannot take place. Therefore, the operations performed by reflections are usually slow.
Question: What is  Instance() method?
Answer: The newInstance() method of Class class and Constructor class is used to create a new instance of the class.

The newInstance() method of Class class can invoke zero-argument constructor, whereas newInstance() method of Constructor class can invoke any number of arguments. So, Constructor class is preferred over Class class..
Question: What is Syntax of newInstance() method of Class class?
Answer: public T newInstance()throws InstantiationException,IllegalAccessException

Here T is the generic version. You can think of it as Object class. You will learn about generics later.
Question: What is Understanding javap tool?
Answer: The javap command disassembles a class file. The javap command displays information about the fields, constructors and methods present in a class file.
Question: What is Syntax to use javap tool?
Answer: Let's see how to use javap tool or command.

javap fully_class_name
Question: Can you provide an Example to use javap tool?

Answer: javap java.lang.Object  
Output:

Compiled from "Object.java"
public class java.lang.Object {
  public java.lang.Object();
  public final native java.lang.Class<?> getClass();
  public native int hashCode();
  public boolean equals(java.lang.Object);
  protected native java.lang.Object clone() throws java.lang.CloneNotSupportedException;
  public java.lang.String toString();
  public final native void notify();
  public final native void notifyAll();
  public final native void wait(long) throws java.lang.InterruptedException;
  public final void wait(long, int) throws java.lang.InterruptedException;
  public final void wait() throws java.lang.InterruptedException;
  protected void finalize() throws java.lang.Throwable;
  static {};
}
Question: Can you create a program that works as javap tool?
Answer: Following methods of java.lang.Class class can be used to display the metadata of a class.
Method: public Field[] getDeclaredFields()throws SecurityException
Description: returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object.

Method: public Constructor[] getDeclaredConstructors()throws SecurityException
Description: returns an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object.

Method: public Method[] getDeclaredMethods()throws SecurityException
Description: returns an array of Method objects reflecting all the methods declared by the class or interface represented by this Class object.
Question: Can you provide an Example of creating javap tool?
Answer: Let's create a program that works like javap tool.

import java.lang.reflect.*;  
  
public class MyJavap{  
   public static void main(String[] args)throws Exception {  
    Class c=Class.forName(args[0]);  
      
    System.out.println("Fields........");  
    Field f[]=c.getDeclaredFields();  
    for(int i=0;i<f.length;i++)  
        System.out.println(f[i]);  
      
    System.out.println("Constructors........");  
    Constructor con[]=c.getDeclaredConstructors();  
    for(int i=0;i<con.length;i++)  
        System.out.println(con[i]);  
      
        System.out.println("Methods........");  
    Method m[]=c.getDeclaredMethods();  
    for(int i=0;i<m.length;i++)  
        System.out.println(m[i]);  
   }  
}
Question: how to creeate your own appletviewer?
Answer: As you know well that appletviewer tool creates a frame and displays the output of applet in the frame.You can also create your frame and display the applet output.
Question: Can you provide an example that works like applet viewer tool?
Answer: Let's see the simple example that works like appletviewer tool. This example displays applet on the frame.

import java.applet.Applet;  
import java.awt.Frame;  
import java.awt.Graphics;  
  
public class MyViewer extends Frame{  
  public static void main(String[] args) throws Exception{  
    Class c=Class.forName(args[0]);  
          
    MyViewer v=new MyViewer();  
    v.setSize(400,400);  
    v.setLayout(null);  
    v.setVisible(true);  
          
    Applet a=(Applet)c.newInstance();  
    a.start();  
    Graphics g=v.getGraphics();  
    a.paint(g);  
    a.stop();  
          
  }  
  
}  
//simple program of applet  
  
import java.applet.Applet;  
import java.awt.Graphics;  
  
public class First extends Applet{  
  
    public void paint(Graphics g){g.drawString("Welcome",50, 50);}  
}
Question: How to call private method from another class in java?
Answer: You can call the private method from outside the class by changing the runtime behaviour of the class.

With the help of java.lang.Class class and java.lang.reflect.Method class, we can call a private method from any other class.
Question: What are the Required methods of the Method class?
Answer: 1) public void setAccessible(boolean status) throws SecurityException sets the accessibility of the method.

2) public Object invoke(Object method, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException is used to invoke the method.
Question: What is the Required method of Class class?
Answer: 1) public Method getDeclaredMethod(String name,Class[] parameterTypes)throws NoSuchMethodException,SecurityException: returns a Method object that reflects the specified declared method of the class or interface represented by this Class object.
Question: Can you provide an Example of calling a private method from another class?
Answer: Let's see the simple example to call private method from another class.

File: A.java

public class A {  
  private void message(){System.out.println("hello java"); }  
}  
File: MethodCall.java

import java.lang.reflect.Method;  
public class MethodCall{  
public static void main(String[] args)throws Exception{  
  
    Class c = Class.forName("A");  
    Object o= c.newInstance();  
    Method m =c.getDeclaredMethod("message", null);  
    m.setAccessible(true);  
    m.invoke(o, null);  
}  
}  
Output:

hello java
Question: What is Java Date and Time?
Answer: The java.time, java.util, java.sql and java.text packages contains classes for representing date and time. Following classes are important for dealing with date in Java.
Question: Can you give me the list of Java 8 Date/Time API?
Answer: Java has introduced a new Date and Time API since Java 8. The java.time package contains Java 8 Date and Time classes.

-java.time.LocalDate class
-java.time.LocalTime class
-java.time.LocalDateTime class
-java.time.MonthDay class
-java.time.OffsetTime class
-java.time.OffsetDateTime class
-java.time.Clock class
-java.time.ZonedDateTime class
-java.time.ZoneId class
-java.time.ZoneOffset class
-java.time.Year class
-java.time.YearMonth class
-java.time.Period class
-java.time.Duration class
-java.time.Instant class
-java.time.DayOfWeek enum
-java.time.Month enum
Question: Can you give me the list of Classical Date/Time API?
Answer: But classical or old Java Date API is also useful. Let's see the list of classical Date and Time classes.

-java.util.Date class
-java.sql.Date class
-java.util.Calendar class
-java.util.GregorianCalendar class
-java.util.TimeZone class
-java.sql.Time class
-java.sql.Timestamp class
Question: What are the following classes in Formatting Date and Time?
Answer: We can format date and time in Java by using the following classes:

-java.text.DateFormat class
-java.text.SimpleDateFormat class
Question: What is Java Date and Time APIs?
Answer: Java provide the date and time functionality with the help of two packages java.time and java.util. The package java.time is introduced in Java 8, and the newly introduced classes tries to overcome the shortcomings of the legacy java.util.Date and java.util.Calendar classes.
Question: What is Classical Date Time API Classes?
Answer: The primary classes before Java 8 release were:

Java.lang.System: The class provides the currentTimeMillis() method that returns the current time in milliseconds. It shows the current date and time in milliseconds from January 1st 1970.

java.util.Date: It is used to show specific instant of time, with unit of millisecond.

java.util.Calendar: It is an abstract class that provides methods for converting between instances and manipulating the calendar fields in different ways.

java.text.SimpleDateFormat: It is a class that is used to format and parse the dates in a predefined manner or user defined pattern.

java.util.TimeZone: It represents a time zone offset, and also figures out daylight savings.