0% found this document useful (0 votes)
18 views

2.java Practicals

The document describes a Java program to create an animated ball movement using threads, applets, and graphics. It includes an algorithm that initializes the background color and ball color, creates and starts a thread, sets boundaries for ball movement, and repaints the scenario on each iteration. The program code implements this algorithm by importing necessary packages, initializing values, creating a thread, painting the ball graphics, and changing the ball's position on each loop iteration. The output shows the animated ball bouncing within the applet boundaries as intended.

Uploaded by

latiy65696
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

2.java Practicals

The document describes a Java program to create an animated ball movement using threads, applets, and graphics. It includes an algorithm that initializes the background color and ball color, creates and starts a thread, sets boundaries for ball movement, and repaints the scenario on each iteration. The program code implements this algorithm by importing necessary packages, initializing values, creating a thread, painting the ball graphics, and changing the ball's position on each loop iteration. The output shows the animated ball bouncing within the applet boundaries as intended.

Uploaded by

latiy65696
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

DATE: NAME:SHWETA DARSHINI P

REGNO:412521104139.

EX 8. Java program to create student report using applet, read the


input using text boxes and display the output using buttons

Aim:
To write a program to create student report using applet, read the input using
textbox and display the output using buttons.

Algorithm:
Step1: Start
Step2: Install jdk* and applet won’t support from jdk9 and upper versions.
Step3: Now create a student info class and declare all the required labels,
textboxes and buttons.
Step4: Create init function and create the labels, text fields and buttons
declared in the above class.
Step5: Create the action performed for each text box, Label and button in try
and catch blocks to avoid exceptions.
Step6: Stop.

Program:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class classstudentreport extends Applet implements ActionListener{
Label
lblTitle,lblRegNo,lblName,lblJava,lblSE,lblCA,lblBI,lblSSPD;
TextField
txtRegNo,txtName,txtJava,txtSE,txtCA,txtBI,txtSSPD;
Button
cmdReport;
int total;
float avg;
public void init(){
setLayout(null);
lblTitle = new Label("Enter Student's Details");
lblRegNo = new Label("Reg.No : ");
DATE: NAME:SHWETA DARSHINI P
REGNO:412521104139.
lblName = new Label("Name : ");
lblJava = new Label("Java : ");
lblSE = new Label("SE : ");
lblCA = new Label("CA :");
lblBI = new Label("BI :");
lblSSPD = new Label("SSPD :");
txtRegNo = new TextField(10);
txtName = new TextField(25);
txtJava = new TextField(3);
txtSE = new TextField(3);
txtCA = new TextField(3);
txtBI = new TextField(3);
txtSSPD = new TextField(3);
cmdReport = new Button("View Student Result");
lblTitle.setBounds(100,0,200,20);
lblRegNo.setBounds(0,50,100,20);
txtRegNo.setBounds(120,50,100,20);
lblName.setBounds(0,70,100,20);
txtName.setBounds(120,75,250,20);
lblJava.setBounds(0,100,100,20);
txtJava.setBounds(120,100,40,20);
lblSE.setBounds(0,125,100,20);
txtSE.setBounds(120,125,40,20);
lblCA.setBounds(0,150,100,20);
txtCA.setBounds(120,150,40,20);
lblBI.setBounds(0,175,100,20);
txtBI.setBounds(120,175,40,20);
lblSSPD.setBounds(0,200,100,20);
txtSSPD.setBounds(120,200,40,20);
cmdReport.setBounds(100,225,150,30);
add(lblTitle);
add(lblRegNo);
add(txtRegNo);
add(lblName);
add(txtName);
add(lblJava);
add(txtJava);
DATE: NAME:SHWETA DARSHINI P
REGNO:412521104139.
add(lblSE);
add(txtSE);
add(lblCA);
add(txtCA);
add(lblBI);
add(txtBI);
add(lblSSPD);
add(txtSSPD);
add(cmdReport);
cmdReport.addActionListener(this);
}
public void actionPerformed(ActionEvent ae){
try{
int java = Integer.parseInt(txtJava.getText());
int se = Integer.parseInt(txtSE.getText());
int ca = Integer.parseInt(txtCA.getText());
int bi = Integer.parseInt(txtBI.getText());
int sspd = Integer.parseInt(txtSSPD.getText());
total = (java + se + ca + bi + sspd);
avg = total/5;
}
catch(NumberFormatException e){
}
repaint();
}
public void paint(Graphics g){
g.drawString(" STUDENTREPORT ",100,275);
g.drawString("Reg. No = "+txtRegNo.getText(),0,300);
g.drawString("Name = "+txtName.getText(),0,325);
g.drawString("Java = "+txtJava.getText(),0,350);
g.drawString("Software Engineering = "+txtSE.getText(),0,375);
g.drawString("Computer Architecture = "+txtCA.getText(),0,400);
g.drawString("Banking & Insurance = "+txtBI.getText(),0,425);
g.drawString("SSPD = "+txtSSPD.getText(),0,450);
g.drawString("Total = "+total,0,475);
g.drawString("Average = "+avg,0,500);
}
DATE: NAME:SHWETA DARSHINI P
REGNO:412521104139.
}
/*<applet code="classstudentreport" height=800 width=800>
</applet>*/
DATE: NAME:SHWETA DARSHINI P
REGNO:412521104139.
Output:

Result:
Thus, the java program to write a program to write a program to create student
report using applet, read the input using text boxes and display the output
using buttons is executed successfully and output is verified.
DATE: NAME:SHWETA DARSHINI P
REGNO:412521104139.

EX 10: Java program to Implement Thread, Applets Graphics


Animate Ball Movement

Aim:
To write a program to implement thread, applets and graphics to animate ball
movement.

Algorithm:
Step1: Start
Step2: Import the Necessary packages.
Step3: Set the Backgroundcolor (setBackground method) and the color of the
ball (paint method) using methods.
Step4: Create thread and start running it( declare flag variable as true).
Step5: Using the dimensions from the html code, set boundaries within which
the ball can move at a certain rate.
Step6: Repaint the scenario at every instance when while loop gets iterated
every time.

Program:
import java.awt.*;
import java.io.*;
import java.applet.*;
import java.awt.event.*;
public class movingball extends Applet implements Runnable
{
int x,y,dx,dy;
Thread t;
boolean flag;
public void init()
{
setBackground(Color.black);
x=100;
y=10;
dx=10;dy=10;
}
DATE: NAME:SHWETA DARSHINI P
REGNO:412521104139.
public void start()
{
flag = true;
t = new Thread(this);
t.start();
}
public void paint(Graphics g)
{
g.setColor(Color.white);
g.fillOval(x,y,50,50);
}
public void run()
{
while(flag)
{
Rectangle r = getBounds();
if((x+dx<=0)||(x+dx>=r.width))
{
dx=-dx;
}
if((y+dy<=0)||(y+dy>=r.height))
dy=-dy;
x+=dx;
y+=dy;
repaint();
try{
Thread.sleep(300);
}
catch(InterruptedException e){}
}
}
public void stop()
{
t = null;
flag = false;
}
}
DATE: NAME:SHWETA DARSHINI P
REGNO:412521104139.
/*
<applet code = "movingball.class" height=100 width = 700></applet>
*/

Output:

Result:
Thus a program to implement thread, applets and graphics to animate ball
movement has been successfully executed.

You might also like