Java Font Derive deriveFont(Font sample, String family, String style, String sizeStr)

Here you can find the source of deriveFont(Font sample, String family, String style, String sizeStr)

Description

derive Font

License

Open Source License

Declaration

public final static Font deriveFont(Font sample, String family, String style, String sizeStr) throws Exception 

Method Source Code

//package com.java2s;
/*//from   w  w  w. j  a v  a2  s. c  o  m
Copyright (C) 2009 Vasili Gavrilov
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.awt.Font;

public class Main {
    public final static Font deriveFont(Font sample, String family, String style, String sizeStr) throws Exception {

        int size;

        if (sizeStr == null) {
            size = sample.getSize();
        } else {
            size = Integer.parseInt(sizeStr);
        }

        int fs;
        if (style == null) {
            fs = sample.getStyle();
        } else if (style.equals("bold")) {
            fs = Font.BOLD;
        } else if (style.equals("italic")) {
            fs = Font.ITALIC;
        } else if (style.equals("bold_italic")) {
            fs = (Font.ITALIC | Font.BOLD);
        } else if (style.equals("plane")) {
            fs = Font.PLAIN;
        } else {
            throw new Exception("Font style:" + style + " does not exist");
        }

        String ff = null;
        if (family == null) {
            ff = sample.getName();
        } else {
            ff = family;
        }

        return new Font(ff, fs, size);
    }
}

Related

  1. deriveFont(Font font, int style, int size)
  2. deriveFont(Font font, String name)