aspose file tools*
The moose likes Applets and the fly likes Canvas & Scrollbras integrated Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Canvas & Scrollbras integrated" Watch "Canvas & Scrollbras integrated" New topic
Author

Canvas & Scrollbras integrated

Samiullah Aazam
Greenhorn

Joined: Jul 02, 2001
Posts: 7
I am displaying some messages in a canvas . Now i want these
messages to be scrolled when i move the slider of the adjacent scrollbar(vertical). how can i do that?
I am desperately looking for its solution. Plz. help me.
I hope some of u can certainly do it. I especially request Mr. Manfred to solve it who has helped me earlier.
here is my code:
------------------------------------------------------------
--------------extended canvas class-------------------------
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
class apltcnv extends Canvas
{
int startCan;
int endCan;
Vector msg=new Vector(17);
public apltcnv()
{}
public void paint( Graphics g )
{
Dimension dd = getSize();
double w = dd.getWidth();
double h = dd.getHeight();

System.out.println( "canvas' width= " + w + "canvas' height= " + h+" time****" );
setBackground( Color.cyan );


String str=aplt.tf.getText();
str=str.trim();
if(str.equals("") | | str==null) return;
System.out.println( "b4 addmessage" );
addMessage("sami : "+str);

int ii=msg.size();
System.out.println("vector size "+ii);
g.setColor(Color.red);
displayMessages(g);
aplt.tf.setText("");
}
void addMessage(String str)
{
msg.addElement(str);
}
void displayMessages(Graphics g)
{
Dimension dd=this.getSize();

double h=dd.getHeight();

int ii=msg.size();
startCan=ii;
endCan=0;
System.out.println("startcan "+startCan+" endcan "+endCan);
if(ii>17)
{
startCan=ii;
endCan=ii-16;
System.out.println("startcan "+startCan+" endcan "+endCan);
}
finalDisplay(startCan,endCan,h,g);
}

void finalDisplay(int startCan, int endCan, double h, Graphics g)
{
int rr=startCan-1;
for(;startCan>endCan;endCan++)
{
int ii=msg.size();

String ss=(String)msg.elementAt(rr);

rr=rr-1;

g.drawString(ss,0,(int)h);
h=h-10;
}
}

}
----------------------------------------------------------------
-------------------extended applet class------------------------
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*
<applet code="aplt.class" width=300 height=300>
</applet>
*/
public class aplt extends Applet implements ActionListener
{
Label l;
Button b1,b2;
Panel p,pl,pll;
apltcnv c;
Scrollbar vrt;
public static TextField tf;
public void init()
{
GridBagConstraints gbc = new GridBagConstraints();
setLayout( new BorderLayout() );
setBackground( Color.yellow );
l=new Label("chat applet");
pl=new Panel();
pl.setLayout(new GridLayout(2,1,5,5));
pl.add(l);
pll=new Panel();
pll.setLayout(new FlowLayout());
b1 = new Button( "btn1" );
b2 = new Button( "btn2" );
pll.add(b1);
pll.add(b2);
pl.add(pll);

add( pl, BorderLayout.NORTH );
p = new Panel( new GridBagLayout() );
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.CENTER;
c = new apltcnv();
c.setSize( 280, 170 );
p.add( c, gbc );

// p.add("East",vrt);
add( p, BorderLayout.CENTER );
vrt=new Scrollbar(Scrollbar.VERTICAL,1,2,1,250);
add(vrt,BorderLayout.EAST);
tf=new TextField(20);
add(tf,BorderLayout.SOUTH);
tf.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Object oo=e.getSource();
if(oo==tf)
{
String getMsg=tf.getText();
getMsg=getMsg.trim();
if(getMsg.equals("") | | getMsg==null) return;
c.repaint();
}

}
public void paint( Graphics g )
{



}
}
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Samiullah,
I have never tried to do that, but I have some code that might help you.

Hope it helps,
Manfred.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Canvas & Scrollbras integrated