Java by Example - using mouseMove and mouseDrag
We have not explored the funcion mouseMove yet, which is a useful and important one. It is called whenever you move the mouse inside the applet and can also detect, at which coordinates the cursor is. We use this function in the following applet, in order to display the actual mouse coordinates and to detect whether the mouse cursor is inside one of three colored rectangles. We use the Rectangle class here, which provides us with a convenient method inside(int x, int y), to tell whether a given point is inside the rectangle or not. Clicking just paints the background in a different color here.
//Sourcecode
import java.awt.*;
import java.applet.*;
public class Project27 extends Applet
{
Image Buffer;
Graphics gBuffer;
int x, y;
boolean click, mouseInside, insideR1, insideR2, insideR3;
//for reference see package java.awt class Rectangle
Rectangle r1, r2, r3;
public void init()
{
Buffer=createImage(size().width,size().height);
gBuffer=Buffer.getGraphics();
//create 3 rectangle objects
r1=new Rectangle(20,70,80,100);
r2=new Rectangle(110,70,80,100);
r3=new Rectangle(200,70,80,100);
}
public void drawStuff()
{
//if mouse was clicked, paint background magenta, otherwise black
if(click)
gBuffer.setColor(Color.magenta);
else
gBuffer.setColor(Color.black);
gBuffer.fillRect(0,0,size().width,size().height);
//paint the 3 rectangles
gBuffer.setColor(Color.blue);
gBuffer.fillRect(r1.x, r1.y, r1.width, r1.height);
gBuffer.setColor(Color.green);
gBuffer.fillRect(r2.x, r2.y, r2.width, r2.height);
gBuffer.setColor(Color.red);
gBuffer.fillRect(r3.x, r3.y, r3.width, r3.height);
//draw the mouse coordinates
if (mouseInside)
{
gBuffer.setFont(new Font("Helvetica",Font.BOLD,12));
gBuffer.setColor(Color.white);
gBuffer.drawString ("X: "+x,30,30);
gBuffer.drawString ("Y: "+y,30,50);
}
else
{
gBuffer.setFont(new Font("Helvetica",Font.PLAIN,14));
gBuffer.setColor(Color.orange);
gBuffer.drawString ("Move the mouse inside the applet!",40,30);
}
//since we need this font several times, we create it here
Font bigFont=new Font("Helvetica",Font.BOLD,45);
if (insideR1)
{
gBuffer.setFont(bigFont);
gBuffer.setColor(Color.blue);
gBuffer.drawString ("BLUE",120,50);
gBuffer.setColor(Color.white);
gBuffer.drawRect(r1.x, r1.y, r1.width-1, r1.height-1);
}
if (insideR2)
{
gBuffer.setFont(bigFont);
gBuffer.setColor(Color.yellow);
gBuffer.drawString ("GREEN",120,50);
gBuffer.setColor(Color.white);
gBuffer.drawRect(r2.x, r2.y, r2.width-1, r2.height-1);
}
if (insideR3)
{
gBuffer.setFont(bigFont);
gBuffer.setColor(Color.red);
gBuffer.drawString ("RED",120,50);
gBuffer.setColor(Color.white);
gBuffer.drawRect(r3.x, r3.y, r3.width-1, r3.height-1);
}
}
//detects any mouse move within our applet
public boolean mouseMove(Event evt,int x,int y)
{
//submit the mouse coordinates to our instance variables
this.x=x;
this.y=y;
//"inside" is a method of the Rectangle class
if(r1.inside(x,y))
insideR1=true;
else
insideR1=false;
if(r2.inside(x,y))
insideR2=true;
else
insideR2=false;
if(r3.inside(x,y))
insideR3=true;
else
insideR3=false;
drawStuff();
repaint();
return true;
}
public boolean mouseEnter(Event evt,int x,int y)
{
mouseInside=true;
return true;
}
public boolean mouseExit(Event evt,int x,int y)
{
mouseInside=false;
repaint(500);
return true;
}
public boolean mouseDown(Event evt,int x,int y)
{
click=true;
repaint();
return true;
}
public boolean mouseUp(Event evt,int x,int y)
{
click=false;
repaint();
return true;
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
drawStuff();
g.drawImage (Buffer,0,0, this);
}
}
|
This applet enhances our last one, by making the colored boxes objects of a self-defined class SwitchRectangle. This class features the possibility to change the colors of the rectangles by clicking at them. The current color of the three SwitchRectangle objects is also reported back, to enable drawing the correct color name. The colors are represented by three constant integer variables, which are declared as static final in Java. Such variables must be initialized with a value at the same time they are declared and cannot accidentally be changed during program runtime. In this program I used expressions like if(inside) instead of if(inside==true) and if(!inside) instead of if(inside==false) (inside being a boolean variable). This is shorter and you should get accustomed to use it yourself. Study the sourcecode carefully, even though it might seem like child's play to you. Small examples like this quickly enable you to succeed in bigger projects.
//Sourcecode
import java.awt.*;
import java.applet.*;
class SwitchRectangle
{
public Rectangle r=new Rectangle();
private int color;
//we declare 3 int variables to store color values here
//"static final" are constant variables that must not be changed
//they have to be initialized at the same time they are declared!
static final int RED=1;
static final int GREEN=2;
static final int BLUE=3;
private boolean inside;
Graphics g;
//our constructor, it initializes the rectangle coordinates and color
public SwitchRectangle(int x, int y, int w, int h, int color)
{
r.x=x;
r.y=y;
r.width=w;
r.height=h;
this.color=color;
}
//if mouse is moved inside our rectangle, the flag "inside" is set
public void SubmitMouseCoord(int x, int y)
{
if(r.inside(x, y))
inside=true;
else
inside=false;
}
public void switchColor(int x, int y)
{
if(r.inside(x, y))
{
if(color<BLUE)
color++;
else
color=RED;
}
}
//a method that is not of type void has to return a value.
//the key word "else" is not neccessary here, but can be used
public int getColor()
{
//if the mouse cursor is not inside our rectangle,
//we return 0, otherwise the active color variable value
if(!inside)
return 0;
else
return color;
}
public void paint(Graphics gr)
{
g=gr;
if(color==RED)
g.setColor(Color.red);
else if(color==GREEN)
g.setColor(Color.green);
else
g.setColor(Color.blue);
g.fillRect(r.x, r.y, r.width, r.height);
if(inside)
{
g.setColor(Color.white);
g.drawRect(r.x, r.y, r.width-1, r.height-1);
}
}
}
public class Project28 extends Applet
{
Image Buffer;
Graphics gBuffer;
int x, y;
boolean mouseInside, insideRed, insideGreen, insideBlue;
SwitchRectangle sr1, sr2, sr3;
//the constant color variables have to be declared here too!
static final int RED=1;
static final int GREEN=2;
static final int BLUE=3;
public void init()
{
Buffer=createImage(size().width,size().height);
gBuffer=Buffer.getGraphics();
//create 3 objects of our SwitchRectangle class
sr1=new SwitchRectangle(20,70,80,100,RED);
sr2=new SwitchRectangle(110,70,80,100,GREEN);
sr3=new SwitchRectangle(200,70,80,100,BLUE);
}
public void drawStuff()
{
gBuffer.setColor(Color.black);
gBuffer.fillRect(0,0,size().width,size().height);
//paint the 3 rectangles
sr1.paint(gBuffer);
sr2.paint(gBuffer);
sr3.paint(gBuffer);
//draw the mouse coordinates
if (mouseInside)
{
gBuffer.setFont(new Font("Helvetica",Font.BOLD,12));
gBuffer.setColor(Color.white);
gBuffer.drawString ("X: "+x,30,30);
gBuffer.drawString ("Y: "+y,30,50);
}
else
{
gBuffer.setFont(new Font("Helvetica",Font.PLAIN,14));
gBuffer.setColor(Color.orange);
gBuffer.drawString ("Click the three rectangles!",70,30);
}
//since we need this font several times, we create it here
Font bigFont=new Font("Helvetica",Font.BOLD,45);
if (insideRed)
{
gBuffer.setFont(bigFont);
gBuffer.setColor(Color.red);
gBuffer.drawString ("Red",120,50);
}
if (insideGreen)
{
gBuffer.setFont(bigFont);
gBuffer.setColor(Color.green);
gBuffer.drawString ("Green",120,50);
}
if (insideBlue)
{
gBuffer.setFont(bigFont);
gBuffer.setColor(Color.blue);
gBuffer.drawString ("Blue",120,50);
}
}
//detects any mouse move within our applet
public boolean mouseMove(Event evt,int x,int y)
{
//submit the mouse coordinates to our instance variables
this.x=x;
this.y=y;
sr1.SubmitMouseCoord(x, y);
sr2.SubmitMouseCoord(x, y);
sr3.SubmitMouseCoord(x, y);
detectColor();
repaint();
return true;
}
public boolean mouseEnter(Event evt,int x,int y)
{
mouseInside=true;
return true;
}
public boolean mouseExit(Event evt,int x,int y)
{
mouseInside=false;
repaint(500);
return true;
}
public boolean mouseDown(Event evt,int x,int y)
{
sr1.switchColor(x, y);
sr2.switchColor(x, y);
sr3.switchColor(x, y);
detectColor();
repaint();
return true;
}
//this method detects the color that's under the mouse cursor,
//to tell if it's "Red", "Green" or "Blue"
void detectColor()
{
//if one of our 3 SwitchingRectangles reports RED, we set the flag
//"insideRed" to true, otherwise to false
if(sr1.getColor()==RED||sr2.getColor()==RED||sr3.getColor()==RED)
insideRed=true;
else
insideRed=false;
if(sr1.getColor()==GREEN||sr2.getColor()==GREEN||sr3.getColor()==GREEN)
insideGreen=true;
else
insideGreen=false;
if(sr1.getColor()==BLUE||sr2.getColor()==BLUE||sr3.getColor()==BLUE)
insideBlue=true;
else
insideBlue=false;
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
drawStuff();
g.drawImage (Buffer,0,0, this);
}
}
|
The last mouse method we haven't used yet, is mouseDrag. It is called whenever the mouse is clicked and then moved, holding the mousebutton pressed. We use it here to realize a very simple drawing applet. You can paint by clicking and dragging the mouse. Whenever the mouse changes position, a tiny line is drawn between the last position and the new one. If you want to implement an "eraze" function somewhere, you have to redraw the background once, like here inside the init() method.
//Sourcecode
import java.awt.*;
import java.applet.*;
public class Project29 extends Applet
{
Image Buffer;
Graphics gBuffer;
int old_x, new_x, old_y, new_y;
public void init()
{
Buffer=createImage(size().width,size().height);
gBuffer=Buffer.getGraphics();
gBuffer.setColor(Color.white);
gBuffer.fillRect(0,0,size().width,size().height);
}
public boolean mouseDown(Event evt,int x,int y)
{
old_x=new_x=x;
old_y=new_y=y;
repaint();
return true;
}
public boolean mouseDrag(Event evt,int x,int y)
{
old_x=new_x;
old_y=new_y;
new_x=x;
new_y=y;
repaint();
return true;
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
gBuffer.setColor(Color.black);
gBuffer.drawString("You can draw inside the applet!", 50,20);
gBuffer.setColor(Color.blue);
gBuffer.drawLine(old_x,old_y,new_x,new_y);
g.drawImage (Buffer,0,0, this);
}
}
|
|