• 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

From JONH hunts mock exam

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In order to cause the paint(Graphics) method to execute, which of the following is the most appropriate method to call:
1 paint()
2 repaint()
3 paint(Graphics)
4 update(Graphics)
5 None � you should never cause paint(Graphics) to execute
the ans was 2 , but i guess shouldnt be 1.

//code 2
import java.awt.*;

public class FrameTest extends Frame {
public FrameTest() {
add (new Button("First"));
add (new Button("Second"));
add (new Button("Third"));
pack();
setVisible(true);
}
public static void main(String args []) {
new FrameTest();
}
}

IN the above code there is no change if i remove pack. what is pack meant for. and also it prints "third" which occupies the frame size why?
plz help thanks in advance
------------------
"Winners don't do different things
They do things differently"
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sunil,
Regarding your first question, I would like to say that the correct answer is 2. In most of the cases we want to clear the drawing area and draw afresh. The repaint() will call update() which will sets the background color of drawing region(clears the drawing area) and calls paint().
regarding the second question,
pack() is to resize the frame so that it occupies the appropriate size in which the components can be fit in.
The default layout of Frame is BorderLayout.
And in this layout policy, adding components to the Frame
without specifying the orientation results in the components added at the center. In your code you are adding three buttons without specifying the orientation, so all the buttons are added at center and third button is visible since it replaces all the previously added buttons in the center.
Hope this clears your doubt.
Regards
------
vadiraj.
------------------
*************************
There's a lot of I in J.
*************************
 
sunilkumar ssuparasmul
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks vadiraj
But in second case even if i dont specify pack it eould adjust 2 the specified size since it is a frame uses border layout as default. can u tell me some example where can i look at the use od pack() command?
thanks,
sunil.s
------------------
"Winners don't do different things
They do things differently"
 
vadiraj vd
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sunilkumar,
As BorderLayout doesn't consider the component's preferred size, pack()(and other methods like- setBounds(), setSize()) will not have any effect.
Change the layout of a Frame to FlowLayout which will take
the component's preferred size into account.
The following code demonstrates the use of pack().
The method pack() is useful in cases you want to set the container size exactly required by the components.
Try commenting/de-commenting the lines 1 and 2 and run the program.
You'll notice the difference.
Code
--------------
<PRE>

import java.awt.*;
public class PackDemo extends Frame
{
Label lUName,lPwd;
TextField tUName,tPwd;
Button bOk,bCancel;
public PackDemo()
{
lUName = new Label("Username:");
lPwd = new Label("Password:");
tUName = new TextField(20);
tPwd = new TextField(20);
bOk = new Button("OK");
bCancel = new Button("Cancel");
setLayout(new FlowLayout()); // change layout to FlowLayout.

//add all the components to Frame.
add(lUName);add(tUName);add(lPwd);add(tPwd);add(bOk);add(bCancel);
}

public static void main(String a[])
{
PackDemo p = new PackDemo();
//p.setSize(500,500); // line 1.
p.pack(); // line 2.
p.setVisible(true);
}
}

</PRE>
---------------
Hope this clears your doubt.
Regards
--------
vadiraj

------------------
*************************
There's a lot of I in J.
*************************
[This message has been edited by vadiraj vd (edited December 21, 2000).]
 
sunilkumar ssuparasmul
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks vadiraj that made me clear where pack has 2 be used
------------------
"Winners don't do different things
They do things differently"
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To go back and elaborate on your first question,
repaint() is the preferred way to call paint() because repaint() calls update which schedules the time to call paint() to the screen. Calling paint() directly or calling udpate() directly is more inefficient and since the question said what is the APPROPRIATE way, repaint() is the correct answer.
Bill
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vadiraj,

Thanks for a very nice example. Can u please tell me which all layout managers are affected when the pack() method is used. ???
Does pack() only work where the layoutmanagers "honors the preferred size of its components". ?
Thanks,
Aruna
 
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic