Mouse motion listener

+1 kieran hester · February 7, 2015
im trying to create a drawing application and am trying to add a mousemotion listener to my paint canvas,
i cant seem to get it to work, can someone tell me what is wrong with my code and show me what needs to be done to fix it?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.io.*;
import java.awt.Color.*;
import java.awt.Graphics.*;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;

public class DrawingApplication extends JFrame
{
    private final int canvasheight = 640;
    private final int canvaswidth = 800;
    private final int controlwidth = 200;
    private final int maheight = 200;

    private String coordinatetext;

    private Canvas canvas;    
    private JPanel controlpanel;
    private JLabel coordinatesLabel;
    private JRadioButton lineRadioButton, ovalRadioButton, rectangleRadioButton, freehandRadioButton;
    private JSlider freehandSizeSlider;
    private JCheckBox fineCheckBox, coarseCheckBox;
    private JButton colourButton, clearButton, animateButton;

    private JTextArea messagearea;    
    private JMenuBar menuBar;

    //
    //CanvasMouseMotionListener canvasMouseMotionListener = new CanvasMouseMotionListener();
    //addMouseMotionListener(this);

    //
    // Drawing area class (inner class).
    class Canvas extends JPanel 
    {
        // Called every time there is a change in the canvas contents.
        
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);        
            draw(g);

        }

    } // end inner class Canvas

    public DrawingApplication()
    {
        setTitle("Drawing Application");
        setLayout(new BorderLayout());

        //drawing canvas

        JPanel canvas = new JPanel();
        canvas.setBackground(Color.white);
        canvas.setBorder(new TitledBorder(new EtchedBorder(), "Canvas"));
        canvas.setPreferredSize(new Dimension(canvaswidth, canvasheight));
        canvas.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
        add(canvas, BorderLayout.CENTER);

        //control panel
        controlpanel = new JPanel();
        controlpanel.setBorder(new TitledBorder(new EtchedBorder(), "Control Panel"));
        controlpanel.setPreferredSize(new Dimension(controlwidth, canvasheight));
        JScrollPane controlPanelScrollPane = new JScrollPane(controlpanel);
        controlPanelScrollPane.setPreferredSize(new Dimension(controlwidth + 30, canvasheight));
        add(controlPanelScrollPane, BorderLayout.LINE_START);    

        // Mouse Coordinates panel
        JPanel coordinatesPanel = new JPanel();
        coordinatesPanel.setBorder(new TitledBorder(new EtchedBorder(), "Drawing Position"));
        coordinatesPanel.setPreferredSize(new Dimension(controlwidth - 20, 60));
        coordinatesLabel = new JLabel();
        coordinatesPanel.add(coordinatesLabel);
        controlpanel.add(coordinatesPanel);

        
        // Drawing tools panel
        JPanel drawingToolsPanel = new JPanel();
        drawingToolsPanel.setPreferredSize(new Dimension(controlwidth - 20, 140));
        drawingToolsPanel.setLayout(new 

            GridLayout(0, 1));
        drawingToolsPanel.setBorder(new 

            TitledBorder(new 

                EtchedBorder(), "Drawing Tools"));
        controlpanel.add(drawingToolsPanel);

        // Freehand trace size slider
        JPanel freehandSliderPanel = new JPanel();
        freehandSliderPanel.setPreferredSize(new Dimension(controlwidth - 20, 90));
        drawingToolsPanel.setLayout(new 

            GridLayout(0, 1));
        freehandSliderPanel.setBorder(new 

            TitledBorder(new 

                EtchedBorder(), "Freehand Size"));
        controlpanel.add(freehandSliderPanel);

        // Grid Panel
        JPanel gridPanel = new JPanel();
        gridPanel.setPreferredSize(new Dimension(controlwidth - 20, 80));
        gridPanel.setLayout(new 

            GridLayout(0, 1));
        gridPanel.setBorder(new 

            TitledBorder(new 

                EtchedBorder(), "Grid"));
        controlpanel.add(gridPanel);

        // Colour Panel
        JPanel colourPanel = new JPanel();
        colourPanel.setPreferredSize(new Dimension(controlwidth - 20, 90));
        colourPanel.setBorder(new 

            TitledBorder(new 

                EtchedBorder(), "Colour"));
        colourButton = new 

        JButton();
        colourButton.setPreferredSize(new 

            Dimension(50, 50));
        colourPanel.add(colourButton);
        controlpanel.add(colourPanel);

        // Clear button
        clearButton = new JButton("Clear Canvas");
        clearButton.setPreferredSize(new 

            Dimension(controlwidth - 20, 50));
        controlpanel.add(clearButton);

        // Animate button 
        animateButton = new JButton("Animate");
        animateButton.setPreferredSize(new 

            Dimension(controlwidth - 20, 50));
        controlpanel.add(animateButton);

        //message board
        messagearea = new JTextArea();
        messagearea.setEditable(false);
        messagearea.setBackground(canvas.getBackground());
        JScrollPane textAreaScrollPane = new JScrollPane(messagearea);
        textAreaScrollPane.setBorder(new TitledBorder(new 

                EtchedBorder(), "Message Area"));
        textAreaScrollPane.setPreferredSize(new 

            Dimension(controlwidth + canvaswidth, maheight));

        add(textAreaScrollPane, BorderLayout.PAGE_END);

        //menu bar
        menuBar = new JMenuBar();
        menuBar.setBackground(Color.red);
        JMenu fileMenu = new JMenu("File");
        JMenu helpMenu = new JMenu("Help");
        JMenuItem fileSaveMenuItem = new JMenuItem("Save");
        JMenuItem fileLoadMenuItem = new JMenuItem("Load");
        JMenuItem fileExitMenuItem = new JMenuItem("Exit");
        JMenuItem helpAboutMenuItem = new JMenuItem("About");
        fileMenu.add(fileSaveMenuItem);
        fileMenu.add(fileLoadMenuItem);
        fileMenu.addSeparator();
        fileMenu.add(fileExitMenuItem);
        helpMenu.add(helpAboutMenuItem);
        menuBar.add(fileMenu);
        menuBar.add(helpMenu);
        add(menuBar, BorderLayout.PAGE_START);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        pack();

        setVisible(true);
    }
    
    CanvasMouseMotionListener mouselistener = new CanvasMouseMotionListener();
    Canvas.addMouseMotionListener(mouselistener);

    private class CanvasMouseMotionListener implements MouseMotionListener
    {

        public void mouseMoved(MouseEvent e)
        {
            coordinatesLabel.setText("Mouse position: "+e.getX()+"x, "+e.getY() +"y");
        }

        public void mouseDragged(MouseEvent e)
        {
            ;
        }
    }
    // Called by the canvas' paintComponent method
    void draw(Graphics g)
    {

        g.drawLine(20, 50, 100, 210);
    } // end draw method   

    public static void main(String args[])
    {
        DrawingApplication drawingApplicationInstance = new DrawingApplication();
    }

}

Post a Reply

Replies

Oldest  Newest  Rating
0 Kevin Faust · February 9, 2015
You have a random semi colon here?




public void mouseDragged(MouseEvent e)
{
;
}
  • 1

Java / Android Development

107,006 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator