Hi
I’m making a tag cloud in Java by using the paint method to style my different size words.
My code:
Integer[] lengths = {1,7,3,12,1};
String[] words = {"horror","thriller","comedy","drama","action"};
public void paint(Graphics g)
{
Graphics2D g2;
g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
int w = getSize().width;
int h = getSize().height;
AffineTransform at = new AffineTransform();
at.setToTranslation(30, 50);
AffineTransform fontAT = new AffineTransform();
fontAT.shear(0.2, 0.0);
FontRenderContext frc = g2.getFontRenderContext();
String s = new String("Still Life with Text");
TextLayout tstring = new TextLayout(s, big, frc);
Font theDerivedFont = big.deriveFont(fontAT);
String str = new String("Still Life with Slanted Text");
TextLayout tstring2 = new TextLayout(str, theDerivedFont, frc);
for(int i = 0; i < lengths.length; i++)
{
int totalInt = lengths*;
if(totalInt <= 2)
{
g2.setFont(small);
g2.setColor(Color.lightGray);
g2.drawString(words*, 35, 35);
}
else if(totalInt > 2 && totalInt < 7)
{
g2.setFont(medium);
g2.setColor(Color.gray);
g2.drawString(words*, 55, 55);
}
else
{
g2.setFont(big);
g2.setColor(Color.darkGray);
g2.drawString(words*, 75, 75);
}
}
}
When the words are displayed, all the “small” words are placed over each other, and the same with the “medium” and “big” words. I also don’t want to define where the words must be displayed i.e. g2.drawString(words*, 75, 75); the x and y coordinates. The words must just be written next to each other in the same way they are read in.
I want the output to be:
[SIZE=1]horror[/SIZE], [SIZE=4]thriller[/SIZE], [SIZE=3]comedy[/SIZE], [SIZE=4]drama[/SIZE], [SIZE=1]action[/SIZE]
How can I modify my method to accommodate this?
Thanks