A Simple scribbling app

28 Jun

This one shows the power of the MouseListener and the MouseMotionListener. Its advisable to implement it using the MouseAdapter and the MouseMotionAdapter.


import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JFrame;

public class PaintStuff extends JFrame{
	int prev_x, prev_y;
	public PaintStuff(){
		// adds a Mouse Listener and so as to avoid implementing all the methods, use the MouseAdapter
		addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent e){
				prev_x= e.getX();
		//Add the mouseMotionListener containing the mouseDragged and mouseMoved methods
		addMouseMotionListener(new MouseMotionAdapter(){
			public void mouseDragged(MouseEvent e){
				int x= e.getX();
				int y = e.getY();
				drawStuff(prev_x, prev_y, x, y);
				prev_x = x;
				prev_y = y;
	public void drawStuff(int x0, int y0, int x1, int y1){
		//will draw the line
		this.getGraphics().drawLine(x0, y0, x1, y1);
	public static void main(String args[]){
		PaintStuff drawing = new PaintStuff();

Leave a comment

Posted by on June 28, 2012 in Uncategorized


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: