• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
 
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,
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
mooooooo ..... tiny ad ....
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic