hi see th efollowing code which code of multi ICon class chechi can display multiple icons over JLabel. Now I want display Text..( image numbers ) over the icons. with Jlabel setText() i am not able to postion it correctly. Is there any way to display image unumber(SetText()) below or over the icons Added over a Jlabel.
Any sample code is highly appricated.
here is the code of MultiIcon I am using
package ateportal;
import java.awt.Component;
import java.awt.Graphics;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
/**
*
* @author deepuj
*/
public class MultiIcon implements Icon {
public static final int ALIGNMENT_HORIZONTAL = 1;
public static final int ALIGNMENT_VERTICAL = 2;
public static final int ALIGNMENT_STACKED = 3;
public ImageIcon thumbNail1 = null;
public ImageIcon thumbNail2 = null;
public ImageIcon thumbNail3 = null;
public ImageIcon boxPosIcon1 = null;
public ImageIcon boxPosIcon2 = null;
public ImageIcon boxPosIcon3 = null;
private int gap = 3; // will be ignored for stacked alignment
private Icon[] icons;
private int alignment;
private int width = 0;
private int height = 0;
public MultiIcon() {
this(ALIGNMENT_HORIZONTAL);
}
public MultiIcon(int alignment) {
this.alignment = alignment;
}
public int getIconCount() {
if (icons == null) {
return 0;
} else {
return icons.length;
}
}
public Icon getIconAt(int index) {
return icons[index];
}
public int getIconAt(int x, int labelx) {
//int y = point.y;
int firstIconWidth = getIconAt(0).getIconWidth();
//JOptionPane.showMessageDialog(null, "firstIconWidth = " +firstIconWidth);
int secondIconWidth = getIconAt(1).getIconWidth();
int thirdIconWidth = getIconAt(2).getIconWidth();
int fourthIconWidth = getIconAt(3).getIconWidth();
int fifthIconWidth = getIconAt(4).getIconWidth();
//JOptionPane.showMessageDialog(null, "fourthIconWidth = " +fourthIconWidth);
//JOptionPane.showMessageDialog(null, "fifthIconWidth = " +fifthIconWidth);
if (x - labelx > 0) {
if (x - (labelx + firstIconWidth) > 0) {
if (x - (labelx + firstIconWidth + secondIconWidth+(gap*1)) > 0) {
if (x - (labelx + firstIconWidth + secondIconWidth + thirdIconWidth+(gap*2)) > 0) {
if (x - (labelx + firstIconWidth + secondIconWidth + thirdIconWidth + fourthIconWidth+(gap*3)) > 0) {
if (x - (labelx + firstIconWidth + secondIconWidth + thirdIconWidth + fourthIconWidth+ fifthIconWidth +(gap*4)) > 0){
//outside
return 0;
}else{
return 5;
}
} else {
return 4;
}
} else {
return 3;
}
} else {
return 2;
}
} else {
return 1;
}
} else {
return 0;
}
}
// public int getIconAt(int x, int y, int labelx, int labely) {
// //int y = point.y;
// int firstIconWidth = getIconAt(0).getIconWidth();
// int secondIconWidth = getIconAt(1).getIconWidth();
// int thirdIconWidth = getIconAt(2).getIconWidth();
// int fourthIconWidth = getIconAt(3).getIconWidth();
// int fifthIconWidth = getIconAt(4).getIconWidth();
//
// if (x - labelx > 0) {
// if (x - (labelx + firstIconWidth) > 0) {
// if (x - (labelx + firstIconWidth + secondIconWidth+(gap*1)) > 0) {
// if (x - (labelx + firstIconWidth + secondIconWidth + thirdIconWidth+(gap*2)) > 0) {
// if (x - (labelx + firstIconWidth + secondIconWidth + thirdIconWidth + fourthIconWidth+(gap*3)) > 0) {
// if (x - (labelx + firstIconWidth + secondIconWidth + thirdIconWidth + fourthIconWidth+ fifthIconWidth +(gap*4)) > 0){
// //outside
// return 0;
// }else{
// return 5;
// }
//
// } else {
// return 4;
// }
//
// } else {
// return 3;
// }
//
// } else {
// return 2;
// }
//
// } else {
// return 1;
// }
//
// } else {
// return 0;
// }
//
// }
public void replaceIconAt(int index, Icon newIcon) {
icons[index] = newIcon;
width = calculateIconWidth();
height = calculateIconHeight();
}
public void addIcon(Icon icon) {
if (icon == null) {
return;
} else {
if (icons == null) {
icons = new Icon[]{icon};
} else {
Icon[] newIcons = new Icon[icons.length + 1];
System.arraycopy(icons, 0, newIcons, 0, icons.length);
newIcons[newIcons.length - 1] = icon;
icons = newIcons;
}
width = calculateIconWidth();
height = calculateIconHeight();
}
}
public void paintIcon(Component c, Graphics g, int x, int y) {
if (icons == null) {
return;
} else if (alignment == ALIGNMENT_VERTICAL) {
int yIcon = y;
for (int i = 0; i < icons.length; i++) {
Icon icon = icons[i];
int xIcon = x + (width - icon.getIconWidth()) / 2;
icon.paintIcon(c, g, xIcon, yIcon);
yIcon += icon.getIconHeight() + gap;
}
} else if (alignment == ALIGNMENT_STACKED) {
for (int i = 0; i < icons.length; i++) {
Icon icon = icons[i];
int xIcon = x + (width - icon.getIconWidth()) / 2;
int yIcon = y + (width - icon.getIconWidth()) / 2;
icon.paintIcon(c, g, xIcon, yIcon);
}
// for (int i = 0; i < icons.length; i++) {
// Icon icon = icons[i];
// int xIcon = x;
// int yIcon = y;
// if(i==1)
// {
// xIcon = x ;
// yIcon = y + (icon.getIconHeight()) / 2 + (icon.getIconHeight()) / 3 + (icon.getIconHeight()) / 3;
// icon.paintIcon(c, g, xIcon, yIcon);
// }
// else{
// //Icon icon = icons[i];
// // yIcon =y;
// xIcon = x + (width - icon.getIconWidth()) / 2;
// icon.paintIcon(c, g, xIcon, yIcon);
// yIcon += icon.getIconHeight() + gap;
// }
//
//
// }
} else {
assert alignment == ALIGNMENT_HORIZONTAL;
ImageIcon left = new ImageIcon("C:\\Documents and Settings\\Deepu James\\Desktop\\1.png");
int xIcon = x;
for (int i = 0; i < icons.length; i++) {
//yIcon = y + (height - icon.getIconHeight()) / 2;
//end of code
Icon icon = icons[i];
int yIcon = y -10 ;//+ (height - icon.getIconHeight()) / 2;
icon.paintIcon(c, g, xIcon, yIcon);
// code
// if(i!=0 && i!=4)
// {
// //yIcon = y;
// //xIcon = x;
// Icon icon1 = null;
// if (i==1)
// icon1 =this.boxPosIcon1;
// else if(i==2)
// icon1 =this.boxPosIcon2;
// else if(i==3)
// icon1 =this.boxPosIcon3;
// yIcon = yIcon + icon.getIconHeight()+3;
// int dx = xIcon + icon.getIconWidth()/2 - icon1.getIconWidth()/2;
// icon1.paintIcon(c, g, dx, yIcon);
// }
xIcon += icon.getIconWidth() + gap;
}
}
}
public int calculateIconWidth() {
if (icons == null) {
return 0;
} else if (alignment == ALIGNMENT_HORIZONTAL) {
int width = 0;
for (int i = 0; i < icons.length; i++) {
width += icons[i].getIconWidth();
}
width += gap * (icons.length - 1);
return width;
} else {
int width = 0;
for (int i = 0; i < icons.length; i++) {
width = Math.max(width, icons[i].getIconHeight());
}
return width;
}
}
public int calculateIconHeight() {
if (icons == null) {
return 0;
} else if (alignment == ALIGNMENT_VERTICAL) {
int height = 0;
for (int i = 0; i < icons.length; i++) {
height += icons[i].getIconWidth();
}
height += gap * (icons.length - 1);
return height;
} else {
int height = 0;
for (int i = 0; i < icons.length; i++) {
height = Math.max(height, icons[i].getIconHeight());
}
return height;
}
}
public int getIconWidth() {
return width;
}
public int getIconHeight() {
return height;
}
}