0% found this document useful (0 votes)
51 views8 pages

Java Applet Animation and Mouse Events

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)
51 views8 pages

Java Applet Animation and Mouse Events

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

Part A:

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


animation of ball moving.

import [Link].*;

import [Link].*;

/*<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)

[Link]([Link]);

[Link](x-r,y-r,r*2,r*2);

public void animate()

Rectangle bounds=getBounds();

if((x-r+dx<0)||(x+r+dx>[Link]))
dx=-dx;

if((y-r+dy<0)||(y+r+dy>[Link]))

dy=-dy;

x+=dx;

y+=dy;

repaint();

public void run()

while(!pleaseStop)

animate();

try

[Link](100);

catch(InterruptedException e)

public void start()

animator=new Thread(this);

pleaseStop=false;
[Link]();

public void stop()

pleaseStop=true;

Output:

javac [Link]

appletviewer [Link]

10)Write a program to implement mouse events.


import [Link].*;

import [Link].*;

import [Link].*;

/*<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)

[Link](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 [Link]

Appletviewer [Link]

Part B

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


class MyThread implements Runnable

public void run()

[Link]("run() started");

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

[Link]("The value is:"+i);

[Link]("run() completeed");

public class ThreadRunnable

public static void main(String args[])

[Link]("Main Thread Started");

MyThread obj=new MyThread();

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

[Link]();

[Link]("Main Thread Completeed");

Output:

Javac [Link]

Java ThreadRunnable
Package
Ex:

1)

package mypack

public class Simple{

public static void main(String args[]){

[Link]("Welcome to package");

o/p:

compile and execute

2) package mypack.pack1.pack2;

public class Simple{

public static void main(String args[]){

[Link]("Welcome to package");

o/p:

Compile and Execute

2)package pack;

public class A

public void msg()

{
[Link]("Hello");

o/p:

Compile

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

package mypack1;

import pack.A;

class B{

public static void main(String args[]){

A obj = new A();

[Link]();

o/p:

Compile and Execute

Common questions

Powered by AI

Import statements enable access to classes and interfaces from other packages, avoiding fully qualified names. In package-related examples, `import pack.A;` allows class `A` usage without prefixing the package name, implying modularity and reusability by accessing necessary classes without extending package names. This simplifies class instantiation and invocation from different packages .

The `volatile` keyword ensures visibility of changes to variables across threads. In the animation applet context, `volatile boolean pleaseStop;` ensures that the flag's updated value is visible to the animation loop thread, allowing it to exit cleanly and stop the animation when the applet is stopped. This prevents thread caching issues and ensures thread-safe interactions .

A Java applet handles mouse events by implementing the MouseListener and MouseMotionListener interfaces, which provide methods for different mouse actions such as mousePressed, mouseClicked, and mouseMoved. Each method updates a string with event-specific messages and calls repaint to reflect the changes visually in the applet's graphics .

Thread sleep introduces a delay between animation frames, controlling the speed of the animation. In the moving ball applet, `Thread.sleep(100);` creates a smooth animation by pausing execution for 100 milliseconds, allowing graphics to update visibly. This prevents excessive CPU usage and provides a controllable frame rate, crucial for smooth visual rendering .

Creating and executing a Java thread using the Runnable interface involves defining a class that implements Runnable, providing a run method where the thread's task is specified. A Thread object is instantiated with this Runnable instance, and the thread is started using the start method. The execution sequence includes the creation of the Runnable object, thread instantiation, and invocation of start leading to run execution .

The mouseClicked event indicates a complete click action (press and release), while mousePressed captures just the press action. Differentiating these allows distinct interactions; for instance, triggering an action immediately when pressed may be useful for instant feedback, whereas clicked might handle confirmation actions after full release. Handling both events provides comprehensive control in user interaction .

Implementing MouseMotionListener enhances applet interactivity by monitoring mouse movements and drags, offering real-time feedback as the mouse pointer travels across the applet. Unlike MouseListener, which handles static clicks and press events, MouseMotionListener captures continuous motion, allowing dynamic interaction such as drawing or dragging elements .

Collision detection ensures the ball rebounds when hitting the applet's boundaries, maintaining the animation within bounds. This is achieved by reversing the direction variables `dx` and `dy` when the current position with radius exceeds the boundary limits, preventing the ball from moving out of view and ensuring continuous animation .

Key elements include defining a class that extends Applet and implements Runnable to allow multithreading. The main components are initializing graphic settings in the paint method, handling animation logic in the animate method by updating the ball's coordinates, and managing thread creation and lifecycle using start and stop methods to control the animation. Collision detection is managed by reversing direction when boundaries are reached .

Defining packages in Java organizes code into namespaces, promoting modularity and preventing naming conflicts. Compilation and execution require the correct directory structure matching package names. For example, `package mypack;` implies a directory named `mypack` must exist. Usage impacts include needing explicit package declarations, import statements for package-contained classes, and compiling with directory structure awareness .

You might also like