Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2004

Tip: Looking for answers? Try searching our database.

Newbie MouseListener Problem

Thread view: 
Cammy - 09 Apr 2004 21:49 GMT
Just started taking a class in java, and finding it pretty interesting, but
very difficult. I'm trying to write some code that will allow me to move a
counter on a chessboard. Got the board and counter setup (not very well),
but the counter won't move. Thanks in advance for any help.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Romeo extends Applet implements MouseListener,
MouseMotionListener {

private Point snail1, mouse;
private int select;

public void init() {

this.addMouseMotionListener(this);
this.addMouseListener(this);
select = 0;
snail1 = new Point (25,25);
mouse = new Point();
}

public void paint(Graphics g)
{
drawRect(g);
g.setColor (Color.red);
g.fillOval (snail1.x, snail1.y, 30,30);

}

public void mouseDragged(MouseEvent e) {
mouse = e.getPoint();
// continuously change the coordinates of the selected counter
if (select == 1) snail1 = mouse;
repaint();
}

public void mousePressed(MouseEvent e) {
//select a counter using the mouse
mouse = e.getPoint();

if (mouse.x > snail1.x - 5 && mouse.x < snail1.x + 5 &&
mouse.y > snail1.y - 5 && mouse.y < snail1.y + 5) select = 1;
}

// required for the interface
public void mouseClicked(MouseEvent event){}
public void mouseReleased(MouseEvent event){}
public void mouseEntered(MouseEvent event){}
public void mouseExited(MouseEvent event){}
public void mouseMoved(MouseEvent event){}

public void drawRect(Graphics g) {

g.setColor (Color.black);
g.fillRect (0,0,50,50);
g.drawRect (50,0,50,50);
g.fillRect (100,0,50,50);
g.drawRect (150,0,50,50);
g.fillRect (200,0,50,50);
g.drawRect (250,0,50,50);
g.fillRect (300,0,50,50);
g.drawRect (350,0,50,50);

g.drawRect (0,50,50,50);
g.fillRect (50,50,50,50);
g.drawRect (100,50,50,50);
g.fillRect (150,50,50,50);
g.drawRect (200,50,50,50);
g.fillRect (250,50,50,50);
g.drawRect (300,50,50,50);
g.fillRect (350,50,50,50);

g.fillRect (0,100,50,50);
g.drawRect (50,100,50,50);
g.fillRect (100,100,50,50);
g.drawRect (150,100,50,50);
g.fillRect (200,100,50,50);
g.drawRect (250,100,50,50);
g.fillRect (300,100,50,50);
g.drawRect (350,100,50,50);

g.drawRect (0,150,50,50);
g.fillRect (50,150,50,50);
g.drawRect (100,150,50,50);
g.fillRect (150,150,50,50);
g.drawRect (200,150,50,50);
g.fillRect (250,150,50,50);
g.drawRect (300,150,50,50);
g.fillRect (350,150,50,50);

g.fillRect (0,200,50,50);
g.drawRect(50,200,50,50);
g.fillRect(100,200,50,50);
g.drawRect (150,200,50,50);
g.fillRect (200,200,50,50);
g.drawRect (250,200,50,50);
g.fillRect (300,200,50,50);
g.drawRect (350,200,50,50);

g.drawRect (0,250,50,50);
g.fillRect (50,250,50,50);
g.drawRect (100,250,50,50);
g.fillRect (150,250,50,50);
g.drawRect (200,250,50,50);
g.fillRect (250,250,50,50);
g.drawRect (300,250,50,50);
g.fillRect (350,250,50,50);

g.fillRect (0,300,50,50);
g.drawRect (50,300,50,50);
g.fillRect (100,300,50,50);
g.drawRect (150,300,50,50);
g.fillRect (200,300,50,50);
g.drawRect (250,300,50,50);
g.fillRect (300,300,50,50);
g.drawRect (350,300,50,50);

g.drawRect (0,350,50,50);
g.fillRect (50,350,50,50);
g.drawRect (100,350,50,50);
g.fillRect (150,350,50,50);
g.drawRect (200,350,50,50);
g.fillRect (250,350,50,50);
g.drawRect (300,350,50,50);
g.fillRect (350,350,50,50);

}
}
Alex Hunsley - 10 Apr 2004 02:46 GMT
> Just started taking a class in java, and finding it pretty interesting, but
> very difficult. I'm trying to write some code that will allow me to move a
> counter on a chessboard. Got the board and counter setup (not very well),
> but the counter won't move. Thanks in advance for any help.

[snip code]

Ooh cammy, you're so close! It actually works, it's just you've slightly
misprogrammed the drawing of the counter.

Run your applet as it is, but instead of clicking and dragging the
centre of the game piece, imagine the piece is square and try clicking
and dragging the top left edge of this 'square' - it works!

The reason for your problem is that you're assuming that java's draw
oval command takes the centre point and the size of the oval. What it
actually takes is the top left corner of the oval and the size! Slightly
different (and slightly counter-intuitive to my brain as well!). This
accounts for the different behaviour.

Try drawing the piece like this:

        g.fillOval(snail1.x - 15, snail1.y - 15, 30, 30);

then you should find the piece appears where you expected it to, and it
drags as you'd expect.

Btw, one last thing, once you've clicked the piece, you can then let go
of the mouse and click anywhere and drag, and it causes the piece to be
dragged, which isn't very desirable, so to fix this try having your
mouseReleased method as follows:

    public void mouseReleased(MouseEvent event) {
        // mouse released, so forget about the
        // selected piece!
        select = 0;
    }

Also, I notice you're drawing your board quite laboriously, square by
square explicitly. It's possible to draw in it a much simpler fashion.
(Hint: two for loops, one inside the other, one going over X square
coordinates, the other over the Y.)

alex


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.