Is there a way in Java Swing to show text in small caps (small capitals)?
http://en.wikipedia.org/wiki/Small_caps
I have loaded a custom font which supports small caps via Font.createFont(). The text should be rendered somehow in small caps. Is this possible in JLabel, JTextPane or some other component?
You can try that, it works : (I get the font here for test : http://www.fonts101.com/fonts/view/Uncategorized/28374/Tahoma_Small_Cap.aspx)
String labelText ="Dfd";
JLabel lbl = new JLabel(labelText);
Font g=null;
Font g2=null;
try {
InputStream myStream = new BufferedInputStream(new FileInputStream("/home/alain/Bureau/tahomscb/tahomscb.ttf"));
g = Font.createFont(Font.TRUETYPE_FONT, myStream);
g2 = g.deriveFont(Font.PLAIN, 24);
} catch (Exception ex) {
ex.printStackTrace();
System.err.println("font not loaded.");
}
lbl.setFont(g2);
this.add(lbl); //this is the parent component
Related
So I am working on a small project using Swing and I am trying to add a font to a JLabel, the font is a little bit weird it's called you murderer bb, I already am using a font that I added and it works fine, but when I do the exact same thing to this one well... it just displays a regular font.
private Font font;
File fontFile = new File("resources\\fonts\\Nunito-Regular.ttf");
try {
font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
font = font.deriveFont(14f);
} catch (FontFormatException | IOException e) {
e.printStackTrace();
}
private Font titleFont;
fontFile = new File("resources\\fonts\\youmurdererbb_reg.ttf");
try {
titleFont = Font.createFont(Font.TRUETYPE_FONT, fontFile);
titleFont = font.deriveFont(40f);
} catch (FontFormatException | IOException e) {
e.printStackTrace();
}
private JLabel title;
title = new JLabel("Welcom To Eureka");
title.setFont(titleFont);
title.setHorizontalAlignment(SwingConstants.CENTER);
title.setForeground(Color.decode("#FFFFFF"));
title.setBounds(228, 125, 354, 50);
private JLabel username;
username = new JLabel("Log In");
username.setFont(font);
username.setHorizontalAlignment(SwingConstants.CENTER);
username.setForeground(Color.decode("#BB86FC"));
username.setBounds(682, 80, 48, 20);
username.addMouseListener(new AppControler());
so the username is working fine and displaying the correct font but the title is just displaying a bigger font (i set the size of it to 40) but the font is not the one I am using
titleFont = Font.createFont(Font.TRUETYPE_FONT, fontFile);
titleFont = font.deriveFont(40f);
Should be:
titleFont = Font.createFont(Font.TRUETYPE_FONT, fontFile);
titleFont = titleFont.deriveFont(40f); // <- use the font just created!
Result (once 1st word spelling changed):
Hi I am unsure on how to format my animated gif images to let them show on Jars created on eclipse.
try {
//ImageIcon titleIcon = new ImageIcon(ImageIO.read(getClass().getResource("title.gif")));
title = new ImagePicture (new ImageIcon(ImageIO.read(getClass().getResource("title.gif"))), 0, 0);
//title = new ImagePicture (new ImageIcon("title.gif"), 0, 0);
}//end try
catch (IOException e) {
}//end catch
//set title bounds
title.setBounds(260, 0, 400, 100);
That is my code right now for an animated GIF, Thank you for your input.
Hard to give a good example without more context, but you can try adding the ImageIcon to a JLabel like this.
URL url = this.getClass().getResource("title.gif");
Icon title = new ImageIcon(url);
JLabel titleLbl = new JLabel(title);
//You have to add titleLbl to a container after this, of course
I am writing a code to make an image file of a chart appearing on a panel. For that purpose I create the buffered image of that and then use ImageIO.write(). It works but it only displays the panel(grey coloured panel) but does not show the chart present on that panel. What to do in this case?? Here is my code
com.objectplanet.chart.NonFlickerPanel p =
new com.objectplanet.chart.NonFlickerPanel(new BorderLayout());
p.add("Center", chart); // this statements adds the chart in the center of the panel
ChartPanel.add("Center", p);
ChartPanel.setSize(500, 200);
ChartPanel.show();
int w = ChartPanel.getWidth();
int h = ChartPanel.getHeight();
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
ChartPanel.paint(g);
ChartPanel.printAll(g);
File f = new File("D:\\image.png");
try {
// png is an image format (like gif or jpg)
ImageIO.write(bi, "png", f);
} catch (IOException ex) {
ex.printStackTrace();
}
Well i solved the problem .Anyone facing the same problem ,here is the solution
Use paintall function rather than just paint function
Pretty straightforward issue. My Java AWT (not Swing) label is simply not showing up. Most of the following code isn't even being used (for debugging this issue).
Just a note: this is within a Frame's constructor (and yes I have added several other panels and such that work just fine). Secondly, the frame's layout has been set to null.
I'm stumped.
File inf = new File("instructions.txt");
Label ilb;
if(inf.exists())
{
Log.v("Loading instructions");
try
{
FileInputStream fis = new FileInputStream(inf);
byte[] insb = new byte[65535];
fis.read(insb);
fis.close();
String inst = new String(insb);
ilb = new Label("test", Label.LEFT);
File fntfile = new File("font/pf_tempesta_seven.ttf");
Font infnt = null;
try {
FileInputStream ffis = new FileInputStream(fntfile);
infnt = Font.createFont(Font.TRUETYPE_FONT, ffis);
ffis.close();
} catch (FontFormatException e) {
Log.e("Could not format LCD font!", e);
} catch (IOException e) {
Log.e("Could not read LCD font file!", e);
}
if(infnt == null)
infnt = new Font("Trebuchet MS", Font.PLAIN, 8);
else
infnt = infnt.deriveFont(8.0f);
//ilb.setFont(infnt);
//ilb.setForeground(new Color(123, 123, 123));
//ilb.setPreferredSize(new Dimension(350, 400));
//ilb.setSize(350, 400);
//ilb.setLocation(580, 190);
Log.d("adding label");
add(ilb);
} catch(IOException e) {
Log.e("Could not read instructions!", e);
}
}else
Log.w("Instructions file not found!");
1) for todays GUI use Swing JComponents (starts with J) rather than prehistoric AWT Label
2) for your issue could be better use JTextArea with method append()
3) you have got issues with Concurency (in Swing) AWT / Swing is single threaded and all output to the GUI must be wrapped into invokeLater
4) for better help sooner you have edit your question with SSCCE
As #JBNizet suggested, null layouts don't work with all AWT components.
I was thrown off since my Panels were positioned just fine with a null layout on my Frame, whereas Labels require a basic layout in order to display. I was tempted to go as far as saying all other components had the same 'feature', but another part of my code proved that point wrong:
// Load Image
Log.v("Loading header image");
_iBG = new ImageIcon("img/hpcount_top_bg.png").getImage();
// Set size
setSize(1024, 152);
setPreferredSize(new Dimension(1024, 152));
// Set position
setLocation(0, 0);
// Set visible
setVisible(true);
// Set layout
setLayout(null);
// Add children
add(new Exit()); // Exit extends java.awt.Button
The above code (which is located within the constructor of a class extending java.awt.Panel) works perfectly.
My workaround is to put the label inside another Panel with a layout (messy, but it works) and position that panel within the Frame absolutely to achieve the same effect.
Usually, when I initialize the fonts I want to use in my SWING applications, I do it this way:
public static final Font TITLEFONT = new Font("Calibri", Font.BOLD, 40);
Now, I have to do it a bit differently since I'm using some custom fonts from a .ttf file. I initialize the font this way:
try
{
InputStream is = OptionsValues.class.getResourceAsStream("fonts//KOMIKAX_.ttf");
TITLEFONT = Font.createFont(Font.TRUETYPE_FONT, is);
}
catch (Exception ex)
{
ex.printStackTrace();
System.err.println("Font not loaded. Using Calibri font.");
TITLEFONT = new Font("Calibri", Font.BOLD, 40);
}
I'm pretty sure it initializes it correctly (I can't tell for sure since it is too small for me to see), but I'd like to know how I can manually set the font's size (and if it's bold / other attributes) when loading a font this way.
Thanks a lot in advance!
createFont returns a Font and you can call deriveFont(...) on this, passing in a float for the point size, or an int and float for Font style and point size. I cannot say whether it will work for your particular situation, but it's worth a try.
e.g.,
InputStream is = OptionsValues.class.getResourceAsStream("fonts//KOMIKAX_.ttf");
TITLEFONT = Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(Font.BOLD, 40f);
I'd simply use:
Font.ITALIC
Font.BOLD
Font.PLAIN