Hi, im having a major problem with printing text on a JFrame. This is a simple whiteboard I'm doing but i just cannot seem to print the text properly. It prints all the alphabets on the same point (all the alphabets are piled on top of each other) instead of printing it incrementally like "this is a random string". Can anyone please help me???
Here's what I have so far:
[CODE=java] import java.awt.*;
import javax.swing.*;
import java.awt.event. *;
import java.lang.Strin gBuffer;
public class JPanels extends JFrame implements MouseListener, MouseMotionList ener//, KeyListener
{
private Point oldPoint, currentPoint;
private String s;
private TextField message;
private JPanel drawingArea;
private FontMetrics fm;
public void init() {
//initialize the "old" point
oldPoint = new Point(0,0);
Font font = new Font("Helvetica ", Font.BOLD, 20);
setFont(font);
fm = getFontMetrics( font);
}
public static void main(String[] args) {
new JPanels();
}
public JPanels() {
super("Welcome to Whiteboard!");
Container content = getContentPane( );
content.setBack ground(Color.li ghtGray);
JPanel drawingArea = new JPanel();
TextField message = new TextField("Draw away! Click anywhere on canvas to type");
message.setBack ground(Color.gr ay);
message.setEdit able(false);
drawingArea.set PreferredSize(n ew Dimension(500, 400));
drawingArea.set Border (BorderFactory. createLineBorde r (Color.blue, 2));
drawingArea.set Background(Colo r.white);
content.add(dra wingArea, BorderLayout.CE NTER);
content.add(mes sage, BorderLayout.SO UTH);
pack();
this.setVisible (true);
this.setResizab le(true);
drawingArea.add MouseListener(t his);
drawingArea.add MouseMotionList ener(this);
// addKeyListener( this);
}
//when the mouse is pressed, a point is formed
public void mousePressed(Mo useEvent e){
oldPoint = e.getPoint();
String msg = ("Mouse Pressed: (" + oldPoint.x + ", " + oldPoint.y + ")");
System.out.prin tln(msg);
s = "";
}
//mouseDragged will do the line drawing
public void mouseDragged(Mo useEvent e){
currentPoint = e.getPoint();
String msg = "Mouse Dragged: (" + currentPoint.x + ", " + currentPoint.y + ")";
System.out.prin tln(msg);
this.getGraphic s().drawLine(cu rrentPoint.x, currentPoint.y, oldPoint.x, oldPoint.y);
oldPoint = currentPoint;//set the oldPoint to the most recent coordinates
}
//keyTyped will print the character, starting at the last place the mouse was pressed
/*public void keyTyped(KeyEve nt e){
s += e.getKeyChar();
getGraphics().d rawString(s, oldPoint.x, oldPoint.y);
}
*/
public boolean keyDown(Event e, int key) {
String s = String.valueOf( (char)key);
getGraphics().d rawString(s, oldPoint.x, oldPoint.y);
return(record(o ldPoint.x + fm.stringWidth( s), oldPoint.y));
}
protected boolean record(int x, int y) {
oldPoint.x = x;
oldPoint.y = y;
return(true);
}
//abstract methods promised by the MouseListener and MouseMotionList ener interfaces
public void mouseEntered(Mo useEvent e){}
public void mouseExited(Mou seEvent e){}
public void mouseReleased(M ouseEvent e){}
public void mouseClicked(Mo useEvent e){}
public void mouseMoved(Mous eEvent e){}
public void keyReleased(Key Event e){}
public void keyPressed(KeyE vent e){}
}
[/CODE]
Here's what I have so far:
[CODE=java] import java.awt.*;
import javax.swing.*;
import java.awt.event. *;
import java.lang.Strin gBuffer;
public class JPanels extends JFrame implements MouseListener, MouseMotionList ener//, KeyListener
{
private Point oldPoint, currentPoint;
private String s;
private TextField message;
private JPanel drawingArea;
private FontMetrics fm;
public void init() {
//initialize the "old" point
oldPoint = new Point(0,0);
Font font = new Font("Helvetica ", Font.BOLD, 20);
setFont(font);
fm = getFontMetrics( font);
}
public static void main(String[] args) {
new JPanels();
}
public JPanels() {
super("Welcome to Whiteboard!");
Container content = getContentPane( );
content.setBack ground(Color.li ghtGray);
JPanel drawingArea = new JPanel();
TextField message = new TextField("Draw away! Click anywhere on canvas to type");
message.setBack ground(Color.gr ay);
message.setEdit able(false);
drawingArea.set PreferredSize(n ew Dimension(500, 400));
drawingArea.set Border (BorderFactory. createLineBorde r (Color.blue, 2));
drawingArea.set Background(Colo r.white);
content.add(dra wingArea, BorderLayout.CE NTER);
content.add(mes sage, BorderLayout.SO UTH);
pack();
this.setVisible (true);
this.setResizab le(true);
drawingArea.add MouseListener(t his);
drawingArea.add MouseMotionList ener(this);
// addKeyListener( this);
}
//when the mouse is pressed, a point is formed
public void mousePressed(Mo useEvent e){
oldPoint = e.getPoint();
String msg = ("Mouse Pressed: (" + oldPoint.x + ", " + oldPoint.y + ")");
System.out.prin tln(msg);
s = "";
}
//mouseDragged will do the line drawing
public void mouseDragged(Mo useEvent e){
currentPoint = e.getPoint();
String msg = "Mouse Dragged: (" + currentPoint.x + ", " + currentPoint.y + ")";
System.out.prin tln(msg);
this.getGraphic s().drawLine(cu rrentPoint.x, currentPoint.y, oldPoint.x, oldPoint.y);
oldPoint = currentPoint;//set the oldPoint to the most recent coordinates
}
//keyTyped will print the character, starting at the last place the mouse was pressed
/*public void keyTyped(KeyEve nt e){
s += e.getKeyChar();
getGraphics().d rawString(s, oldPoint.x, oldPoint.y);
}
*/
public boolean keyDown(Event e, int key) {
String s = String.valueOf( (char)key);
getGraphics().d rawString(s, oldPoint.x, oldPoint.y);
return(record(o ldPoint.x + fm.stringWidth( s), oldPoint.y));
}
protected boolean record(int x, int y) {
oldPoint.x = x;
oldPoint.y = y;
return(true);
}
//abstract methods promised by the MouseListener and MouseMotionList ener interfaces
public void mouseEntered(Mo useEvent e){}
public void mouseExited(Mou seEvent e){}
public void mouseReleased(M ouseEvent e){}
public void mouseClicked(Mo useEvent e){}
public void mouseMoved(Mous eEvent e){}
public void keyReleased(Key Event e){}
public void keyPressed(KeyE vent e){}
}
[/CODE]
Comment