I want to include a font called Fixedsys in my game and this is the code I use :
try{
Font myFont = null;
File fontFile = new File("Fixedsys.ttf");
if(fontFile.exists()){
myFont = Font.createFont(Font.TRUETYPE_FONT, fontFile).deriveFont(Font.PLAIN, 22f);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(myFont);
System.out.println("Not null");
}else{
System.out.println("FILE DOES NOT EXIST");
}
} catch (FontFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
For some reason, Java thinks the file DOES NOT exist and prints out the FILE DOES NOT EXIST line. I have searched through google and stackoverflow but none of then work when I use :
myComponent.setFont(myFont);
I get an error saying:
cannot find variable myFont
I have checked over and over and over but nothing seems wrong.
EDIT : I removed the if(file.exists()) line and i get a different error. I get :
Cannot read Fixedsys.ttf !
EDIT 2 : ug_'s comment proved right. Java was looking in the wrong folder for the file. Thanks.
The myFont variable is a local variable inside the catch block and therefore doesn't exist anywhere else.
You have to make it a class variable to use it outside the catch block.
Like so:
class SomeClass {
// declare here
private Font myFont;
public SomeClass() {
try{
// initialize here
File fontFile = new File("Fixedsys.ttf");
if(fontFile.exists()){
myFont = Font.createFont(Font.TRUETYPE_FONT, fontFile).deriveFont(Font.PLAIN, 22f);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(myFont);
System.out.println("Not null");
}else{
System.out.println("FILE DOES NOT EXIST");
}
} catch (FontFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// somewhere else:
myComponent.setFont(myFont);
}
The real answer is that the font file does not exist at that path location. Look in Windows\Fonts or wherever the file really is.
Related
I want to rename a csv file in java using following code segment, but file is not getting renamed.
public static void main(String[] args) {
File fileToBeRenamed = new File("C:/abc/a.txt");
File newFileName = new File("C:/abcd/b.txt");
try {
fileToBeRenamed.createNewFile();
newFileName.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
boolean isRenamed = fileToBeRenamed.renameTo(newFileName);
if(isRenamed)
System.out.println("File renamed successfully");
else
System.out.println("File could not be renamed");
}
Its not throwing any error. but file is not getting renamed.So please help me to do so.
let's suppose you have a file A(fileToBeRenamed) and you want to rename it to B(newFileName). Then , no need to create "newFileName" file. your code is fine , except the file creation part.
so comment out the lines:
try {
fileToBeRenamed.createNewFile();
newFileName.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
And it will work.
Note: I don't think it has anything to do with file extension(csv/text etc), when both are the same.
I think you want to rename a.txt to b.txt, So you don't need create b.txt. If you remove newFileName.createNewFile() will work
I am having a problem loading a custom font when I run my Java program on the command line. I receive the error messages:
Reason (IOException): Can't read Resources/Fonts/customFont.ttf
java.io.IOException: Can't read Resources/Fonts/customFont.ttf
However, when I run the program within Eclipse, the Font file is found.
The Font file is resident in the runnable Jar file created when I export my program from Eclipse.
Here is the code:
File aFile= new File("Resources/Fonts/customFont.ttf");
try {
System.out.println("About to access: " + aFile.toString());
font = Font.createFont(Font.TRUETYPE_FONT, aFile);
} catch (FontFormatException e) {
// TODO Auto-generated catch block
System.out.println("Reason (FontFormat): " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Reason (IOException): " + e.getMessage());
e.printStackTrace();
}
font = font.deriveFont(Font.BOLD,15);
Now, that was the latest way I've tried to get this to work from reading other people posts.
Here is the other way I've tried:
String filename="/Fonts/customFont.ttf";
Font font = null;
File aFile= new File(getURL(filename).getFile());
try {
System.out.println("About to access: " + aFile.toString());
font = Font.createFont(Font.TRUETYPE_FONT, aFile);
} catch (FontFormatException e) {
// TODO Auto-generated catch block
System.out.println("Reason (FontFormat): " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Reason (IOException): " + e.getMessage());
e.printStackTrace();
}
font = font.deriveFont(Font.BOLD,15);
private static URL getURL(String imageFilename) {
URL aURL= null;
aURL= Airplane.class.getResource(imageFilename);
System.out.println("Got URL: " + aURL.toString());
return aURL;
}
And got these error messages:
Got URL: rsrc:Fonts/customFont.ttf
About to access: Fonts/customFont.ttf
Reason (IOException): Can't read Fonts/customFont.ttf
java.io.IOException: Can't read Fonts/customFont.ttf
at java.awt.Font.createFont(Font.java:1008)
at Game.Screen.main(Screen.java:104)
Would appreciate any help with this!
Thanks!
I am attempting to print Word documents generated using docx4j that I convert to PDF and print using PDFBox. The document I'm attempting to print just contains a simple table with basic English text.
This is the method I use:
private void printDocument(){
PrinterJob job = PrinterJob.getPrinterJob();
try {
PDDocument document;
document = PDDocument.load("files\\textDocs\\OITScanSheet.pdf");
job.setPageable(new PDPageable(document, job));
job.setJobName("OIT Scan Sheet");
job.print();
} catch (NullPointerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (PrinterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
This is what I'm trying to print:
Before print
And this is what comes out when I print (Sorry for the terrible quality):
After print
The table is intact with the correct number of rows, but the text is not there. When I print the document from Adobe Reader it prints as it should.
I'm using PDFBox 2.0.0-snapshot right now. I switched from version 1.8.4 because I was running into this problem, where it was printing the table correctly but all of the text was junk.
I'm fairly certain the problem has something to do with some sort of font problem behind the scenes, but I have no idea how to go about fixing it. Any help would be greatly appreciated.
If you really need the printDialog, this is a ready to use code, assuming you have PDFBox 2.0 and font 2.0 in your classpath
public static void PDFPrintingServices(String filePath) throws IOException, PrinterException {
PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet();
attr_set.add(MediaSizeName.ISO_A4);
attr_set.add(Sides.ONE_SIDED);
PDDocument pdf = PDDocument.load(new File(filePath));
PDFPageable p = new PDFPageable(pdf);
PDFPrintable printable = new PDFPrintable(pdf, Scaling.SCALE_TO_FIT);
PrinterJob job = PrinterJob.getPrinterJob();
job.setJobName("My Printer");
job.setPageable(p);
job.setPrintable(printable);
if(job.printDialog()) {
job.print();
job.print(attr_set);
}
}
I was wondering why my file is not updating to dropbox. Currently it only creates the an empty file.
final String TEST_FILE_NAME = DateTime + ".txt";
DbxPath path = new DbxPath(DbxPath.ROOT, TEST_FILE_NAME);
try {
if (!FileSystem.exists(path)) {
newFile = FileSystem.create(path);
try {
newFile.writeString("Hello world!");
} finally {
newFile.update();
newFile.close();
}
}
} catch (DbxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Solved: I never unlinked my dbxacc and kept relinking, causing errors to show up! Silly mistake.
I am trying to use java to open an exe file. I'm not sure which program I want to open so I am using Skype as an example. When I try to do it, it gives me errors.
try {
Process p = Runtime.getRuntime().exec("C:\\Program Files (x86)\\Skype\\Phone\\Skype");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
error:
Cannot run program "C:\Program": CreateProcess error=2, The system cannot find the file specified
Try this:
String path = "/path/to/my_app.exe";
File file = new File(path);
if (! file.exists()) {
throw new IllegalArgumentException("The file " + path + " does not exist");
}
Process p = Runtime.getRuntime().exec(file.getAbsolutePath());
You have to use a string array, change to
try {
Process p = Runtime.getRuntime().exec(new String[] {"C:\\Program Files (x86)\\Notepad++\\notepad++.exe"});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
You are on windows so you have to include the extension .exe
try {
Process p = Runtime.getRuntime().exec("C:/Program Files (x86)/Skype/Phone/Skype.exe");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Maybe use File.separator instead of '\'
I tried this and it works fine, it's taken from your example. Pay attention to the double \\
public static void main(String[] args) {
try {
Process p;
p = Runtime.getRuntime().exec("C:\\Program Files\\Java\\jdk1.8.0_05\\bin\\Jconsole.exe");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}