Mouse in a applet

M

Mark

I am trying to make an applet so that when my mouse enters the applet
box, the text will hover around it until it leaves the applet box. I
have tried using loops to attain this but I have had no luck. Can
someone please explain how I can do this?


import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class ClickName extends Applet implements MouseListener
{
//variable named "square" with data type from Square.class
private Square square = null;
private static final int WIDH = 7;

public void init()
{
addMouseListener(this);
}

public void paint(Graphics g)
{
//draw a black border and a white background
g.setColor(Color.black);
g.fillRect(0, 0, getSize().width - 1, getSize().height - 1);
g.setColor(Color.black);
g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);

//draw the spot
g.setColor(Color.orange);
if (square != null)
{
//draw a rectangle and send to graphics program
g.drawString("Mark Waddoups", square.x - WIDH, square.y - WIDH);
}
}

public void mouseEntered(MouseEvent event)
{
int x = 1;
if (square == null)
{
square = new Square(WIDH);
}
square.x = event.getX();
square.y = event.getY();
repaint();
}

public void mousePressed(MouseEvent event) {}
public void mouseReleased(MouseEvent event) {}
public void mouseClicked(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
}
 
I

IchBin

Mark said:
I am trying to make an applet so that when my mouse enters the applet
box, the text will hover around it until it leaves the applet box. I
have tried using loops to attain this but I have had no luck. Can
someone please explain how I can do this?


import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class ClickName extends Applet implements MouseListener
{
//variable named "square" with data type from Square.class
private Square square = null;
private static final int WIDH = 7;

public void init()
{
addMouseListener(this);
}

public void paint(Graphics g)
{
//draw a black border and a white background
g.setColor(Color.black);
g.fillRect(0, 0, getSize().width - 1, getSize().height - 1);
g.setColor(Color.black);
g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);

//draw the spot
g.setColor(Color.orange);
if (square != null)
{
//draw a rectangle and send to graphics program
g.drawString("Mark Waddoups", square.x - WIDH, square.y - WIDH);
}
}

public void mouseEntered(MouseEvent event)
{
int x = 1;
if (square == null)
{
square = new Square(WIDH);
}
square.x = event.getX();
square.y = event.getY();
repaint();
}

public void mousePressed(MouseEvent event) {}
public void mouseReleased(MouseEvent event) {}
public void mouseClicked(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
}
Be nice to see your Square.java to complete a compilation.

--


Thanks in Advance...
IchBin
__________________________________________________________________________

'The meeting of two personalities is like the contact of two chemical
substances:
if there is any reaction, both are transformed.'
- Carl Gustav Jung, (1875-1961), psychiatrist and psychologist
 
I

IchBin

Andrew said:
To the OP. I agree, check here for tips on examples.
<http://www.physci.org/codes/sscce.jsp>

It would also be nice if you (IchBin) trimmed some of the
55 lines of the message to which you are responding.
<http://www.physci.org/codes/javafaq.jsp#netiquette>
Caught again.. Keep on forgetting about the 20mph speed limit..

Sorry

IchBin
__________________________________________________________________________

'The meeting of two personalities is like the contact of two chemical
substances:
if there is any reaction, both are transformed.'
- Carl Gustav Jung, (1875-1961), psychiatrist and psychologist
 
M

Michael Wong

Mark said:
I am trying to make an applet so that when my mouse enters the applet
box, the text will hover around it until it leaves the applet box. I
have tried using loops to attain this but I have had no luck. Can
someone please explain how I can do this?


You'll want to have your applet implement the MouseMotionListener interface
and use the "mouseMoved(MouseEvent)" method like you're using the
"mouseEntered" method now. Remember to use the "mouseExited" method (of
MouseListener interface) if you want to clear the text when the mouse leaves
the applet.

You should pay attention to the other posts about posting your code. You
should almost never post something that doesn't compile. More importantly,
learn to use the Java documentation and the Java tutorials found at
http://java.sun.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top