aspose file tools*
The moose likes Applets and the fly likes why do this canvas is covering the entire area of the applet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "why do this canvas is covering the entire area of the applet?" Watch "why do this canvas is covering the entire area of the applet?" New topic
Author

why do this canvas is covering the entire area of the applet?

Samiullah Aazam
Greenhorn

Joined: Jul 02, 2001
Posts: 7
Here is an applet(code) which has the following settings:

Layout for the applet is BorderLayout.
Background color for the applet is yellow.
A button(b1) is added to the "north" of the applet.
A panel(p) is added to the "center" of the applet.
Layout for the Panel(p) is GridLayout(1,1,5,5).
A canvas(canvas11) is added to the panel(p).
Background color for the canvas is cyan.
I chose the size of the canvas to b (100,50) pixels.

When i run this applet with appletviewer it firstly shows(flicks) the applet's background color as yellow & the statically showing its background color as cyan. i am unable to figure out that why do this canvas is covering the entire area of the applet although its size is specified. I just want to create a canvas(with red color) of a specified size say 100,100 pixels & place this canvas at the center of an applet(with BorderLayout) of size (width=300, height=300). This canvas should not cover the entire area of the applet. How can i do that? Plz. help me!
Hope some of u will clear my doubt.
Thankx!
the code of the applet is:
--------------------------------------------------------------
import java.awt.*;
import java.applet.*;

/*
<applet code = "vc.class" width = 350 height = 300>
</applet>
*/
public class vc extends Applet
{
Button b1=new Button("button");
Panel p=new Panel();
canv1 canv11=new canv1();
public void init()
{
setLayout( new BorderLayout() );
p.setLayout( new GridLayout(1,1,5,5) );
canv11.setBackground( Color.yellow );
p.add(canv11);
add("Center",p);
add("North",b1);

}
}
class canv1 extends Canvas
{
public void paint(Graphics g)
{
Dimension dd=this.getSize();
double w=dd.getWidth();
double h=dd.getHeight();
setBackground( Color.cyan );
g.drawString("hi",10,25);
System.out.println("width= "+w+"height= "+h);
}
public Dimension minimumSize()
{
return new Dimension(100,50);
}
public Dimension preferredSize()
{
return minimumSize();
}
}
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Samiullah,
By placing your canvas into a GridLayout you are expanding it to fill the entire applet center (of BorderLayout). If you want a canvas to float in the center of the BorderLayout and not expand you need to use the dreaded GridBagLayout. See the code below for an example.

Regards,
Manfred.
Samiullah Aazam
Greenhorn

Joined: Jul 02, 2001
Posts: 7
Hello Manfred,
I am really very thankful to u for giving your precious time to solve my problem. Your approach really encouraged me to play with the canvas as per my requirement and understood that why GridBagLayout is more versatile that any other Layouts. Actually, i am working on a chat applet project & this was just the first step to understand the canvas in which later on i would like to place user messages one below the previous in the canvas. Now i can cofidently move towards my project. If any problem comes out in its development then i'll contact u without any hesitation . Hope u will help me in future too.
Thankx a lot! Bye!
samiullah Aazam
Samiullah Aazam
Greenhorn

Joined: Jul 02, 2001
Posts: 7
Hello Manfred!
I have read the GridBagLayout from some books but still unable to get the full grasp over this. Will u please tell me about some good sites(or internet resources) where i can get a nicely explained(with support of diagrams) deatails of GridBagLayout manager.
Hope to get a positive reply from your end.
Thankx! Bye!!!1
Samiullah Aazam
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why do this canvas is covering the entire area of the applet?