Overlay 1 icon with another

Good afternoon...

basically i want to create a TreeCellRenderer that takes the standard icon (a folder or a file) and puts another icon over it (so you see both at the same time).

i'd like to use the standard icons and just add mine on top, so what i really need is a way to merge two icons into one.

[327 byte] By [EricSchultz] at [2007-9-26 3:10:29]
# 1
Create a custom TreeCellRenderer that extends some subclass of Component (usually JLabel), andpaint your second icon on top of the standard one in the custom TreeCellRenderer's paint() method.
chuanhaochiu at 2007-6-29 11:17:03 > top of Java-index,Archived Forums,Swing...
# 2
I have a freeware class that creates composite icons, if you want it, drop me an email.Mitch GoldsteinAuthor, Hardcore JFC (Cambridge Univ Press)mdgoldstein@hotmail.com
mitchg at 2007-6-29 11:17:03 > top of Java-index,Archived Forums,Swing...
# 3
Good afternoon...but would that not just replace the standard icon with mine? i'm wondering what manipulation of images i have to do to get them to appear as one icon.
EricSchultz at 2007-6-29 11:17:03 > top of Java-index,Archived Forums,Swing...
# 4
Good afternoon...thanks mitch, but i'd really like to know how it's done - not just do it. would you mind posting the source for your class?
EricSchultz at 2007-6-29 11:17:03 > top of Java-index,Archived Forums,Swing...
# 5
I will be moving the source to SourceForge where anyone who is interested can see it. E-mail me if you want a notification.Mitch GoldsteinAuthor, Hardcore JFC (Cambridge Univ Press)mdgoldstein@hotmail.com
mitchg at 2007-6-29 11:17:03 > top of Java-index,Archived Forums,Swing...
# 6
- write your own JComponent extending JLable- load the to icons (gif's with transperent background) in an Image object each- in the paint method paint the two images one after the other on the same positon.- use the component in you treecellrendererSpieler
spieler at 2007-6-29 11:17:03 > top of Java-index,Archived Forums,Swing...
# 7
Good morning...i've been trying to figure this one out, but the paint method is deeper into the api than i've ever gone before. would you mind providing me some code, or some more tips on how to do this?thanks.
EricSchultz at 2007-6-29 11:17:03 > top of Java-index,Archived Forums,Swing...
# 8

Ok, I'm writing this without a compiler at hand

so doned blame me if some syntax is messed up,

but you should be able to figure it out:

public class TwoIconLabel extends JLabel{

// for the test version (see below) you can leave out

// the following two lines and the constructor

Image icon1;

Image icon2;

TwoIconLabel(Image i1, Image i2){

// this or something like this is needes, so the

// size of the component meets the size of the icons.

setSize(10,10);

icon1 = i1;

icon2 = i2;

}

public void paint(Graphics g){

// use these two lines for a real version

g.drawImage(icon1,0,0,this);

g.drawImage(icon2,0,0,this);

// or use these two for checking if the whole paint thinggy works:

g.drawLine(0,0,10,10);

g.drawLine(0,10,10,0);

}

}

Hint you can put your TwoIconLabel in an ordinary GUI

to see how it looks, before putting it in a TreeRenderer.

Spieler

spieler at 2007-6-29 11:17:03 > top of Java-index,Archived Forums,Swing...
# 9

Good afternoon...

well, thanks for the code sample. following your suggestion i dug down into the sources. aiee-caramba! that's more than i wanted to change. also i figured out what i'd have to override was the BasicLabelUI.

but, i was inspired and looked at the code of ImageIcon and then i came up with the following class which does the trick quite nicely:package Schultz.swing;

import javax.swing.Icon;

import java.awt.Component;

import java.awt.Graphics;

import javax.swing.SwingConstants;

/**

* Creates a composite icon by combing two icons, the base and the overlay. The base

* must be bigger than the overlay or the overlay will be rejected! The base icon

* must be set first. The position of the overlay is determined by the

* SwingConstants N, NE, NW, etc.

*

* @author Eric Schultz

* @version CompositeIcon 1.00 Aug 15, 2001

*/

public class CompositeIcon implements Icon, SwingConstants {

/**

* The version of the class

*/

public static final String version = "CompositeIcon 1.00 Aug 15, 2001";

/**

* The icon that serves as the base image

*/

protected Icon base;

/**

* The icon that is drawn over the base

*/

protected Icon overlay;

/**

* The position the user requests the overlay be put in

*/

protected int position;

/**

* The overlay's X position relative to the base

*/

protected int ox;

/**

* The overley's Y position relative to the base

*/

protected int oy;

/**

* Creates a new composite icon from two existing icons and places the overlay in the

* specified position

* @param base the base icon

* @param overlay the icon to draw on top of the base

* @param position the location of the overlay relative to the base

*/

public CompositeIcon(Icon base, Icon overlay, int position) {

setBaseIcon(base);

setOverlayIcon(overlay, position);

}

/**

* Sets the base icon

* @param base base icon, does nothing if this parameter is null

*/

public void setBaseIcon(Icon base) {

if (base != null)

this.base = base;

}

/**

* Returns the base icon

* @returns the base icon

*/

public Icon getBaseIcon() {

return this.base;

}

/**

* Sets the overlay icon and calculates the new position

* @param overlay the icon to overlay, does nothing if the base and overlay are null

*/

public void setOverlayIcon(Icon overlay) {

if (base != null && overlay != null) {

if (overlay.getIconHeight() <= base.getIconHeight() && overlay.getIconWidth() <= base.getIconWidth()) {

this.overlay = overlay;

calcPosition();

}

}

}

/**

* Sets the new overlay icon and the new position

* @param overlay the new icon to overlay

* @param position the new position for the overlay

*/

public void setOverlayIcon(Icon overlay, int position) {

setOverlayIcon(overlay);

setPosition(position);

}

/**

* Returns the overlay icon

* @returns the overlay icon

*/

public Icon getOverlayIcon() {

return this.overlay;

}

/**

* Sets the new position. Base on the SwingConstants cardinal constants (NORTH, etc.)

* @param the position of the overlay (1 of 9).

*/

public void setPosition(int position) {

this.position = position;

calcPosition();

}

/**

* Returns the position of the overlay

* @returns the position of the overlay

*/

public int getPosition() {

return this.position;

}

/**

* Calculates the position of the overlay relative to the base icon

*/

protected void calcPosition() {

if (base != null && overlay != null) {

switch (position) {

case NORTH_EAST:

ox = base.getIconWidth() - overlay.getIconWidth();

oy = 0;

break;

case NORTH:

ox = (base.getIconWidth() - overlay.getIconWidth()) / 2;

oy = 0;

break;

case NORTH_WEST:

ox = 0;

oy = 0;

break;

case WEST:

ox = 0;

oy = (base.getIconHeight() - overlay.getIconHeight()) / 2;

break;

case CENTER:

ox = (base.getIconWidth() - overlay.getIconWidth()) / 2;

oy = (base.getIconHeight() - overlay.getIconHeight()) / 2;

break;

case EAST:

ox = base.getIconWidth() - overlay.getIconWidth();

oy = (base.getIconHeight() - overlay.getIconHeight()) / 2;

break;

case SOUTH_EAST:

ox = base.getIconWidth() - overlay.getIconWidth();

oy = base.getIconHeight() - overlay.getIconHeight();

break;

case SOUTH:

ox = (base.getIconWidth() - overlay.getIconWidth()) / 2;

oy = base.getIconHeight() - overlay.getIconHeight();

break;

case SOUTH_WEST:

ox = 0;

oy = base.getIconHeight() - overlay.getIconHeight();

break;

default :

ox = (base.getIconWidth() - overlay.getIconWidth()) / 2;

oy = (base.getIconHeight() - overlay.getIconHeight()) / 2;

break;

}

}

}

/**

* Returns the height of the base icon

* @returns the height of the icon

*/

public int getIconHeight() {

return base.getIconHeight();

}

/**

* Returns the width of the icon

* @returns the width of the base icon

*/

public int getIconWidth() {

return base.getIconWidth();

}

/**

* Paints the two icons, calling each of their paintIcon methods

* @param c the GUI component inw which to paint this icon

* @param g the graphics object which contains the icon

* @param x the x location of the base icon

* @param y the y location of the base icon

*/

public void paintIcon(Component c, Graphics g, int x, int y) {

base.paintIcon(c, g, x, y);

overlay.paintIcon(c, g, x+ox, y+oy);

}

}

thanks for your help.

EricSchultz at 2007-6-29 11:17:03 > top of Java-index,Archived Forums,Swing...
# 10

I must have been have a sleep when I wrote my last posting.

proceed like follows:

create a bufferedImage.

get the Graphic from it.

using that Graphic paint the first Image (it will apear only

in memory, not anywhere on the display

using that Graphic paint the second Image

set the Icon of the label to that image (the buffered Image)

for details see the graphic trail in the java tutorial.

sorry for making stuff so complecated

Spieler

spieler at 2007-6-29 11:17:03 > top of Java-index,Archived Forums,Swing...