Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

display text on form without label

 
Joshua Austin
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi.
I am trying to make a telnet client, so I have to be able to draw text anywhere on the form, prefurably by setting coordinates, and change the color of the text (without previously drawn text's color being changed).
I cant seem to figure out how to do this..
Tryed using Graphics, but from what I can see, it can only be used in the Overrided Paint(Graphics g) {} method of a component?...
any help would be apreciated
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes... that would be the way you would have to do it... override the paintComponent( Graphics ) method of any JComponent ( most probably JPanel ) and set the color and font before drawing the string at a specified location...

I'm not really clear on what you are trying to do (telnet client? Isn't that just a scrolling text console? ), but the code below illustrates the general concept:

 
Joshua Austin
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is just a scrolling text console, but in this case its ANSI, so I have color codes, and other ansi codes such as moving the cursor to specific coordinates. So a normal text field or a Label wont work, unless I can change the color of individual characters, and choose where in those components the text can be displayed...
the overriding the Paint method poses a problem because it requires you to pass it the Graphics object, so I cant call it to refresh the display.
Also, if I override it in the frame class, will it still be able to draw the components?
[ January 23, 2003: Message edited by: Joshua Austin ]
 
raj madhuram
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might be useful
http://www.nongnu.org/jcurzez/
regards,
Raj
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm... looks like JCurzez may help you out...


the overriding the Paint method poses a problem because it requires you to pass it the Graphics object, so I cant call it to refresh the display.


You usually don't pass a Graphics component into the paint(Graphics) method... usually paint(Graphics) is called from update(Graphics), which in turn is called from the native console code and is automatically passed the Graphics object.

You would just call repaint() in your code whenever you needed to update... you could do this either in a thread that refreshes the display at a specified rate, or you could do this in listeners in response to events (either user generated(i.e. mouse or key events)or data generated(i.e. property change events)).

Also, if I override it in the frame class, will it still be able to draw the components?


You'll need to call super.paint( g ) in your overridden paint() method, if you are using Swing. I think in AWT you don't have to do this because each component draws itself. However, my suggestion is to make the custom painting component a subclass of Panel or JPanel instead of Frame... that way if you need to put it in a scroll pane, put other components around it, put it in an internal frame, etc. you will not have to rewrite your code. Instead of painting on the frame, you would paint on the panel, and add the panel to the frame.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!