Results 1 to 3 of 3

Thread: drawing on Jpanel with mouseMotionListener{Resolved}

Threaded View

  1. #1

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    drawing on Jpanel with mouseMotionListener{Resolved}

    I have been trying to make something that draws on the application whever the mouse goes but I cant seem to get the code to make it draw. I have worked for hours on this trying diffrent ways. anyways here is what i tryed last:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;


    public class MouseDraw extends JFrame implements
    {

    public MouseDraw()
    {
    MouseDrawPanel mdp = new MouseDrawPanel();
    mdp.addMouseMotionListener(this);
    FlowLayout flo = new FlowLayout();
    Container pane = getContentPane();
    pane.setLayout(flo);
    pane.add(mdp);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    super("Draw");
    setSize(100,100,300,300);
    setVisible(true);
    }



    public static void main(String[] args)
    {
    MouseDraw md = new MouseDraw();
    }
    }

    class MouseDrawPanel extends JPanel implements MouseMotionListener
    {
    int x;
    int y;
    public void mouseMoved(MouseEvent e)
    {
    x = e.getX();
    y = e.getY();
    }
    public void mouseDragged(MouseEvent e)
    {
    }

    public void paintComponent(Graphics comp)
    {
    super.paintComponent(comp);
    Graphics2D comp2D = (Graphics2D)comp;
    comp2D.setColor(Color.red);
    comp2D.fillRect(x,y,20,20);
    }
    }







    can anybody get this to work?????
    Last edited by System_Error; Jul 9th, 2004 at 06:43 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width