Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Canvas & Scrollbras integrated

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 )
{



}
}
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Samiullah,
I have never tried to do that, but I have some code that might help you.

Hope it helps,
Manfred.
reply
    Bookmark Topic Watch Topic
  • New Topic