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

Java Programs 1

The document discusses two Java programs. The first program implements animation of a ball moving by creating a thread and using applets and graphics. The second program implements mouse events by adding mouse listeners and updating the mouse movement text. It also includes a program to create a thread using the Runnable interface.

Uploaded by

Soma Shekara
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)
99 views

Java Programs 1

The document discusses two Java programs. The first program implements animation of a ball moving by creating a thread and using applets and graphics. The second program implements mouse events by adding mouse listeners and updating the mouse movement text. It also includes a program to create a thread using the Runnable interface.

Uploaded by

Soma Shekara
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

Part A:

9)Write a program to implement thread,applets and graphics by implementing


animation of ball moving.

import java.applet.*;

import java.awt.*;

/*<applet code="MovingBall" width=200 height=200>

</applet>*/

public class MovingBall extends Applet implements Runnable

int x=150,y=50,r=50;

int dx=11,dy=7;

Thread animator;

volatile boolean pleaseStop;

public void paint(Graphics g)

g.setColor(Color.green);

g.fillOval(x-r,y-r,r*2,r*2);

public void animate()

Rectangle bounds=getBounds();

if((x-r+dx<0)||(x+r+dx>bounds.width))
dx=-dx;

if((y-r+dy<0)||(y+r+dy>bounds.height))

dy=-dy;

x+=dx;

y+=dy;

repaint();

public void run()

while(!pleaseStop)

animate();

try

Thread.sleep(100);

catch(InterruptedException e)

public void start()

animator=new Thread(this);

pleaseStop=false;
animator.start();

public void stop()

pleaseStop=true;

Output:

javac MovingBall.java

appletviewer MovingBall.java

10)Write a program to implement mouse events.


import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*<applet code="MouseEvents" width=300 height=300>

</applet>*/

public class MouseEvents extends Applet implements MouseListener,MouseMotionListener

String str="";

public void init()

{
addMouseListener(this);

addMouseMotionListener(this);

public void paint(Graphics g)

g.drawString(str,20,20);

public void mousePressed(MouseEvent me)

str="Mouse Button Pressed";

repaint();

public void mouseClicked(MouseEvent me)

str="Mouse Button Clicked";

repaint();

public void mouseReleased(MouseEvent me)

str="Mouse Button Released";

repaint();

public void mouseEntered(MouseEvent me)

str="Mouse Entered";

repaint();

}
public void mouseExited(MouseEvent me)

str="Mouse Button Exited";

repaint();

public void mouseMoved(MouseEvent me)

str="Mouse Moved";

repaint();

public void mouseDropped(MouseEvent me)

str="Mouse Dropped";

repaint();

public void mouseDragged(MouseEvent me)

str="Mouse Dragged";

repaint();

Output: javac MouseEvents.java

Appletviewer MouseEvents.java

Part B

10)Write a program to create a Thread using Runnable Interface.


class MyThread implements Runnable

public void run()

System.out.println("run() started");

for(int i=1;i<=10;i++)

System.out.println("The value is:"+i);

System.out.println("run() completeed");

public class ThreadRunnable

public static void main(String args[])

System.out.println("Main Thread Started");

MyThread obj=new MyThread();

Thread t1=new Thread(obj,"Thread One");

t1.start();

System.out.println("Main Thread Completeed");

Output:

Javac ThreadRunnable.java

Java ThreadRunnable
Package
Ex:

1)

package mypack

public class Simple{

public static void main(String args[]){

System.out.println("Welcome to package");

o/p:

compile and execute

2) package mypack.pack1.pack2;

public class Simple{

public static void main(String args[]){

System.out.println("Welcome to package");

o/p:

Compile and Execute

2)package pack;

public class A

public void msg()

{
System.out.println("Hello");

o/p:

Compile

******************

package mypack1;

import pack.A;

class B{

public static void main(String args[]){

A obj = new A();

obj.msg();

o/p:

Compile and Execute

You might also like