Results 1 to 3 of 3

Thread: drawing on Jpanel with mouseMotionListener{Resolved}

  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.

  2. #2
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    repaint.
    PHP Code:
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    public class 
    MouseDraw1 extends JFrame implements MouseMotionListener{
        
    int x,y;
        public 
    MouseDraw1(){
            
    super("MouseDraw1");
            
    setDefaultCloseOperation(EXIT_ON_CLOSE);
            
    setSize(300,300);
            
    addMouseMotionListener(this);
            
    setVisible(true);
        }
        public 
    void mouseDragged(MouseEvent e){}
        public 
    void mouseMoved(MouseEvent e){
            
    x=e.getX();
            
    y=e.getY();
            
    repaint();
        }
        public 
    void paint(Graphics g){
            
    g.setColor(Color.red);
            
    g.fillRect(x,y,20,20);
        }
        public static 
    void main(String[] args){
            new 
    MouseDraw1();
        }


  3. #3

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

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