problem with printing text on a JFrame

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dishal
    New Member
    • Jun 2007
    • 8

    problem with printing text on a JFrame

    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]
    Last edited by r035198x; Jun 11 '07, 07:21 AM. Reason: added code tags
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by dishal
    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]
    Is your fm initialised when the keyDown method is called?

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Is your fm initialised when the keyDown method is called?

      Comment

      • dishal
        New Member
        • Jun 2007
        • 8

        #4
        Yes it has already been initialized. I just dont seem to the a problem with the codes and I really have not a clue why it doesnt work.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by dishal
          Yes it has already been initialized. I just dont seem to the a problem with the codes and I really have not a clue why it doesnt work.
          No it has not. Not when that method is called. Print it out and see.

          Comment

          • dishal
            New Member
            • Jun 2007
            • 8

            #6
            Originally posted by r035198x
            No it has not. Not when that method is called. Print it out and see.
            Can you help me out here? What should I add in? I'm a novice at this :-|

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by dishal
              Can you help me out here? What should I add in? I'm a novice at this :-|
              Try it with your keyDown method replaced by
              [CODE=java]public boolean keyDown(Event e, int key) {
              String s = String.valueOf( (char)key);
              getGraphics().d rawString(s, oldPoint.x, oldPoint.y);
              Font font = new Font("Helvetica ", Font.BOLD, 20);
              fm = getFontMetrics( font);
              return(record(o ldPoint.x + fm.stringWidth( s), oldPoint.y));
              }[/CODE]

              Comment

              • dishal
                New Member
                • Jun 2007
                • 8

                #8
                Originally posted by r035198x
                Try it with your keyDown method replaced by
                [CODE=java]public boolean keyDown(Event e, int key) {
                String s = String.valueOf( (char)key);
                getGraphics().d rawString(s, oldPoint.x, oldPoint.y);
                Font font = new Font("Helvetica ", Font.BOLD, 20);
                fm = getFontMetrics( font);
                return(record(o ldPoint.x + fm.stringWidth( s), oldPoint.y));
                }[/CODE]
                It works fine now but when i try to backspace it, I get little boxes (the kind you get when typing a foreign lang) any idea how i can get rid of it?

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by dishal
                  It works fine now but when i try to backspace it, I get little boxes (the kind you get when typing a foreign lang) any idea how i can get rid of it?
                  Since you're handling the printing yourself, you also need to handle the delete.
                  The backspace key is special so you need to handle it differently from the way you were doing it. See the KeyEvent from the API.

                  Comment

                  • dishal
                    New Member
                    • Jun 2007
                    • 8

                    #10
                    Originally posted by r035198x
                    Since you're handling the printing yourself, you also need to handle the delete.
                    The backspace key is special so you need to handle it differently from the way you were doing it. See the KeyEvent from the API.
                    Thanks for all the help! You're awesome :D

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by dishal
                      Thanks for all the help! You're awesome :D
                      *blush*

                      The API docs are a good thing to refer to once in a while.

                      Comment

                      Working...