Mouse E


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Mouse E

  1. #1
    Join Date
    Aug 2005
    Posts
    14

    Mouse E

    at the moment my applet displays a big black circle but i want the applet to draw a white circle where the mouse is. the mouse will point to the centre of the circle. i want to decide how big my circle will be where i want the top left corner of the mouse to be.

    import java.applet.Applet;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.geom.Ellipse2D;
    import java.awt.Rectangle;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseEvent;
    import java.awt.Color;


    public class MouseE extends Applet
    {
    public MouseE()
    {
    x1 = -1; // initialise (x1,y1) and (x2,y2) to -1
    y1 = -1; // the value '-1' is OUTSIDE the applet
    x2 = -1; // so, if, in the program, these variables are less than zero
    y2 = -1; // we KNOW that they don't currently represent a point on the image

    class MousePressListener implements MouseListener
    {

    // do-nothing methods
    public void mousePressed(MouseEvent event){}
    public void mouseReleased(MouseEvent event){}
    public void mouseClicked(MouseEvent event){}
    public void mouseExited(MouseEvent event){}

    public void mouseEntered(MouseEvent event)
    {
    x1 = event.getX();
    y1 = event.getY();

    repaint();
    }

    }

    MouseListener listener = new MousePressListener();
    addMouseListener(listener);
    }

    public void paint(Graphics g)
    {
    Graphics2D g2 = (Graphics2D) g;
    Ellipse2D.Double circle = new Ellipse2D.Double(0,0, 300, 300);
    g2.draw(circle);
    g2.fill(circle);
    }

    public double x1;
    public double y1;
    public double x2;
    public double y2;


    }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    The circle parameters represent the topLeft and the diameter(s), so if your
    mousePosition is going to be at the center then your circle should be drawn, in the
    appropriate mouseEvent(mousePressed, mouseMoved) like:

    g.drawOval(event.getX()-diameter/2, event.getY()-diameter/2, diameter, diameter);

    For an ellipse you must use diameterX & diameterY, this should work if you use the
    Graphics2D also.
    eschew obfuscation

Similar Threads

  1. Capturing Mouse Movements outside of screen resolution
    By Chris Eastwood in forum VB Classic
    Replies: 4
    Last Post: 03-28-2002, 02:26 PM
  2. Mouse Movement Playback
    By Eugene in forum VB Classic
    Replies: 4
    Last Post: 12-19-2000, 01:52 PM
  3. Mouse Movement Playback
    By Eugene in forum VB Classic
    Replies: 0
    Last Post: 12-19-2000, 12:13 PM
  4. Mouse Hook
    By Michael Culley in forum VB Classic
    Replies: 2
    Last Post: 12-10-2000, 02:56 PM
  5. Wheel Mouse In Access
    By Paul Harris in forum VB Classic
    Replies: 0
    Last Post: 03-13-2000, 05:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center