wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes regarding custom component development Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "regarding custom component development" Watch "regarding custom component development" New topic
Author

regarding custom component development

ch praveen
Ranch Hand

Joined: Mar 08, 2004
Posts: 67
Hello Friends,
Iam using a custom java.awt component (ImageButton) in an applet. I got some doubts which are listed below and the concered programs are also posted. If possible please clarify my doubts.
[1] When I try to include the ImageButton component in an Applet for which BorderLayout is set, the component is occupying the whole applet width. When the layout is FlowLayout() [default one], it's working fine. Reason Please ?
[2] When the below steps are not implented in ImageButton (custom component) constructor, component width and height in Applet extracted by means of getSize() method are returned as 0 respectively even if the image(custom component) is displayed in Applet. Reason Please ?
step 1. super.resize(size);
step 2. resize(size);
[3] When I first time browse the webpage that contains this Applet, nothing is displayed (i.e gray screen is displayed and a variable,'size' of type Dimension which holds component size contains width and height as -1 respectively). After that when I press refresh button, then image (custom component) is displayed and a variable,'size' of type Dimension which holds component size contains image width and height correctly.
[4] Please state what are the things that we must keep in mind while developing a custom component and to avoid all the pain.

------- Custom Component [ImageButton] Code -------
import java.awt.*;
public class ImageButton extends Canvas
{
public Image image;
private String command;
private boolean selected;
public Dimension size;
public ImageButton(Image img, String command)
{
selected = false;
image = img;
this.command = command;
if(image == null)
size = new Dimension(0, 0);
else
size = new Dimension(image.getWidth(this), image.getHeight(this));
super.resize(size);
resize(size);
}
public Dimension preferredSize()
{
return minimumSize();
}
public Dimension minimumSize()
{
return size;
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
g.drawImage(image, 0, 0, this);
if(selected)
{
g.setColor(Color.yellow);
g.drawRect(0, 0, image.getWidth(this) - 1, image.getHeight(this) - 1);
}
}
public boolean handleEvent(Event e)
{
if(e.id == 502 && selected)
{
e.id = 1001;
e.arg = command;
}
if(e.id == 504)
{
selected = true;
repaint();
return true;
}
if(e.id == 505)
{
selected = false;
repaint();
return true;
} else
{
return super.handleEvent(e);
}
}
};

------- Applet Code -------
/*
<applet code="ImageButtonTest" height="400" width="400">
</applet>
*/
import java.awt.*;
import java.applet.*;
import java.net.URL;
public class ImageButtonTest extends Applet
{
Image img = null;
ImageButton ib = null;
public void init()
{
//setLayout(new BorderLayout());
URL url=null;
try
{
url = new URL(getCodeBase(),"../icon/1icon.jpg");
}
catch (Exception e) { }

img = getImage(url);
ib = new ImageButton(img,"This is Button 1");
ib.setBackground(Color.black); // This step is to track the ImageButton (ib) component width.
add(ib);
Dimension d = ib.getSize();
Label lbl = new Label("w=" + d.width + ",h=" + d.height + (img==null?",nullimg":",notnullimg") + (ib.image==null?",nullib.imgae":",notnullib.image") + ",ib.size.width=" + ib.size.width + ",ib.size.height=" + ib.size.height);
add("South",lbl);
repaint(); // If we discard this step, button will not appear. (we may include super.repaint() in the custom component's paint() method to exclude this step.
}
public void paint(Graphics g) {
/* The below steps are included to ensure that image is valid and properly passed to ImageButton component */
//g.drawImage(img,0,0,this);
//g.drawImage(ib.image,150,0,this);
}
};
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Some answers to your questions:
1 - When you add to a BorderLayout without specifying a constraint, eg, BorderLayout.EAST, or "East", the default is to add the component to the center. Look in the BorderLayout api and see that any component added to the center section is expanded in both horizontal and vertical dimensions to fill the available space. FlowLayout respects the preferred size of the components it manages, see FlowLayout api, last sentence at top. If you enjoy making custom components you will discover GridBagLayout before too long. It is very respectful of the preferred sizes of its components.

2 - Why is getSize returning 0? Probably because your image has not loaded yet. The getImage method returns immediately, not waiting around for the image to be full loaded. So we use a MediaTracker to block the flow of program execution until the image has fully loaded. Also, image components need to let their containers (that load them) know what size they are. Overriding getPreferredSize and returning the desired Dimension takes care of this. Here we add a little extra (PAD) so our border will show when the Canvas is selected.

3 - Image is not loaded so returns "-1" to all requests for the image width and height. Image shows up fine after loading has completed.

4 - Learning to develop custom components seems to involve pain associated with a steep learning curve involving many disparate areas in Java. Practice, patience and a sense of humor are good allies.

And an unsolicited comment:
You seem to be using some old code, eg, looking in the Event class api it tells us it is obsolete and we should use the AWTEvent class instead. This code is written and compiled with J2SE 1.4+.
ch praveen
Ranch Hand

Joined: Mar 08, 2004
Posts: 67
Thank You Craig Wood for your valuable guidance. One small doubt - While incorporating methods such as setSize(), setLocation(), setVisible(), setBounds() etc., we must follow any order or we can use these methods randomly without any problem. I got this doubt because some methods are not producing any reaction at all. (eg. changing a Frame location with setLocation() or setBounds() method).
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
In general a call to one of setVisible, show or pack will create the peer (heavyweight component) and layout the components in the gui. The gui is "realized". After this any calls to methods like setSize, setLocation, setBounds won't change things unless you tell java you have made the change and that you want it to show the changes. We usually do this by calling one or both of (Container) validate and repaint on the appropriate component. This seems to take practice and experimentation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regarding custom component development
 
Similar Threads
Layout Problem, Help Me Plz
How to code a button with image
Help with paint to panel
creating an application similar to ms paint in java...
placing image on jpanel and then add other components