Posted By:
Sakibul_Khan
Posted On:
Friday, April 11, 2003 09:43 AM
Hi, I have a Canvas inside a Scrollpane. I want to increase the size of the canvas, when a certain event occurs, but even though the Canvas size increases, which I double check using "System.out.println("--> " + c.getSize());", but the scrollbar of the Scrollpane remains unchanged. Hence, it is as if the size of the canvas didn't change, even though I can see in the "System.out.println("--> " + c.getSize());" statement that the Canvas size has changed. Hence, the problem is that the Scrollpane is not detecting that the Canvas size has increased. Thanx in advance. -------------------------------SAKIB
More>>
Hi,
I have a Canvas inside a Scrollpane. I want to increase the size of the canvas, when a certain event occurs, but even though the Canvas size increases, which I double check using "System.out.println("--> " + c.getSize());", but the scrollbar of the Scrollpane remains unchanged. Hence, it is as if the size of the canvas didn't change, even though I can see in the "System.out.println("--> " + c.getSize());" statement that the Canvas size has changed.
Hence, the problem is that the Scrollpane is not detecting that the Canvas size has increased.
Thanx in advance.
-------------------------------SAKIB
The following is my code (NB. Clicking on the button (@ West) increases the size of the Canvas.):-
package client;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class CanvasScroll extends Applet {
Canvas c;
ScrollPane s;
private BorderLayout borderLayout1 = new BorderLayout();
private Button button1 = new Button();
Vector vect = new Vector();
int canvasWidth = 75, canvasHeight = 400;
//double buffering PART-1
Image myOffScreenImage;
Graphics myOffScreenGraphics;
//double buffering PART-2 & 3 together
public void update(Graphics g){
myOffScreenImage = createImage(getSize().width, getSize().height);
myOffScreenGraphics = myOffScreenImage.getGraphics();
paint(myOffScreenGraphics);
g.drawImage(myOffScreenImage, 0, 0, this);
}
public void init() {
setLayout(new BorderLayout());
s = new ScrollPane();
s.setSize(100,100);
add("Center", s);
c = new myCanvas();
c.setSize(canvasWidth,canvasHeight);//no horizontal scrollbar's//75, 400
s.add(c);
button1.setLabel("button1");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button1_actionPerformed(e);
}
});
this.add(button1, BorderLayout.WEST);
}
class myCanvas extends Canvas {
//double buffering PART-1
Image myOffScreenImage;
Graphics myOffScreenGraphics;
//double buffering PART-2 & 3 together
public void update(Graphics g){
myOffScreenImage = createImage(getSize().width, getSize().height);
myOffScreenGraphics = myOffScreenImage.getGraphics();
paint(myOffScreenGraphics);
g.drawImage(myOffScreenImage, 0, 0, this);
}
public myCanvas(){ super(); }
public void paint(Graphics g){
int x = 10, y = 10, y_diff = 20;
g.drawString("Hello World = " + y, x, y); y += y_diff;
g.drawString("Hello World = " + y, x, y); y += y_diff;
g.drawString("Hello World = " + y, x, y); y += y_diff;
g.drawString("Hello World = " + y, x, y); y += y_diff;
g.drawString("Hello World = " + y, x, y); y += y_diff;
g.drawString("Hello World = " + y, x, y); y += y_diff;
for(int i = 0; i
< vect.size(); i++){
g.drawString((String) vect.elementAt(i), x, y); y += y_diff;
}
}
}
public CanvasScroll(){
try{
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception{}
public void button1_actionPerformed(ActionEvent e){
vect.addElement("Hello World !!!");
canvasHeight += 20;
c.setSize(canvasWidth, canvasHeight);
System.out.println("--> " + c.getSize());
}
}
<<Less