problem with printing text on a JFrame | Newbie | | Join Date: Jun 2007
Posts: 8
| |
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: - import java.awt.*;
-
import javax.swing.*;
-
import java.awt.event.*;
-
import java.lang.StringBuffer;
-
-
-
public class JPanels extends JFrame implements MouseListener, MouseMotionListener//, 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.setBackground(Color.lightGray);
-
-
JPanel drawingArea = new JPanel();
-
-
TextField message = new TextField("Draw away! Click anywhere on canvas to type");
-
message.setBackground(Color.gray);
-
message.setEditable(false);
-
-
drawingArea.setPreferredSize(new Dimension(500, 400));
-
drawingArea.setBorder (BorderFactory.createLineBorder (Color.blue, 2));
-
drawingArea.setBackground(Color.white);
-
-
-
content.add(drawingArea, BorderLayout.CENTER);
-
content.add(message, BorderLayout.SOUTH);
-
-
-
pack();
-
-
this.setVisible(true);
-
this.setResizable(true);
-
-
drawingArea.addMouseListener(this);
-
drawingArea.addMouseMotionListener(this);
-
// addKeyListener(this);
-
-
}
-
//when the mouse is pressed, a point is formed
-
public void mousePressed(MouseEvent e){
-
oldPoint = e.getPoint();
-
-
String msg = ("Mouse Pressed: (" + oldPoint.x + ", " + oldPoint.y + ")");
-
System.out.println(msg);
-
-
s = "";
-
-
}
-
-
//mouseDragged will do the line drawing
-
public void mouseDragged(MouseEvent e){
-
currentPoint = e.getPoint();
-
-
String msg = "Mouse Dragged: (" + currentPoint.x + ", " + currentPoint.y + ")";
-
System.out.println(msg);
-
-
this.getGraphics().drawLine(currentPoint.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(KeyEvent e){
-
s += e.getKeyChar();
-
getGraphics().drawString(s, oldPoint.x, oldPoint.y);
-
}
-
*/
-
-
-
public boolean keyDown(Event e, int key) {
-
String s = String.valueOf((char)key);
-
getGraphics().drawString(s, oldPoint.x, oldPoint.y);
-
return(record(oldPoint.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 MouseMotionListener interfaces
-
public void mouseEntered(MouseEvent e){}
-
public void mouseExited(MouseEvent e){}
-
public void mouseReleased(MouseEvent e){}
-
public void mouseClicked(MouseEvent e){}
-
public void mouseMoved(MouseEvent e){}
-
public void keyReleased(KeyEvent e){}
-
public void keyPressed(KeyEvent e){}
-
-
-
}
-
| | Lives Here | | Join Date: Sep 2006
Posts: 12,070
| | | 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: - import java.awt.*;
-
import javax.swing.*;
-
import java.awt.event.*;
-
import java.lang.StringBuffer;
-
-
-
public class JPanels extends JFrame implements MouseListener, MouseMotionListener//, 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.setBackground(Color.lightGray);
-
-
JPanel drawingArea = new JPanel();
-
-
TextField message = new TextField("Draw away! Click anywhere on canvas to type");
-
message.setBackground(Color.gray);
-
message.setEditable(false);
-
-
drawingArea.setPreferredSize(new Dimension(500, 400));
-
drawingArea.setBorder (BorderFactory.createLineBorder (Color.blue, 2));
-
drawingArea.setBackground(Color.white);
-
-
-
content.add(drawingArea, BorderLayout.CENTER);
-
content.add(message, BorderLayout.SOUTH);
-
-
-
pack();
-
-
this.setVisible(true);
-
this.setResizable(true);
-
-
drawingArea.addMouseListener(this);
-
drawingArea.addMouseMotionListener(this);
-
// addKeyListener(this);
-
-
}
-
//when the mouse is pressed, a point is formed
-
public void mousePressed(MouseEvent e){
-
oldPoint = e.getPoint();
-
-
String msg = ("Mouse Pressed: (" + oldPoint.x + ", " + oldPoint.y + ")");
-
System.out.println(msg);
-
-
s = "";
-
-
}
-
-
//mouseDragged will do the line drawing
-
public void mouseDragged(MouseEvent e){
-
currentPoint = e.getPoint();
-
-
String msg = "Mouse Dragged: (" + currentPoint.x + ", " + currentPoint.y + ")";
-
System.out.println(msg);
-
-
this.getGraphics().drawLine(currentPoint.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(KeyEvent e){
-
s += e.getKeyChar();
-
getGraphics().drawString(s, oldPoint.x, oldPoint.y);
-
}
-
*/
-
-
-
public boolean keyDown(Event e, int key) {
-
String s = String.valueOf((char)key);
-
getGraphics().drawString(s, oldPoint.x, oldPoint.y);
-
return(record(oldPoint.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 MouseMotionListener interfaces
-
public void mouseEntered(MouseEvent e){}
-
public void mouseExited(MouseEvent e){}
-
public void mouseReleased(MouseEvent e){}
-
public void mouseClicked(MouseEvent e){}
-
public void mouseMoved(MouseEvent e){}
-
public void keyReleased(KeyEvent e){}
-
public void keyPressed(KeyEvent e){}
-
-
-
}
-
Is your fm initialised when the keyDown method is called?
| | Lives Here | | Join Date: Sep 2006
Posts: 12,070
| | | 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
| | | 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
| | | 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
| | | 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
| | | 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 - public boolean keyDown(Event e, int key) {
-
String s = String.valueOf((char)key);
-
getGraphics().drawString(s, oldPoint.x, oldPoint.y);
-
Font font = new Font("Helvetica", Font.BOLD, 20);
-
fm = getFontMetrics(font);
-
return(record(oldPoint.x + fm.stringWidth(s), oldPoint.y));
-
}
| | Newbie | | Join Date: Jun 2007
Posts: 8
| | | re: problem with printing text on a JFrame Quote:
Originally Posted by r035198x Try it with your keyDown method replaced by - public boolean keyDown(Event e, int key) {
-
String s = String.valueOf((char)key);
-
getGraphics().drawString(s, oldPoint.x, oldPoint.y);
-
Font font = new Font("Helvetica", Font.BOLD, 20);
-
fm = getFontMetrics(font);
-
return(record(oldPoint.x + fm.stringWidth(s), oldPoint.y));
-
}
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
| | | 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
| | | 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
| | | 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.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|