• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

some help reqd in painting of Canvas?

 
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic