BrianGEFF719
asked on
TextArea Mouse Position
Code produces the following error:
init:
deps-jar:
Compiling 1 source file to C:\Documents and Settings\Brian\MathForum\b
C:\Documents and Settings\Brian\Desktop\nh\
textCanvas.addMouseListene
C:\Documents and Settings\Brian\Desktop\nh\
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.MouseListen
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_VERTIC
);
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.setEnable
/* dont allow user to edit the text box */
/* all functions should be available to allow user to make changes */
textCanvas.setEditable(fal
textCanvas.setFont(Font.de
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.addActionLi
textCanvas.setText("Loadin
textCanvas.addMouseListene
clearCanvas(myCanvas,numRo
copyCanvas(myTmpCanvas,myC
displayCanvas(myCanvas,num
}
//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]).a
}
textCanvas.setText(tmp.toS
}
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(ActionEven
{
}
}
class myMouse implements MouseListener
{
public void actionPerformed(MouseEvent
{
}
}
private void addHorizontalLine(Color c)
{
Canvas line = new Canvas( );
line.setSize(10000,1);
line.setBackground(c);
add(line);
}
private void addNewLine( )
{
addHorizontalLine(getBackg
}
}
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.MouseListen er;
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_VERTIC AL_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.setEnable d(false);
/* dont allow user to edit the text box */
/* all functions should be available to allow user to make changes */
textCanvas.setEditable(fal se);
textCanvas.setFont(Font.de code("Mono spaced"));
add(fillChar_label);
fillChar_text.setText("*") ;
add(fillChar_text);
/* get font metrics */
FontMetrics currentMetrics = getFontMetrics(Font.decode ("Monospac ed"));
textWidth = currentMetrics.charWidth(' A');
textHeight = currentMetrics.getHeight() ;
addNewLine( );
add(insertLabelButton);
labelStart_text.setText("A ");
add(labelStart);
add(labelStart_text);
addNewLine( );
statusHeader.setAlignment( statusHead er.LEFT);
add(statusHeader);
statusLabel.setAlignment(s tatusLabel .LEFT);
statusLabel.setSize(100,10 );
add(statusLabel);
addNewLine( );
add(textCanvas);
addNewLine( );
drawLineButton.addActionLi stener(new drawLineListner( ));
textCanvas.setText("Loadin g MathDesign applet...");
textCanvas.addMouseListene r( new myMouseListner() );
textCanvas.addMouseMotionL istener(ne w myMouseMotion() );
clearCanvas(myCanvas,numRo ws,numCols );
copyCanvas(myTmpCanvas,myC anvas,numR ows,numCol s);
displayCanvas(myCanvas,num Rows,numCo ls);
setStatus("Ready");
}
private void setStatus(String myStatus)
{
statusLabel.setText(myStat us);
}
//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]).a ppend("\n" );
}
textCanvas.setText(tmp.toS tring());
}
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(ActionEven t 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(getBackg round( ));
}
}
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.MouseListen
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_VERTIC
);
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.setEnable
/* dont allow user to edit the text box */
/* all functions should be available to allow user to make changes */
textCanvas.setEditable(fal
textCanvas.setFont(Font.de
add(fillChar_label);
fillChar_text.setText("*")
add(fillChar_text);
/* get font metrics */
FontMetrics currentMetrics = getFontMetrics(Font.decode
textWidth = currentMetrics.charWidth('
textHeight = currentMetrics.getHeight()
addNewLine( );
add(insertLabelButton);
labelStart_text.setText("A
add(labelStart);
add(labelStart_text);
addNewLine( );
statusHeader.setAlignment(
add(statusHeader);
statusLabel.setAlignment(s
statusLabel.setSize(100,10
add(statusLabel);
addNewLine( );
add(textCanvas);
addNewLine( );
drawLineButton.addActionLi
textCanvas.setText("Loadin
textCanvas.addMouseListene
textCanvas.addMouseMotionL
clearCanvas(myCanvas,numRo
copyCanvas(myTmpCanvas,myC
displayCanvas(myCanvas,num
setStatus("Ready");
}
private void setStatus(String myStatus)
{
statusLabel.setText(myStat
}
//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]).a
}
textCanvas.setText(tmp.toS
}
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(ActionEven
{
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(getBackg
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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