This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes some help reqd in painting of Canvas? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "some help reqd in painting of Canvas? " Watch "some help reqd in painting of Canvas? " New topic

some help reqd in painting of Canvas?

daman sidhu
Ranch Hand

Joined: Aug 19, 2000
Posts: 184
Hi Freinds ,
I had made a chat in swings some time back and am now bringing it down to awt, for the colors part of the message window i used canvas as there is not support for multiple colors at the same time in textarea etc.For testing only i made this small class , the problem is that i explicitly used the graphics object of this class and since canvas does not have inbuilt functionality of repainting it self i am having problems as when a component gets over this one and it has to repaint itself it doesnot, i am pasting the sample code here plz see if anyone can help me out with this one, i will highly appreciate it,
Thanks ,
PS: also if anyone can tell me how to make this thing scrollable ill be grateful as i tried adding it to scrollpane but to no avail.
import java.awt.*;
import java.awt.event.*;
public class myPanel extends Frame implements ActionListener
Canvas toPaint;
Panel bottom ;
TextField tf;
Graphics g;
int y=10;
Color c=new Color(125,125,166);
public myPanel()
toPaint=new Canvas();
bottom=new Panel();
setLayout(new BorderLayout());
tf=new TextField();
public void actionPerformed(ActionEvent ae)
Dimension d=toPaint.getSize();
int max=(int)d.getHeight()-10;
//if the line starts with red string then make it a red text line else as it is
//change it back to black text
//increment the counter for y axis for the next line
//if the end of canvas is reached the clear up the screen and start from the top again
//fill it with a white rectangle
//change the color back to black
// start again with y as 10
public static void main(String s[])
new myPanel();
pete smith

Joined: Dec 08, 2000
Posts: 4
You need to subclass Canvas and do override paint:
class myCanvas extends Canvas{
You cant put a Frame in a Scrollpane, make your class extend Panel not Frame, and then use a frame to hold a scrollpane, the canvas and whatever else.
do: ScrollPane sp = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
that should help.
I agree. Here's the link:
subject: some help reqd in painting of Canvas?
It's not a secret anymore!