Hello all,
Can someone please help? Im trying to add an outline to a JLabel text but im after hitting a wall. Below is a section of code ive wrote which opens a colorchooser - adds the color to the text and converts RGB to Hex, blah, blah, blah....
private void OP_Text_PanelMouseClicked(java.awt.event.MouseEvent evt) {
Color OP_text_color = JColorChooser.showDialog(this,"Choose Color",getBackground());
OP_text_color.getRGB();
System.out.println("hex "+ Integer.toHexString( OP_text_color.getRGB() & 0x00ffffff ));
if (OP_text_color != null)
{
OP_Text_Panel.setBackground(OP_text_color);
OP_text_textfield.setText(Integer.toHexString(OP_text_color.getRGB() & 0x00ffffff));
displayOpTextLabel.setForeground(OP_text_color);
new FontPaint().setVisible(true);
}
}
.... but i cant get the outline of the text to work. After researching the problem at hand i came across the following piece of code but im a bit stuck to say the least...
import java.awt.*;
import javax.swing.*;
import java.awt.font.*;
import java.awt.geom.AffineTransform;
public class FontOutline extends JApplet {
public void initialize() {
FontPaint font1 = new FontPaint();
getContentPane().add(font1, BorderLayout.CENTER);
}
public static void main(String[] args) {
FontPaint font2 = new FontPaint();
JFrame frame = new JFrame("Outlining Font");
frame.getContentPane().add(font2, BorderLayout.CENTER);
frame.setSize(new Dimension(350, 200));
frame.setVisible(true);
}
}
class FontPaint extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.white);
int w = getSize().width;
int h = getSize().height;
Graphics2D g2d = (Graphics2D) g;
FontRenderContext fontRendContext = g2d.getFontRenderContext();
Font font = new Font("Monotype Corsiva", 2, 50);
String st = new String("Hello World");
TextLayout text = new TextLayout(st, font, fontRendContext);
AffineTransform affineTransform = new AffineTransform();
Shape shape = text.getOutline(null);
Rectangle rect = shape.getBounds();
affineTransform = g2d.getTransform();
affineTransform.translate(w / 2 - (rect.width / 2), h / 2
+ (rect.height / 2));
g2d.transform(affineTransform);
g2d.setColor(Color.blue);
g2d.draw(shape);
g2d.setClip(shape);
}
}
Can anyone could point me in the right direction please?