Getting started with Java Applet
YY435
Pacific Network Academy
Fourth day
Mouse event Example 1
The source code of this example is as follows:
Import java.applet. *;
Import java.awt. *;
The // said in front of the example, no longer said. ^ _ ^
Public class aa1 extends applet
{
INT CX = 50;
INT CY = 50;
/ / Define the initial position of the mouse, that is, when you have not pressed the string of the mouse will appear.
s position.
Public Boolean MouseDown (Event E, INT X, INT Y)
/ / Define the mouse button, this is a Boolean function, that is, only two of true and false
The status function.
// Parameter E is a mouse prescription parameter, when the mouse is pressed, the E value is true, x, y is the mouse
Location parameters.
{
Cx = x;
CY = Y;
/ / Redefine the location parameters of the string so that the position of the string is where the mouse is pressed.
Repaint ();
// Reopen the screen to update the string position.
Return True;
// Return the function, this function is true.
}
Public void Paint (Graphics G)
/ / Paint function, draw screen functions, browser starts to know how to draw screens from here,
What is drawn?
// This is to draw a string in CX, CY position, making it displayed.
{
g.drawstring ("Where to press the mouse, where I appear!", CX, CY);
}
}
This program, the browser is executed from the PAINT function. When the first call is called, the CX, the CY value is the value defined at the beginning of the program, which then responds to the value when the user is on the screen, executed by mousedown, change after changing , Reopen, so that the purpose of starting.