Importing fonts to Netbeans - java

The project I'm working on currently requires me to use the font Helvetica, in my Netbeans I don't have the option to use that font in my GUI, how can I get it?

Try This :
You can SetFont for component using font class
Font f=new Font("Helvetica",Font.BOLD,20);
setFont(JComponent);
or use :
public class FontLoader {
public static Font loadFont(float size, int style, JLabel j) {
Font font = null;
InputStream is = j.getClass().getResourceAsStream("starv___.ttf");
try {
font = Font.createFont(Font.TRUETYPE_FONT, is);
font = font.deriveFont(size);
font = font.deriveFont(style);
} /*catch (FileNotFoundException fe) {
System.out.println("File not found"); // was in here because i tried a file instead of an InputStream
} */catch (FontFormatException ex) {
System.out.println("Font is null1");
} catch (IOException e) {
System.out.println("Font is null2");
}
return font;
}
}

Related

How to test if BufferedImage using ImageIO picks up a 'null'

I am trying to set my program (java using netbeans) to do one thing if it finds the image its looking for, and another thing if it doesn't...
this is what I have got so far however it never completes the else statement. I believe this is because "image" is not technically null, as it still has corresponds to the filename entered, however I am not sure how to set java to do something based on if the filename is not found within the directory.
public void displayImage(String strfilename, JLabel JLlabel) {
try {
JLabel label = JLlabel;
String FileName = strfilename;
BufferedImage image = ImageIO.read(new File(FileName + ".jpg"));
if(image!=null){
ImageIcon icon = new ImageIcon(image);
label.setIcon(icon);}
else{
BufferedImage image2 = ImageIO.read(new File("NOIMAGE.jpg"));
ImageIcon icon2 = new ImageIcon(image2);
label.setIcon(icon2);
}
} catch (IOException ioe) {
}
}
If anyone could help me with this I would be very grateful
You're swallowing the exception. You don't need an if{}else{} loop since you already have a try{}catch{}.
String FileName = file;
try {
BufferedImage image = ImageIO.read(new File(FileName + ".jpg"));
// Code for when the image is found
} catch (IOException ex) {
// Code for when the image is not found
}
EDIT:
As #haraldK pointed out, you can have a file that exists but is unreadable, in which case a NullPointerException will be thrown.
You can handle them both in the catch clause.
public void displayImage(String strfilename, JLabel label) {
try {
BufferedImage image = ImageIO.read(new File(strfilename + ".jpg"));
ImageIcon icon = new ImageIcon(image); // Can throw NullPointerException if the file is found but is unreadable
label.setIcon(icon);
} catch (IOException | NullPointerException ex) {
ImageIcon icon = new ImageIcon("NOIMAGE.jpg");
label.setIcon(icon);
}
// You're probably going to have to pack or validate your container here
}
One thing worth noting is that this does not check exceptions for the NOIMAGE, you might want to add that.
This is better than just calling File.exists() as it also handles files that exist but are unreadable (text file and whatnot).
you can check the file if exist before you process this method also i removed some unnecessary code.
public void displayImage(String strfilename, JLabel JLlabel) {
// declare only one reference you don't need two references
BufferedImage image=null;
if(!isImageExist(strfilename)){
// assign the NOIMAGE if image not found
image = ImageIO.read(new File("NOIMAGE.jpg"));
} else {
try {
// assign the image if found
image = ImageIO.read(new File(strfilename + ".jpg"));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
ImageIcon icon = new ImageIcon(image);
//setting the image once instead of repeating the code in if and else blocks also you don't need to add another reference to JLlabel because you are already got it from a parameter
JLlabel.setIcon(icon);
}
private boolean isImageExist(String imageName) {
return new File(imageName.jpg).exist();
}

Capture a specified area of screen using java

I am implementing a screenshot capture in my Java web Application Which Is Already Working. I however have an issue with the screenshot because the Image is Taken for the entire Screen:
As you can see this is the entire screen, I just want to capture the area with the graphs(the White area). The Code I am using to capture this screen is:
public void captureGraphs() {
try {
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
Robot robot = new Robot();
BufferedImage img = robot.createScreenCapture(new Rectangle(size));
File f = new File("C:/capture");
if (!f.exists()) {
f.mkdir();
}
File[] flist = f.listFiles();
for (File flist1 : flist) {
flist1.delete();
}
ImageIO.write(img, "JPG", new File("C:/capture/screenShot.jpg"));
System.out.println("Capture Successfull");
FacesContext.getCurrentInstance().addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_INFO,
"Successful!",
"You have successfully taken a snapshot. Thank You"));
} catch (HeadlessException e) {
System.out.println(e.getMessage());
} catch (AWTException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
Is there any way I can change this code to capture the Mid Part only holding the graphs??

How to add Image as header in JEditorPane

I want to print Header with some text and a specific logo and footer with some text and page no.
How to add image on header?
public class JEditorPaneTest {
public static void main(String args[]) {
JEditorPane pane = new JEditorPane();
JScrollPane js = new JScrollPane(pane);
try {
URL url = new URL("file:C:/temp/html/12.html");
// File f=new File("C:/temp/html/12.html");
pane.setPage(url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pane.setContentType("text/html");
JFrame frmae = new JFrame();
frmae.setSize(200, 300);
try {
MessageFormat header = new MessageFormat("Order Details History");
MessageFormat footer = new MessageFormat(" Page #{0,number,integer}");
pane.print(header, footer);
} catch (PrinterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// frmae.add(pane);
frmae.add(js);
frmae.setVisible(true);
}
}
http://java-sl.com/JEditorPanePrinter.html this one is editor kit independent printing
http://java-sl.com/Pagination_In_JEditorPane_HF.html this one if you need WYSIWYG editor.
Or you can jus override paintComponent() method and after calling super draw your images over the content.

Using Custom Font

I am trying to set font of a JLabel to a custom font. No exceptions are thrown reading the file, but nothing appears when I call label.setText("string"). Text appears when I comment out the line label.setFont(f). Anyone know what I'm doing wrong? This code is inside a JPanel class.
_mineLabel = new JLabel();
_timeLabel = new JLabel();
try {
Font f = Font.createFont(Font.TRUETYPE_FONT,new File("/Users/simon/Documents/workspace/Minesweeper/bin/minesweeper/DS-DIGI.TTF"));
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(f);
f.deriveFont(12f);
_mineLabel.setFont(f);
_timeLabel.setFont(f);
} catch(IOException e) {
e.printStackTrace();
} catch(FontFormatException e) {
e.printStackTrace();
}
this.add(_mineLabel);
this.add(_timeLabel);
_timeLabel.setText("test");
Change this line
f.deriveFont(12f);
to
f=f.deriveFont(12f);

Append Images to RTF document

I am trying to add images to a rtf document. I am able to add images to the document but I can't append any images. This means that when the 2nd Image is added, the first image is removed. I think that whenever the code is executed a new rtf document is created.
public class InsertToWord {
com.lowagie.text.Document doc = null;
FileOutputStream evidenceDocument;
String fileName = "evidenceDocument.rtf";
settings obj = null;
InsertToWord() {
obj = new settings();
doc = new com.lowagie.text.Document();
}
public void insertImage(File saveLocation) {
try {
evidenceDocument = new FileOutputStream(obj.getFileLocation() + fileName);
RtfWriter2.getInstance(doc, evidenceDocument);
doc.open();
com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(saveLocation.toString());
image.scaleAbsolute(400, 300);
doc.add(image);
doc.close();
} catch (Exception e) {
}
}
}
On your insertImage() method, you are indeed creating a new file and overwriting your old one.
This line is creating the new file:
evidenceDocument = new FileOutputStream(obj.getFileLocation()+fileName);
You can pass the FileOutputStream in as a parameter to the method and then remove the line all together:
public void insertImage( FileOutputStream evidenceDocument , File saveLocation )
This code is the one I´m using to add an image into a RTF format and its working fine :
public void actionPerformed(ActionEvent arg0) {
JFileChooser fileChooser = new JFileChooser();
int option = fileChooser.showOpenDialog(null);
File file = fileChooser.getSelectedFile();
if (option == JFileChooser.APPROVE_OPTION) {
try {
BufferedImage image = ImageIO.read(file);
image = Scalr.resize(image, 200);
document = (StyledDocument) textPane.getDocument();
javax.swing.text.Style style = document.addStyle("StyleName", null);
StyleConstants.setIcon(style, new ImageIcon(image));
document.insertString(document.getLength(), "ignored text", style);
}
catch (Exception e) {
e.printStackTrace();
}
}
if (option == JFileChooser.CANCEL_OPTION) {
fileChooser.setVisible(false);
}
}// End of Method
The textPane variable is a JTextPane.

Categories