Link to home
Start Free TrialLog in
Avatar of BrianGEFF719
BrianGEFF719Flag for United States of America

asked on

TextArea Mouse Position


Code produces the following error:

init:
deps-jar:
Compiling 1 source file to C:\Documents and Settings\Brian\MathForum\build\classes
C:\Documents and Settings\Brian\Desktop\nh\mathDesign.java:78: addMouseListener(java.awt.event.MouseListener) in java.awt.Component cannot be applied to (mathDesign)
      textCanvas.addMouseListener( this );
C:\Documents and Settings\Brian\Desktop\nh\mathDesign.java:133: mathDesign.myMouse is not abstract and does not override abstract method mouseExited(java.awt.event.MouseEvent) in java.awt.event.MouseListener
  class myMouse implements MouseListener
2 errors
BUILD FAILED (total time: 0 seconds)




I need to be able to determine when the mouse moves and where the mouse is, and when the mouse clicks and where it is in (x,y) cooridnates or the row,column coordinates, code that would give me the row & col of where the mouse is would be best, if not I can do this myself.



Brian



import java.applet.Applet;
import java.awt.*;          
import java.awt.event.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.FontMetrics;
import java.awt.Font;


public class mathDesign extends Applet  
{  
   /* these variables are used to keep track of what the user is doing */
   
   
   
   int numRows = 25;
   int numCols = 75;
   int txtWidth;
   int txtHeight;
   
   char myCanvas[][] = new char [numRows][numCols];
   char myTmpCanvas[][] = new char [numRows][numCols];
   
   Button    drawLineButton           = new Button("Draw Line");
   Button    insertLabelButton        = new Button("Insert Label");
   
   TextArea  textCanvas               = new TextArea(
                                            "Loading...",
                                            numRows ,
                                             numCols+1,
                                            TextArea.SCROLLBARS_VERTICAL_ONLY
                                          );
   
   Label fillChar_label = new Label("Fill Char: ");
   TextField fillChar_text = new TextField(1);
   Label labelStart = new Label("Label: ");
   TextField labelStart_text = new TextField(1);

 
     
   public void init( )
   {      
      add(drawLineButton);
      //drawLineButton.setEnabled(false);
     
           
      /* dont allow user to edit the text box */
      /* all functions should be available to allow user to make changes */
      textCanvas.setEditable(false);
      textCanvas.setFont(Font.decode("Monospaced"));

   
     
      add(fillChar_label);
     
      fillChar_text.setText("*");
      add(fillChar_text);
      /* get font metrics */
     
           
     
      addNewLine( );
      add(insertLabelButton);
     
      labelStart_text.setText("A");
      add(labelStart);
      add(labelStart_text);
     
      addNewLine( );
      addNewLine( );
     
      add(textCanvas);
     
      addNewLine( );
     
      drawLineButton.addActionListener(new drawLineListner( ));
      textCanvas.setText("Loading MathDesign applet...");
      textCanvas.addMouseListener( this );
     
      clearCanvas(myCanvas,numRows,numCols);
      copyCanvas(myTmpCanvas,myCanvas,numRows,numCols);
      displayCanvas(myCanvas,numRows,numCols);
     
   }
 
   //will copy what is in the 2 dimensional array into the text area
   //it will stop at row r.
   private void displayCanvas(char theCanvas[][],int r,int c)
   {
       StringBuilder tmp = new StringBuilder();
         for (int i = 0; i < (r);++i )
         {
             tmp.append(theCanvas[i]).append("\n");
          }
     
       textCanvas.setText(tmp.toString());
   }
   
   
   
   private void clearCanvas(char theCanvas[][],int r, int c)
   {
          for (int i = 0; i < (r); ++i )
          {
            for (int j = 0; j < (c); ++j )
            {
                    theCanvas[i][j] = 'W';
            }
          }
   }
   
   /* ASSUMES BOTH ARRAYS HAVE THE SAME SIZE!!!!!! */
   private void copyCanvas(char dest[][], char from[][], int r, int c)
   {
         for (int i = 0; i < (r); ++i )
          {
            for (int j = 0; j < (c); ++j )
            {
                dest[i][j] = from[i][j];
            }
          }
             
   }
   
   
   class drawLineListner implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {
      }                  
   }
   
  class myMouse implements MouseListener
   {
      public void actionPerformed(MouseEvent e)
      {
      }                  
   }
   private void addHorizontalLine(Color c)
   {  
      Canvas line = new Canvas( );
      line.setSize(10000,1);
      line.setBackground(c);
      add(line);
   }
   
   private void addNewLine( )
   {  
      addHorizontalLine(getBackground( ));
   }

     
}
Avatar of Mick Barry
Mick Barry
Flag of Australia image

add the following methods to your MouseListener

public void mouseClicked(MouseEvent event)
{
}

public void mouseEntered(MouseEvent event)
{
}

public void mouseExited(MouseEvent event)
{
}

public void mousePressed(MouseEvent event)
{
 }

public void mouseReleased(MouseEvent event)
{
}

To be notified of mouse movement you will also need to impleement MouseMotionListener

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/event/MouseMotionListener.html


some background on implementing mouse listeners

http://java.sun.com/docs/books/tutorial/post1.0/ui/mouselistener.html
Avatar of BrianGEFF719

ASKER

Yah, i figured that out a few minutes ago...below is my current code, however, the problem I have with it is this, it doesnt get the columns correctly. I was trying to have 25 cols....however, only 21 are displayed...


import java.applet.Applet;
import java.awt.*;          
import java.awt.event.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.FontMetrics;
import java.awt.Font;


public class mathDesign extends Applet  
{  
   /* these variables are used to keep track of what the user is doing */
   boolean allowedToDraw = false;
   boolean drawingLine = false;
   int drawingObject = 0;
   
   
   int numRows = 25;
   int numCols = 75;
   int textWidth;
   int textHeight;
   
   char myCanvas[][] = new char [numRows][numCols];
   char myTmpCanvas[][] = new char [numRows][numCols];
   
   Button    drawLineButton           = new Button("Draw Line");
   Button    insertLabelButton        = new Button("Insert Label");
   
   TextArea  textCanvas               = new TextArea(
                                            "Loading...",
                                            numRows ,
                                             numCols+1,
                                            TextArea.SCROLLBARS_VERTICAL_ONLY
                                          );
   Label statusHeader = new Label("Status: ");
   Label statusLabel = new Label("");
   
   Label fillChar_label = new Label("Fill Char: ");
   TextField fillChar_text = new TextField(1);
   Label labelStart = new Label("Label: ");
   TextField labelStart_text = new TextField(1);

 
     
   public void init( )
   {      
      add(drawLineButton);
      //drawLineButton.setEnabled(false);
     
           
      /* dont allow user to edit the text box */
      /* all functions should be available to allow user to make changes */
      textCanvas.setEditable(false);
      textCanvas.setFont(Font.decode("Monospaced"));

   
     
      add(fillChar_label);
     
      fillChar_text.setText("*");
      add(fillChar_text);
     
      /* get font metrics */
      FontMetrics currentMetrics = getFontMetrics(Font.decode("Monospaced"));
      textWidth = currentMetrics.charWidth('A');
      textHeight = currentMetrics.getHeight();
           
     
      addNewLine( );
      add(insertLabelButton);
     
      labelStart_text.setText("A");
      add(labelStart);
      add(labelStart_text);
      addNewLine( );
     
      statusHeader.setAlignment(statusHeader.LEFT);
      add(statusHeader);
     
      statusLabel.setAlignment(statusLabel.LEFT);
      statusLabel.setSize(100,10);
      add(statusLabel);
     
      addNewLine( );
     
      add(textCanvas);
     
      addNewLine( );
     
      drawLineButton.addActionListener(new drawLineListner( ));
      textCanvas.setText("Loading MathDesign applet...");
      textCanvas.addMouseListener( new myMouseListner() );
      textCanvas.addMouseMotionListener(new myMouseMotion() );
     
      clearCanvas(myCanvas,numRows,numCols);
      copyCanvas(myTmpCanvas,myCanvas,numRows,numCols);
      displayCanvas(myCanvas,numRows,numCols);
      setStatus("Ready");
   }
 
   private void setStatus(String myStatus)
   {
       statusLabel.setText(myStatus);      
   }
   
   //will copy what is in the 2 dimensional array into the text area
   //it will stop at row r.
   private void displayCanvas(char theCanvas[][],int r,int c)
   {
       StringBuilder tmp = new StringBuilder();
         for (int i = 0; i < (r);++i )
         {
             tmp.append(theCanvas[i]).append("\n");
          }
     
       textCanvas.setText(tmp.toString());
   }
   
   
   
   private void clearCanvas(char theCanvas[][],int r, int c)
   {
          for (int i = 0; i < (r); ++i )
          {
            for (int j = 0; j < (c); ++j )
            {
                    theCanvas[i][j] = 'W';
            }
          }
   }
   
   /* ASSUMES BOTH ARRAYS HAVE THE SAME SIZE!!!!!! */
   private void copyCanvas(char dest[][], char from[][], int r, int c)
   {
         for (int i = 0; i < (r); ++i )
          {
            for (int j = 0; j < (c); ++j )
            {
                dest[i][j] = from[i][j];
            }
          }
             
   }
   
   
   class drawLineListner implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {
          setStatus("Ready to draw line, click a point to start drawing.");
          allowedToDraw = true;
      }                  
   }
   
  class myMouseListner implements MouseListener
   {
    public void mouseClicked (MouseEvent me)
    {
        a(me);
   
    }
    public void mousePressed (MouseEvent me) {}
    public void mouseReleased (MouseEvent me) {}  
    public void mouseEntered (MouseEvent me) {}
    public void mouseExited (MouseEvent me) {}
   }
 
  class myMouseMotion implements MouseMotionListener
  {
      public void mouseMoved(MouseEvent me)
      {
           if (!allowedToDraw)
               a(me);  
      }
      public void mouseDragged(MouseEvent me){}
  }
 
  private void a(MouseEvent m)
  {
        StringBuilder tmp = new StringBuilder();
         int xpos = m.getX();
         int ypos =m.getY();
         int row = getRow(m);
         int col = getCol(m);
         
         tmp.append(xpos);
         tmp.append(",");
         tmp.append(ypos);
         tmp.append("(");
         tmp.append(row);
         tmp.append(",");
         tmp.append(col);
         tmp.append(")");
     
         setStatus(tmp.toString());
     
     
  }
 
   private int getRow(MouseEvent m)
   {
       int y = m.getY();
       int row = 0;
       if (textHeight!=0)
        row = y/textHeight;
       return row;
   }
   
   private int getCol(MouseEvent m)
   {
       int x = m.getX();
       int col = 0;
       if (textHeight!=0)
        col = x/textWidth;
       return col;
   }
   private void addHorizontalLine(Color c)
   {  
      Canvas line = new Canvas( );
      line.setSize(10000,1);
      line.setBackground(c);
      add(line);
   }
   
   private void addNewLine( )
   {  
      addHorizontalLine(getBackground( ));
   }

     
}
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial