Java how to increase font size? - java

Hello I have this code which creates a font from a ttf file in my res folder.
try {
font1 = Font.createFont(Font.TRUETYPE_FONT, new File("res/1942.ttf"));
font1.deriveFont(12f);
} catch (FontFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I thought .deriveFont(); increased the custom font size but for me it dosnt. What is wrong?
Here is where is use the font.
g.setColor(Color.blue);
font1.deriveFont(52);
g.setFont(font1);
g.drawString("hello",480, 250);

This font1.deriveFont(12f); doesn't change font1. Rather it returns a new Font of differing size. You need to something with this returned object, perhaps something like:
setFont(font1.deriveFont(12f));
or
font1 = font1.deriveFont(12f);

Use this
g.setFont(new Font("Serif", Font.PLAIN, 14));
you can use another approach also
JButton btn = new JButton();
btn.setFont(btn.getFont().deriveFont(14.0f));

Related

Jframe.setContentPane(new JLabel([image])); background image not loading after built .jar

I am having a problem with loading images, as I am trying to load a background for my launcher. It works when I run it in Eclipse, but when I export it to a jar file, it doesn't. I have paged through the already asked questions, No luck.
Here is my code:
public void initializeJFrame(){
ImageIcon bg = new ImageIcon("src/background.png");
jb.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (jta.getText().length() < 3 || jta.getText().length() > 16) {
System.exit(-1);
} else {
try {
Runtime.getRuntime().exec(cmd, null, dir);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.exit(0);
}
}
});
jf.setSize(WIDTH,HEIGHT);
jf.setTitle("Cracked Launcher");
jf.setLayout(null);
jf.setContentPane(new JLabel(bg));
jf.setResizable(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.start();
My image is in the src folder, nothing else. I cannot find what the problem is.
Thanks for your support.
ImageIcon bg = new ImageIcon("src/background.png");
This constructor takes a filename; it will not load resources from a jar. You want to pass a URL generated by a classloader, like so:
ImageIcon bg = new ImageIcon(getClass().retResource("background.png"));

Java: show GIF image doesn't work

I'm trying to show a GIF image inside a JLabel using the following code
Image waitImage = null;
JLabel l1;
try {
waitImage = ImageIO.read(IndexesPanel.class.getResource("/images/error.gif"));
l1 = new JLabel(new ImageIcon(waitImage));
l1.setBounds(20, 20, 100, 100);
waitPanel.add(l1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The image is shown, but it's not animated. I'm using the following Gif:
Any idea?
ImageIO returns BufferedImage
Use new ImageIcon(new URL("path to resource"));
Guess you can use new ImageIcon(IndexesPanel.class.getResource("/images/error.gif"));

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.

Scrollbar in JEditorPane in a Rectangle

I have a JEditorPane that is inside a rectangle in a window so that it doesnt fill the whole window. However when the HTML page gets too big, it cuts it off instead of putting in a scrollbar, I know I need a JScrollpane or something, but I dont want the scrollbar on the whole window, only inside the rectangle.
Here is a snippet of my code (rweb is already defined as a Rectangle):
JEditorPane web = new JEditorPane();
web.setEditable(false);
try {
web.setPage("http://www.google.com");
}catch (IOException e) {
web.setContentType("text/html");
web.setText("<html>Could not load webpage</html>");
}
web.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
if(Desktop.isDesktopSupported()) {
try {
Desktop.getDesktop().browse(e.getURL().toURI());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
});
rweb = new Rectangle(0, 0, width, 600);
web.setBorder(null);
web.setBounds(rweb);
window.add(web);
You never add the JEditorPane to a JScrollPane. Text components don't have scroll support by themselves.
Try using window.add(new JScrollPane(web)); instead
Take a look at How to use scroll panes for more details
ps- web.setBounds(rweb); will only work if you are using a null layout and if you are, you'd be better of using a EmptyBorder or a layout manager that provided the ability to specify insets, like GridBagLayout

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);

Categories