I have downloaded akshar.ttf file and want to add it to my java project. I have tried the following ways by searching online but nothing worked so far.
Try 1:
Font ttfBase = null;
Font ttfReal = null;
try {
InputStream myStream = new BufferedInputStream(new FileInputStream("akshar.TTF"));
ttfBase = Font.createFont(Font.TRUETYPE_FONT, myStream);
ttfReal = ttfBase.deriveFont(Font.PLAIN, 24);
} catch (Exception ex) {
ex.printStackTrace();
System.err.println("akshar font not loaded.");
}
Try 2:
Font font = new Font("akshar",Font.PLAIN,15);
I have the akshar.ttf file at the following places:-
java/jre/lib/fonts
bin folder of my project
src folder of my project
I am new to java and have tried all these by following various links online. Please help me where am i going wrong.
You can register the created font with the graphics environment , as below :
try {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("akshar.TTF"));
} catch (IOException|FontFormatException e) {
//Handle exception
}
Refer the Java tutorial.
Put your ttf into the assests folder :)
Related
I have an enterprise application client and I want a custom font.
Exception:
java.io.IOException: Problem reading font data.
at java.awt.Font.createFont0(Font.java:1000)
at java.awt.Font.createFont(Font.java:877)
at de.fh_dortmund.inf.cw.surstwalat.client.user.view.LoginPanel.<init>(LoginPanel.java:68)
Code:
public void loaderFonts() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
// ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("resources/fonts/closeandopen.ttf")));
InputStream stream = ClassLoader.getSystemClassLoader().getResourceAsStream("/resources/fonts/closeandopen.ttf");
Font font = Font.createFont(Font.TRUETYPE_FONT, stream).deriveFont(16f);
ge.registerFont(font);
} catch (IOException | FontFormatException e) {
System.err.println(e.getMessage());
// } finally {
// String[] fontnames = ge.getAvailableFontFamilyNames();
// for (String fontname : fontnames) {
// System.out.println(fontname);
// }
}
I tried a few things, but found no solution.
different fonts
different ways to see comments
paths
etc
maybe some ideas?
So I'm trying to make a simple application, and I got a window appearing. The problem is that I require some special font, which I doubt would be installed on the user's OS, so I've got to load it from a file.
I've read countless articles and I've been stuck on this for quite a while and I'm not sure if the font isn't being loaded or it's not registering. I've also gotten confused with the type of "Stream" to use, because countless tries with InputStream, FileInputStream and BufferStream have lead to nothing.
The font is being copied to the output directory. This is my project structure
<packages-with-classes>
fonts
-> gameFont.ttf
This is the code I'm using
displayArea = new JTextArea();
displayArea.setEditable(false);
//FONT to give graphics (plz help me)
GraphicsEnvironment ge = null;
try {
//ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
/*Font initFont = Font.createFont(Font.TRUETYPE_FONT, Main.class.getClassLoader().getResourceAsStream("/fonts/gameFont.ttf") );
Font fontBase = initFont.deriveFont(Font.PLAIN, 20);*/
Font gameFnt = Font.createFont(Font.TRUETYPE_FONT, Main.class.getClassLoader().getResourceAsStream("fonts/gameFont.ttf")).deriveFont(Font.PLAIN, 12f);
GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(gameFnt);
displayArea.setFont(gameFnt);
} catch (IOException e) {
e.printStackTrace();
} catch(FontFormatException e) {
e.printStackTrace();
}
So after a really really huge headache, I got it working :)
Here's the code if anyone else is stuck on the same problem:
InputStream fontStream = Main.class.getResourceAsStream("/fonts/gameFont.ttf");
try {
Font gameFont = Font.createFont(Font.TRUETYPE_FONT,fontStream);
gameFont=gameFont.deriveFont(Font.PLAIN, 14);
displayArea.setFont(gameFont);
} catch (FontFormatException | IOException e) {
e.printStackTrace();
}
I am able generate the extent reports with screenshots on my local machine.
But when i mail the reports to someone else, or open the html on a differant machine, the screenshots are not visible. It says that the path is invalid.
While attaching the screenshot, i am giving the path of my local machine. And it is searching the same path on other machine too.
I tried zipping the html and pics in one folder too.
Please help me how to attach the screenshots into html file without local machine dependency.
You can do this by using base64 conversion of the obtained screenshots.
Use the following code in your framework and try it.
public static String addScreenshot() {
File scrFile = ((TakesScreenshot) BasePage.driver).getScreenshotAs(OutputType.FILE);
String encodedBase64 = null;
FileInputStream fileInputStreamReader = null;
try {
fileInputStreamReader = new FileInputStream(scrFile);
byte[] bytes = new byte[(int)scrFile.length()];
fileInputStreamReader.read(bytes);
encodedBase64 = new String(Base64.encodeBase64(bytes));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "data:image/png;base64,"+encodedBase64;
}
I faced same issue.
Share the folder in which you are storing the screenshots with everyone and return same path in below method.
public static String getScreenshot(WebDriver oDriver, String ScreenShotName) throws IOException
{
String dateName=new SimpleDateFormat("YYYYMMDDHHMMSS").format(new Date());
TakesScreenshot ts=(TakesScreenshot)oDriver;
File source=ts.getScreenshotAs(OutputType.FILE);
String destination=System.getProperty("user.dir")+"/FailedScreenshots/"+ScreenShotName+dateName+".png";
File finalDestination=new File(destination);
FileUtils.copyFile(source, finalDestination);
String Imagepath="file://Machinename/FailedScreenshots/"+ScreenShotName+dateName+".png";
return Imagepath;
}
Hope this helps!
Everything has been working fine in my level editor program I've been working on until I decided to export the JAR file for a beta version. All the image loading works perfectly in Eclipse but once I try to run it as an external JAR file I get this error: http://puu.sh/kP681/1be9b77e21.png
Here is the code I'm using to load the image, it is working in Eclipse.
public static void main(String[] args) {
BufferedImage appIcon = null;
try {
appIcon = ImageIO.read(new File("res" + File.separator + "appicon.png"));
} catch (IOException e1) {
e1.printStackTrace();
}
JFrame frame = new JFrame("Mario Map Creator");
frame.setSize(RESELOUTION);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setIconImage(appIcon);
That doesn't appear to be working, that is the MapCreator class.
Then the MapCreatorPanel class
public MapCreatorPanel() {
this.setLayout(null);
try {
levelIcon = new ImageIcon(ImageIO.read(new File("res" + File.separator + "levelicon.png")));
flagIcon = new ImageIcon(ImageIO.read(new File("res" + File.separator + "flagicon.png")));
scriptIcon = new ImageIcon(ImageIO.read(new File("res" + File.separator + "scripticon.png")));
} catch (IOException e) {
e.printStackTrace();
}
scriptManager = new ScriptManager();
}
Thanks for your help in advance!
ALSO NOTE: I've looked at a bunch of other posts on this site and all their solutions didn't seem to work for me (unless I was doing something wrong).
I'm doing a simple Pong game and wanted to add an 8-Bit font but can not figure how.
This is the method I used for JLabels:
public void drawScore()
{
player1 = "Player 1";
player2 = "Player 2";
JLabel leftScore = new JLabel(player1);
JLabel rightScore = new JLabel(player2);
leftScore.setForeground(Color.white);
rightScore.setForeground(Color.white);
leftScore.setLocation(20, 0);
rightScore.setLocation(730, 0);
leftScore.setSize(100, 40);
rightScore.setSize(100, 40);
add(leftScore);
add(rightScore);
}
I tried solutions which I found on here and other web-sites and they didn't work out well either. There is a .TTF file in a folder called 'assets' -which I created- in Java Project Folder named Pong. It would be perfect if the right code not include try and catch blocks.
private static Font fontAwesome;
static {
try (InputStream in = YOURCLASS.class.getClassLoader().getResourceAsStream("assets/fontawesome-webfont.ttf")) {
fontAwesome = Font.createFont(Font.TRUETYPE_FONT, in);
} catch (FontFormatException | IOException e) {
e.printStackTrace();
}
}
Here's an example with fontawesome. Paste that at the top of your class and then simply use
leftScore.setFont(fontAwesome); to set the font.
Unfortunately you're going to need the try/catches. Note that the multicatch block might not work depending on your language level. If it doesn't just split them into two catch blocks.
Please try this:
try {
InputStream is = YourClass.class.getResourceAsStream("path/to/font");
Font font = Font.createFont(Font.TRUETYPE_FONT, is);
Font sizedFont = font.deriveFont(18f);
jLabel.setFont(sizedFont);
} catch (Exception ex) {
System.err.println("Not loaded");}
After you load the font you need to set a size to it!
You will have to use try/catch blocks to do this.