0% found this document useful (0 votes)
13 views19 pages

Mouse Prorgram in Java

The document contains multiple Java programs including a mouse event handler using AWT, a calculator built with Swing, a digital watch application, and a bouncing ball animation in a JPanel. Each program demonstrates different aspects of Java GUI programming and event handling. The code snippets are structured for educational purposes, showcasing practical implementations of Java features.

Uploaded by

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

Mouse Prorgram in Java

The document contains multiple Java programs including a mouse event handler using AWT, a calculator built with Swing, a digital watch application, and a bouncing ball animation in a JPanel. Each program demonstrates different aspects of Java GUI programming and event handling. The code snippets are structured for educational purposes, showcasing practical implementations of Java features.

Uploaded by

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

/*Mouse prg*/

Program:

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

/*

<applet code="MouseEvents" width=300 height=100>

</applet>

*/

public class MouseEvents extends Applet implements MouseListener,MouseMotionListener

String msg="";

int mouseX=0;//coordinates of mouse

int mouseY=0;//coordinates of mouse

public void init()

addMouseListener(this);

addMouseMotionListener(this);

//HANDLE MOUSE CLICKED

public void mouseClicked(MouseEvent me)

//save coordinates

mouseX=0;

mouseY=10;

msg="MouseClicked";

repaint();
}

//HANDLE MOUSE ENTERED

public void mouseEntered(MouseEvent me)

//save coordinates

mouseX=0;

mouseY=10;

msg="Mouse Entered";

repaint();

//HANDLE MOUSE EXITED

public void mouseExited(MouseEvent me)

//save coordinates

mouseX=0;

mouseY=10;

msg="Mouse Exited ";

repaint();

//HANDLE MOUSE BUTTON PRESSED

public void mousePressed(MouseEvent me)

//save coordinates

mouseX=me.getX();

mouseY=me.getY();

msg="Down ";

repaint();
}

//HANDLE MOUSE BUTTON RELEASED

public void mouseReleased(MouseEvent me)

//save coordinates

mouseX=me.getX();

mouseY=me.getY();

msg="Up ";

repaint();

//HANDLE MOUSE BUTTON DRAGGED

public void mouseDragged(MouseEvent me)

//save coordinates

mouseX=me.getX();

mouseY=me.getY();

msg="*";

showStatus("Dragging mouse at "+mouseX+" , "+mouseY);

repaint();

//HANDLE MOUSE MOVED

public void mouseMoved(MouseEvent me)

//Show status

showStatus("Mopving mouse at "+me.getX()+" , "+me.getY());

//Display msg in applet window at current X,Y location


public void paint(Graphics g)

g.drawString(msg,mouseX,mouseY);

Output:
Exercise - 15

a).Write a JAVA programto build a Calculator in Swings

Program:

import javax.swing.*;

import java.awt.event.*;

class Calc extends JFrame implements ActionListener

JTextField t;

JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bdiv,bmul,bsub,badd,bdec,beq,bdel,bclr;

static double a=0,b=0,result=0;

static int operator=0;

Calc()

t=new JTextField();

b1=new JButton("1");

b2=new JButton("2");

b3=new JButton("3");

b4=new JButton("4");

b5=new JButton("5");

b6=new JButton("6");

b7=new JButton("7");

b8=new JButton("8");

b9=new JButton("9");

b0=new JButton("0");

bdiv=new JButton("/");

bmul=new JButton("*");

bsub=new JButton("-");
badd=new JButton("+");

bdec=new JButton(".");

beq=new JButton("=");

bdel=new JButton("Delete");

bclr=new JButton("Clear");

t.setBounds(30,40,280,30);

b7.setBounds(40,100,50,40);

b8.setBounds(110,100,50,40);

b9.setBounds(180,100,50,40);

bdiv.setBounds(250,100,50,40);

b4.setBounds(40,170,50,40);

b5.setBounds(110,170,50,40);

b6.setBounds(180,170,50,40);

bmul.setBounds(250,170,50,40);

b1.setBounds(40,240,50,40);

b2.setBounds(110,240,50,40);

b3.setBounds(180,240,50,40);

bsub.setBounds(250,240,50,40);

bdec.setBounds(40,310,50,40);

b0.setBounds(110,310,50,40);

beq.setBounds(180,310,50,40);

badd.setBounds(250,310,50,40);

bdel.setBounds(60,380,100,40);

bclr.setBounds(180,380,100,40);

add(t);

add(b7);

add(b8);
add(b9);

add(bdiv);

add(b4);

add(b5);

add(b6);

add(bmul);

add(b1);

add(b2);

add(b3);

add(bsub);

add(bdec);

add(b0);

add(beq);

add(badd);

add(bdel);

add(bclr);

setLayout(null);

setVisible(true);

setSize(350,500);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//setResizable(false);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

b5.addActionListener(this);

b6.addActionListener(this);
b7.addActionListener(this);

b8.addActionListener(this);

b9.addActionListener(this);

b0.addActionListener(this);

badd.addActionListener(this);

bdiv.addActionListener(this);

bmul.addActionListener(this);

bsub.addActionListener(this);

bdec.addActionListener(this);

beq.addActionListener(this);

bdel.addActionListener(this);

bclr.addActionListener(this);

public void actionPerformed(ActionEvent e)

if(e.getSource()==b1)

t.setText(t.getText().concat("1"));

if(e.getSource()==b2)

t.setText(t.getText().concat("2"));

if(e.getSource()==b3)

t.setText(t.getText().concat("3"));

if(e.getSource()==b4)

t.setText(t.getText().concat("4"));
if(e.getSource()==b5)

t.setText(t.getText().concat("5"));

if(e.getSource()==b6)

t.setText(t.getText().concat("6"));

if(e.getSource()==b7)

t.setText(t.getText().concat("7"));

if(e.getSource()==b8)

t.setText(t.getText().concat("8"));

if(e.getSource()==b9)

t.setText(t.getText().concat("9"));

if(e.getSource()==b0)

t.setText(t.getText().concat("0"));

if(e.getSource()==bdec)

t.setText(t.getText().concat("."));

if(e.getSource()==badd)

a=Double.parseDouble(t.getText());

operator=1;

t.setText("");

}
if(e.getSource()==bsub)

a=Double.parseDouble(t.getText());

operator=2;

t.setText("");

if(e.getSource()==bmul)

a=Double.parseDouble(t.getText());

operator=3;

t.setText("");

if(e.getSource()==bdiv)

a=Double.parseDouble(t.getText());

operator=4;

t.setText("");

if(e.getSource()==beq)

b=Double.parseDouble(t.getText());

switch(operator)
{

case 1: result=a+b;

break;

case 2: result=a-b;

break;

case 3: result=a*b;

break;

case 4: result=a/b;

break;

default: result=0;

t.setText(""+result);

if(e.getSource()==bclr)

t.setText("");

if(e.getSource()==bdel)

String s=t.getText();

t.setText("");

for(int i=0;i<s.length()-1;i++)
t.setText(t.getText()+s.charAt(i));

public static void main(String args[])

new Calc();

Output:
15 b)Write a JAVA program to display the digital watch in swing tutorial.

Program:

import javax.swing.*;

import java.awt.*;

import java.text.*;

import java.util.*;

public class DigitalWatch extends JFrame implements Runnable

Thread t=null;

int hours=0, minutes=0, seconds=0;

String timeString = "";

JButton b;

DigitalWatch()

t = new Thread(this);

t.start();

b=new JButton();

b.setBounds(100,100,100,50);

add(b);

setSize(300,400);

setLayout(null);

setVisible(true);

}
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 );

printTime();

t.sleep( 1000 ); // interval given in milliseconds

catch (Exception e) { }

public void printTime(){

b.setText(timeString);
}

public static void main(String[] args) {

new DigitalWatch();

Output:
Exercise – 16

a). Write a JAVA program that to create a single ball bouncing inside a JPanel.

Program:

import java.awt.*;

import javax.swing.*;

public class BouncingBall extends JPanel

int width; // Box height and width

int height;

float radius = 40; // Ball Size

float diameter = radius * 2;

float X = radius + 50; // Center of Call

float Y = radius + 20;

float dx = 3; // Direction

float dy = 3;

public BouncingBall()

Thread thread = new Thread()

public void run()

while (true)

width = getWidth();
height = getHeight();

X = X + dx ;

Y = Y + dy;

if (X - radius < 0)

dx = -dx;

X = radius;

else if (X + radius > width)

dx = -dx;

X = width - radius;

if (Y - radius < 0)

dy = -dy;

Y = radius;

else if (Y + radius > height)

dy = -dy;

Y = height - radius;

repaint();

try

Thread.sleep(50);
}

catch (InterruptedException ex)

};

thread.start();

public void paintComponent(Graphics g)

super.paintComponent(g);

g.setColor(Color.BLUE);

g.fillOval((int)(X-radius), (int)(Y-radius), (int)diameter, (int)diameter);

public static void main(String[] args)

JFrame.setDefaultLookAndFeelDecorated(true);

JFrame frame = new JFrame("Bouncing Ball");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(300, 200);

frame.setContentPane(new BouncingBall());

frame.setVisible(true);

}
Output:

You might also like