AWT !!

To draw a line on applet as we move mouse

import java.applet.*;

import java.awt.*;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.awt.event.MouseMotionListener;

public class line extends Applet implements MouseMotionListener,MouseListener

{ int x,y;

Graphics g;

public void init()

{      g=getGraphics();

addMouseMotionListener(this);

addMouseListener(this);

}

@Override

public void mouseClicked(MouseEvent arg0) {

// TODO Auto-generated method stub

}

@Override

public void mousePressed(MouseEvent arg0) {

// TODO Auto-generated method stub

}

@Override

public void mouseDragged(MouseEvent arg0) {

// TODO Auto-generated method stub

x=arg0.getX();

y=arg0.getY();

g.fillOval(x,y,2,2);

}

@Override

public void mouseMoved(MouseEvent arg0) {

// TODO Auto-generated method stub

}

public void mouseEntered(MouseEvent arg0) {

// TODO Auto-generated method stub

}

@Override

public void mouseExited(MouseEvent arg0) {

// TODO Auto-generated method stub

}

@Override

public void mouseReleased(MouseEvent arg0) {

// TODO Auto-generated method stub

}

}

 

AWT CONTROLS

Get means to retrieve, set means to assign. Only Get has returns .

Program to illustrate actionlistener on buttons and its properties.

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

 

import javax.swing.JButton;

import javax.swing.JOptionPane;

public class mybutton extends Applet implements ActionListener

{      JButton b1,b2;

public void init()

{

b1=new JButton(“click”);

b1.setBounds(15,15,120,40);

b1.setBackground(Color.BLUE);

add(b1);

b2=new JButton(“exit”);

b2.setBounds(150,150,120,40);

b2.setBackground(Color.pink);

add(b2);

b1.addActionListener(this);

b2.addActionListener(this);

}

 

@Override

public void actionPerformed(ActionEvent arg0) {

// TODO Auto-generated method stub

if (arg0.getSource()==b1)

{

JOptionPane.showConfirmDialog(null, “hello”);

}

if (arg0.getSource()==b2)

{

System.exit(0);

}

}

}

Advertisements

Constructive and genuine appreciation and/or criticism most welcome

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s