Connecting Tech Pros Worldwide Help | Site Map

problem with printing text on a JFrame

Newbie
 
Join Date: Jun 2007
Posts: 8
#1: Jun 11 '07
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:

Expand|Select|Wrap|Line Numbers
  1.  import java.awt.*; 
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. import java.lang.StringBuffer;
  5.  
  6.  
  7. public class JPanels extends JFrame implements MouseListener, MouseMotionListener//, KeyListener
  8. {
  9.  
  10.     private Point oldPoint, currentPoint;
  11.     private String s;
  12.     private TextField message;
  13.     private JPanel drawingArea;
  14.     private FontMetrics fm;
  15.  
  16.  
  17.     public void init() {
  18.  
  19.     //initialize the "old" point
  20.     oldPoint = new Point(0,0);
  21.  
  22.     Font font = new Font("Helvetica", Font.BOLD, 20);
  23. setFont(font);
  24. fm = getFontMetrics(font);
  25.  
  26. }        
  27. public static void main(String[] args) {
  28. new JPanels();
  29.  
  30. }
  31.  
  32. public JPanels() {
  33.  
  34.  
  35. super("Welcome to Whiteboard!");
  36.  
  37. Container content = getContentPane();
  38. content.setBackground(Color.lightGray);
  39.  
  40.     JPanel drawingArea = new JPanel();
  41.  
  42. TextField message = new TextField("Draw away! Click anywhere on canvas to type");
  43.     message.setBackground(Color.gray);
  44. message.setEditable(false);
  45.  
  46. drawingArea.setPreferredSize(new Dimension(500, 400));
  47. drawingArea.setBorder (BorderFactory.createLineBorder (Color.blue, 2));
  48. drawingArea.setBackground(Color.white);
  49.  
  50.  
  51. content.add(drawingArea, BorderLayout.CENTER);
  52. content.add(message, BorderLayout.SOUTH);
  53.  
  54.  
  55. pack();
  56.  
  57. this.setVisible(true);
  58.     this.setResizable(true);
  59.  
  60.         drawingArea.addMouseListener(this);
  61.         drawingArea.addMouseMotionListener(this);
  62. //        addKeyListener(this);
  63.  
  64. }
  65. //when the mouse is pressed, a point is formed
  66.     public void mousePressed(MouseEvent e){
  67.         oldPoint = e.getPoint();
  68.  
  69.         String msg = ("Mouse Pressed: (" + oldPoint.x + ", " + oldPoint.y + ")");    
  70.         System.out.println(msg);
  71.  
  72.         s = "";
  73.  
  74.     }
  75.  
  76.     //mouseDragged will do the line drawing
  77.     public void mouseDragged(MouseEvent e){
  78.         currentPoint = e.getPoint();
  79.  
  80.         String msg = "Mouse Dragged: (" + currentPoint.x + ", " + currentPoint.y + ")";
  81.         System.out.println(msg);
  82.  
  83.         this.getGraphics().drawLine(currentPoint.x, currentPoint.y, oldPoint.x, oldPoint.y);
  84.         oldPoint = currentPoint;//set the oldPoint to the most recent coordinates
  85.     }
  86.  
  87.  
  88.     //keyTyped will print the character, starting at the last place the mouse was pressed
  89.  
  90. /*public void keyTyped(KeyEvent e){
  91.         s += e.getKeyChar();
  92.         getGraphics().drawString(s, oldPoint.x, oldPoint.y);
  93.         }
  94.     */
  95.  
  96.  
  97. public boolean keyDown(Event e, int key) {
  98. String s = String.valueOf((char)key);
  99. getGraphics().drawString(s, oldPoint.x, oldPoint.y);
  100. return(record(oldPoint.x + fm.stringWidth(s), oldPoint.y));
  101.      }
  102.  
  103.  
  104.     protected boolean record(int x, int y) {
  105.     oldPoint.x = x;
  106.     oldPoint.y = y;
  107. return(true);
  108.  
  109.     }
  110.  
  111.     //abstract methods promised by the MouseListener and MouseMotionListener interfaces
  112.     public void mouseEntered(MouseEvent e){}
  113.     public void mouseExited(MouseEvent e){}
  114.     public void mouseReleased(MouseEvent e){}
  115.     public void mouseClicked(MouseEvent e){}
  116.     public void mouseMoved(MouseEvent e){}
  117.     public void keyReleased(KeyEvent e){}
  118.     public void keyPressed(KeyEvent e){}
  119.  
  120.  
  121. }
  122.  
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Jun 11 '07

re: problem with printing text on a JFrame


Quote:

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:

Expand|Select|Wrap|Line Numbers
  1.  import java.awt.*; 
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. import java.lang.StringBuffer;
  5.  
  6.  
  7. public class JPanels extends JFrame implements MouseListener, MouseMotionListener//, KeyListener
  8. {
  9.  
  10. private Point oldPoint, currentPoint;
  11. private String s;
  12. private TextField message;
  13. private JPanel drawingArea;
  14. private FontMetrics fm;
  15.  
  16.  
  17. public void init() {
  18.  
  19. //initialize the "old" point
  20. oldPoint = new Point(0,0);
  21.  
  22. Font font = new Font("Helvetica", Font.BOLD, 20);
  23. setFont(font);
  24. fm = getFontMetrics(font);
  25.  
  26. public static void main(String[] args) {
  27. new JPanels();
  28.  
  29. }
  30.  
  31. public JPanels() {
  32.  
  33.  
  34. super("Welcome to Whiteboard!");
  35.  
  36. Container content = getContentPane();
  37. content.setBackground(Color.lightGray);
  38.  
  39. JPanel drawingArea = new JPanel();
  40.  
  41. TextField message = new TextField("Draw away! Click anywhere on canvas to type");
  42. message.setBackground(Color.gray);
  43. message.setEditable(false);
  44.  
  45. drawingArea.setPreferredSize(new Dimension(500, 400));
  46. drawingArea.setBorder (BorderFactory.createLineBorder (Color.blue, 2));
  47. drawingArea.setBackground(Color.white);
  48.  
  49.  
  50. content.add(drawingArea, BorderLayout.CENTER);
  51. content.add(message, BorderLayout.SOUTH);
  52.  
  53.  
  54. pack();
  55.  
  56. this.setVisible(true);
  57. this.setResizable(true);
  58.  
  59. drawingArea.addMouseListener(this);
  60. drawingArea.addMouseMotionListener(this);
  61. // addKeyListener(this);
  62.  
  63. }
  64. //when the mouse is pressed, a point is formed
  65. public void mousePressed(MouseEvent e){
  66. oldPoint = e.getPoint();
  67.  
  68. String msg = ("Mouse Pressed: (" + oldPoint.x + ", " + oldPoint.y + ")"); 
  69. System.out.println(msg);
  70.  
  71. s = "";
  72.  
  73. }
  74.  
  75. //mouseDragged will do the line drawing
  76. public void mouseDragged(MouseEvent e){
  77. currentPoint = e.getPoint();
  78.  
  79. String msg = "Mouse Dragged: (" + currentPoint.x + ", " + currentPoint.y + ")";
  80. System.out.println(msg);
  81.  
  82. this.getGraphics().drawLine(currentPoint.x, currentPoint.y, oldPoint.x, oldPoint.y);
  83. oldPoint = currentPoint;//set the oldPoint to the most recent coordinates
  84. }
  85.  
  86.  
  87. //keyTyped will print the character, starting at the last place the mouse was pressed
  88.  
  89. /*public void keyTyped(KeyEvent e){
  90. s += e.getKeyChar();
  91. getGraphics().drawString(s, oldPoint.x, oldPoint.y);
  92. }
  93. */
  94.  
  95.  
  96. public boolean keyDown(Event e, int key) {
  97. String s = String.valueOf((char)key);
  98. getGraphics().drawString(s, oldPoint.x, oldPoint.y);
  99. return(record(oldPoint.x + fm.stringWidth(s), oldPoint.y));
  100. }
  101.  
  102.  
  103. protected boolean record(int x, int y) {
  104. oldPoint.x = x;
  105. oldPoint.y = y;
  106. return(true);
  107.  
  108. }
  109.  
  110. //abstract methods promised by the MouseListener and MouseMotionListener interfaces
  111. public void mouseEntered(MouseEvent e){}
  112. public void mouseExited(MouseEvent e){}
  113. public void mouseReleased(MouseEvent e){}
  114. public void mouseClicked(MouseEvent e){}
  115. public void mouseMoved(MouseEvent e){}
  116. public void keyReleased(KeyEvent e){}
  117. public void keyPressed(KeyEvent e){}
  118.  
  119.  
  120. }
  121.  

Is your fm initialised when the keyDown method is called?
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#3: Jun 11 '07

re: problem with printing text on a JFrame


Is your fm initialised when the keyDown method is called?
Newbie
 
Join Date: Jun 2007
Posts: 8
#4: Jun 11 '07

re: problem with printing text on a JFrame


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.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#5: Jun 11 '07

re: problem with printing text on a JFrame


Quote:

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.
Newbie
 
Join Date: Jun 2007
Posts: 8
#6: Jun 11 '07

re: problem with printing text on a JFrame


Quote:

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 :-|
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#7: Jun 11 '07

re: problem with printing text on a JFrame


Quote:

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
Expand|Select|Wrap|Line Numbers
  1. public boolean keyDown(Event e, int key) {
  2.     String s = String.valueOf((char)key);
  3.     getGraphics().drawString(s, oldPoint.x, oldPoint.y);
  4.     Font font = new Font("Helvetica", Font.BOLD, 20);
  5.     fm = getFontMetrics(font);
  6.     return(record(oldPoint.x + fm.stringWidth(s), oldPoint.y));
  7. }
Newbie
 
Join Date: Jun 2007
Posts: 8
#8: Jun 11 '07

re: problem with printing text on a JFrame


Quote:

Originally Posted by r035198x

Try it with your keyDown method replaced by

Expand|Select|Wrap|Line Numbers
  1. public boolean keyDown(Event e, int key) {
  2.     String s = String.valueOf((char)key);
  3.     getGraphics().drawString(s, oldPoint.x, oldPoint.y);
  4.     Font font = new Font("Helvetica", Font.BOLD, 20);
  5.     fm = getFontMetrics(font);
  6.     return(record(oldPoint.x + fm.stringWidth(s), oldPoint.y));
  7. }

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?
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#9: Jun 11 '07

re: problem with printing text on a JFrame


Quote:

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.
Newbie
 
Join Date: Jun 2007
Posts: 8
#10: Jun 11 '07

re: problem with printing text on a JFrame


Quote:

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
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#11: Jun 11 '07

re: problem with printing text on a JFrame


Quote:

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.
Reply