Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Explain Code?

Status
Not open for further replies.

Yorkiee

Technical User
Oct 2, 2003
18
IE
Could anyone please explain in english what's been carried out in this code. The lines i especially want explained are just below the comment where the user moves the mouse, the new and old x & y bit!

Code:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.image.*;


	
public class SimpleDraw extends JFrame {

	int old_x, new_x, old_y, new_y;

	Color color = new Color(0,0,0);
	BufferedImage buff = new BufferedImage(900,700, BufferedImage.TYPE_INT_RGB);
	Graphics gBuff = buff.getGraphics(); 


public SimpleDraw() {
	gBuff.setColor(Color.white);
	gBuff.fillRect(0,0,900,700); 
	setTitle("                                                       Have Fun With This Drawing Pad");
	setSize(900,700);

	MouseListener listOne = new MouseListener();
	addMouseMotionListener(listOne);
	MouseListenerAnother listTwo = new MouseListenerAnother();
	addMouseListener(listTwo);
}


	public void paint(Graphics g) {

	g.drawImage(buff,0,0,null);
}


// When the user moves the mouse

	private class MouseListener extends MouseMotionAdapter {
	public void mouseDragged(MouseEvent event) {

	old_x = new_x;
	old_y = new_y;

	new_x = event.getX();
	new_y = event.getY();

	gBuff.setColor(Color.blue);
	gBuff.drawLine(old_x, old_y, new_x, new_y);

	repaint();

}
}

// When the user has pressed the mouse 


	private class MouseListenerAnother extends MouseAdapter {
	public void mousePressed(MouseEvent event) {

	old_x = new_x = event.getX();
	old_y = new_y = event.getY();
	color = new Color(0,0,0);

	gBuff.setColor(color);
	gBuff.drawLine(old_x, old_y, new_x, new_y); 

	repaint(); 

}
}



	public static void main(String[] args) {
	new SimpleDraw().setVisible(true);
}
}

 
It draws a line from where the old mouse postion was to where the new postion is ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top