wood burning stoves 2.0*
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 Android Security Essentials Live Lessons this week in the Android 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
Author

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 ,
Daman
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();
toPaint.setBackground(Color.white);
bottom=new Panel();
setLayout(new BorderLayout());
tf=new TextField();
tf.addActionListener(this);
add(toPaint,BorderLayout.CENTER);
add(tf,BorderLayout.SOUTH);
setBounds(100,100,200,300);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
g=toPaint.getGraphics();
Dimension d=toPaint.getSize();
int max=(int)d.getHeight()-10;
if(y<max)>
{
//if the line starts with red string then make it a red text line else as it is
if(tf.getText().startsWith("red"))
g.setColor(Color.red);
g.drawString(tf.getText(),10,y);
//change it back to black text
g.setColor(Color.black);
//increment the counter for y axis for the next line
y+=10;
tf.setText("");
}
else
{
//if the end of canvas is reached the clear up the screen and start from the top again
//fill it with a white rectangle
g.setColor(Color.white);
g.fillRect(0,0,(int)d.getWidth(),(int)d.getHeight());
//change the color back to black
g.setColor(Color.black);
// start again with y as 10
y=10;
g.drawString(tf.getText(),10,y);
}
}
public static void main(String s[])
{
new myPanel();
}
}
pete smith
Greenhorn

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);
sp.add(myCanvas);
that should help.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: some help reqd in painting of Canvas?
 
Similar Threads
Problem painting component
Double Buffering Problem
How to code optimization..
Problem with custom painting and multiple JPanel
Rectangle versus java.awt.Rectangle