Skip to main content

Can applet support mutiple canvas ?

4 replies [Last post]
hhuang
Offline
Joined: 2009-11-17
Points: 0

My application will use 2 canvas, each canvas maintenance some components. When the condition trigger, I want to swith between these 2 canvas. eg. in applet, switch from canvas A to canvas B. So I just want to switch the canvas, NOT delete the components in canvas A, and then add all the components of canvas B to the applet.
For testing, I wrote a simple program and found it is NOT ok, the test resutl is : applet only can show the latest canvas added to it...So, I need to be confirm: j2se can NOT support mutiple canvas or not ? if not, for my situation, what should I do ? Hope sombody can help to answer my question, thx in advance ~

For the program source is as below :

*******************************************************
package mutiple.canvas;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SaveCanvas extends Frame {
ImageCanvas ic = new ImageCanvas();
ImageCanvas2 ic2 = new ImageCanvas2();

boolean flag = false;

public SaveCanvas() {
Button b = new Button("save canvas");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if(flag){
ic.setVisible(true);
ic2.setVisible(false);
}else{
ic.setVisible(false);
ic2.setVisible(true);
}
flag = !flag;
}
});
setLayout(new BorderLayout());
add(b, BorderLayout.NORTH);

add(ic, BorderLayout.CENTER);
add(ic2, BorderLayout.CENTER);
setSize(new Dimension(400, 400));
setVisible(true);
ic.setVisible(true);
ic2.setVisible(false);
}

public static void main(String args[]) {
new SaveCanvas();
}

class ImageCanvas extends Canvas {
public void paint(Graphics g) {
g.setColor(Color.blue);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.red);
g.drawLine(0, 0, this.getWidth(), this.getHeight());
}
}

class ImageCanvas2 extends Canvas {
public void paint(Graphics g) {
g.setColor(Color.blue);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.red);
g.drawLine(0, this.getHeight(), this.getWidth(), 0);
}
}
}
*******************************************************

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
anthony_p
Offline
Joined: 2006-07-24
Points: 0

You can't have two components in the CENTER of a BorderLayout-ed container. The layout manager supports only ONE component in each of the sides (east, west, north, south, center) at a time.

claudeabb
Offline
Joined: 2006-08-09
Points: 0

Hi,

There is no interest to use a canvas if it's for managing component such as textpane, scroller, label etc.. A canvas has to be used when you need to draw something.

But regarding your need, you could remove the canvas A at put your canvas B.

This.remove(canvasA)
this.add(canvasA, BorderLayout.Center);

You could also create your own layout manager to be able to stack the canvas. and so the canvas A will appear above the canvas B. After that you just have to make visible/unvisible the canvas A.

Claude

hhuang
Offline
Joined: 2009-11-17
Points: 0

thx for you guys` answer, they are really helpful

hhuang
Offline
Joined: 2009-11-17
Points: 0

Does somebody can give me a answer or suggestion ? This question is really urgent for my project right now, pls help .