Example usage for java.awt Font Font

List of usage examples for java.awt Font Font

Introduction

In this page you can find the example usage for java.awt Font Font.

Prototype

private Font(String name, int style, float sizePts) 

Source Link

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    int SIZE = 14;
    String FONT = "Dialog";

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextPane tp = new JTextPane();
    tp.setFont(new Font(FONT, Font.PLAIN, SIZE));
    tp.setPreferredSize(new Dimension(400, 300));
    StyledDocument doc = tp.getStyledDocument();
    Style defaultStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
    Style boldStyle = doc.addStyle("bold", defaultStyle);
    StyleConstants.setBold(boldStyle, true);
    String boldText = "this is bold test";
    String plainText = "this is plain.";

    doc.insertString(doc.getLength(), boldText, boldStyle);
    doc.insertString(doc.getLength(), plainText, defaultStyle);

    JPanel panel = new JPanel();
    panel.add(tp);//www  .j  a v  a2  s  .c  o  m

    JComboBox<String> zoomCombo = new JComboBox<String>(
            new String[] { "0.75", "1.00", "1.50", "1.75", "2.00" });
    zoomCombo.addActionListener(e -> {
        String s = (String) zoomCombo.getSelectedItem();
        double scale = new Double(s).doubleValue();
        int size = (int) (SIZE * scale);
        tp.setFont(new Font(FONT, Font.PLAIN, size));
    });
    zoomCombo.setSelectedItem("1.00");
    JPanel optionsPanel = new JPanel();
    optionsPanel.add(zoomCombo);
    panel.setBackground(Color.WHITE);
    frame.add(panel, BorderLayout.CENTER);
    frame.add(optionsPanel, BorderLayout.NORTH);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    String HTMLTEXT = "<html><head><style>.foot{color:red} .head{color:black}</style></head>"
            + "<span style='font-family:consolas'>java2s.com</span><br/>"
            + "<span style='font-family:tahoma'>java2s.com</span>";
    JTextPane textPane1 = new JTextPane();

    textPane1.setContentType("text/html");
    textPane1.setFont(new Font("courier new", Font.PLAIN, 32));
    textPane1.setDocument(new HTMLDocument() {
        @Override//  w ww . j ava  2s .co m
        public Font getFont(AttributeSet attr) {
            StyleContext styles = (StyleContext) getAttributeContext();
            Font f = styles.getFont(attr);
            String ff = f.getFamily();
            System.out.println(ff);
            return textPane1.getFont();
        }
    });
    textPane1.setText(HTMLTEXT);

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(textPane1));
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:ATitledBorder.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Titled Borders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Border thisBorder = BorderFactory.createTitledBorder("Easy");

    Border thatBorder1 = new LineBorder(Color.RED);
    Border thatBorder2 = new TitledBorder(thatBorder1, "Harder");

    Font font = new Font("Serif", Font.ITALIC, 12);
    Border thatBorder = new TitledBorder(thatBorder2, "Harder", TitledBorder.LEFT, TitledBorder.ABOVE_BOTTOM,
            font, Color.RED);/*  w w w.j a va2 s  .  com*/

    JButton thisButton = new JButton("Easy");
    thisButton.setBorder(thisBorder);

    JButton thatButton = new JButton("Harder");
    thatButton.setBorder(thatBorder);

    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(1, 2));
    contentPane.add(thisButton);
    contentPane.add(thatButton);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:WriteImageType.java

static public void main(String args[]) throws Exception {
    try {//from   w  w w .  jav  a2s . c  o m
        int width = 200, height = 200;

        // TYPE_INT_ARGB specifies the image format: 8-bit RGBA packed
        // into integer pixels
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        Graphics2D ig2 = bi.createGraphics();

        Font font = new Font("TimesRoman", Font.BOLD, 20);
        ig2.setFont(font);
        String message = "www.java2s.com!";
        FontMetrics fontMetrics = ig2.getFontMetrics();
        int stringWidth = fontMetrics.stringWidth(message);
        int stringHeight = fontMetrics.getAscent();
        ig2.setPaint(Color.black);
        ig2.drawString(message, (width - stringWidth) / 2, height / 2 + stringHeight / 4);

        ImageIO.write(bi, "PNG", new File("c:\\yourImageName.PNG"));
        ImageIO.write(bi, "JPEG", new File("c:\\yourImageName.JPG"));
        ImageIO.write(bi, "gif", new File("c:\\yourImageName.GIF"));
        ImageIO.write(bi, "BMP", new File("c:\\yourImageName.BMP"));

    } catch (IOException ie) {
        ie.printStackTrace();
    }

}

From source file:ComplexRenderingSample.java

public static void main(String args[]) {

    Object elements[][] = {//from  w w  w  .ja va  2 s . c om
            { new Font("Helvetica", Font.PLAIN, 20), Color.red, new DiamondIcon(Color.blue), "Help" },
            { new Font("TimesRoman", Font.BOLD, 14), Color.blue, new DiamondIcon(Color.green), "Me" },
            { new Font("Courier", Font.ITALIC, 18), Color.green, new DiamondIcon(Color.black), "I'm" },
            { new Font("Helvetica", Font.BOLD | Font.ITALIC, 12), Color.gray, new DiamondIcon(Color.magenta),
                    "Trapped" },
            { new Font("TimesRoman", Font.PLAIN, 32), Color.pink, new DiamondIcon(Color.yellow), "Inside" },
            { new Font("Courier", Font.BOLD, 16), Color.yellow, new DiamondIcon(Color.red), "This" },
            { new Font("Helvetica", Font.ITALIC, 8), Color.darkGray, new DiamondIcon(Color.pink),
                    "Computer" } };

    JFrame frame = new JFrame("Complex Renderer");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    JList jlist = new JList(elements);
    ListCellRenderer renderer = new ComplexCellRenderer();
    jlist.setCellRenderer(renderer);
    JScrollPane scrollPane = new JScrollPane(jlist);
    contentPane.add(scrollPane, BorderLayout.CENTER);

    JComboBox comboBox = new JComboBox(elements);
    comboBox.setRenderer(renderer);
    contentPane.add(comboBox, BorderLayout.NORTH);

    frame.setSize(300, 300);
    frame.setVisible(true);
}

From source file:ATitledBorder.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Titled Borders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Border thisBorder = BorderFactory.createTitledBorder("Easy");
    Icon thatIcon = new ImageIcon("diamond.gif");
    Border thatBorder1 = new MatteBorder(18, 20, 18, 20, thatIcon);
    Border thatBorder2 = new TitledBorder(thatBorder1, "Harder");
    Font font = new Font("Serif", Font.ITALIC, 12);
    Border thatBorder = new TitledBorder(thatBorder2, "Harder", TitledBorder.LEFT, TitledBorder.ABOVE_BOTTOM,
            font, Color.red);//from  ww w  . java  2s  . c  o m
    JButton thisButton = new JButton("Easy");
    thisButton.setBorder(thisBorder);
    JButton thatButton = new JButton("Harder");
    thatButton.setBorder(thatBorder);
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(1, 2));
    contentPane.add(thisButton);
    contentPane.add(thatButton);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String[] a) {
    JFrame frame = new JFrame("Titled Borders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Border thisBorder = BorderFactory.createTitledBorder("Easy");
    Icon thatIcon = MetalIconFactory.getFileChooserHomeFolderIcon();

    Border thatBorder1 = new MatteBorder(18, 20, 18, 20, thatIcon);
    Border thatBorder2 = new TitledBorder(thatBorder1, "Harder");

    Font font = new Font("Serif", Font.ITALIC, 12);
    Border thatBorder = new TitledBorder(thatBorder2, "Hardest", TitledBorder.LEFT, TitledBorder.ABOVE_BOTTOM,
            font, Color.RED);/*  w w  w  . ja va  2s  . c  o m*/

    JButton thisButton = new JButton("Easy");
    thisButton.setBorder(thisBorder);
    JButton thatButton = new JButton("Harder");
    thatButton.setBorder(thatBorder);
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(1, 2));
    contentPane.add(thisButton);
    contentPane.add(thatButton);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    PrinterJob pj = PrinterJob.getPrinterJob();
    Book book = new Book();
    PageFormat defaultFormat = new PageFormat();
    defaultFormat = pj.defaultPage(defaultFormat);
    PageFormat landscapeFormat = new PageFormat();
    landscapeFormat.setOrientation(PageFormat.LANDSCAPE);
    PagePrinter[] page = new PagePrinter[2];
    int pageWidth = (int) defaultFormat.getImageableWidth();
    int pageHeight = (int) defaultFormat.getImageableHeight();
    Font font = new Font("Helvetica", Font.BOLD, 18);
    page[0] = new PagePrinter();
    page[0].addPrintElement(new MyItem("AAA", font, 100, pageHeight / 2));
    page[0].addPrintElement(new MyItem("line", 0, pageHeight, pageWidth, pageHeight));

    page[1] = new PagePrinter();
    page[1].addPrintElement(new MyItem("rectangle", 100, 100, pageWidth - 200, pageHeight - 200));
    page[1].addPrintElement(new MyItem("oval", 120, 120, pageWidth - 240, pageHeight - 240));

    book.append(page[0], defaultFormat);
    book.append(page[1], landscapeFormat);

    pj.setPageable(book);//from  www .  ja v a2  s .  c  om
    pj.print();
}

From source file:ButtonCornerSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Cornering Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Icon acrossLogo = new ImageIcon("logo-across.jpg");
    Icon downLogo = new ImageIcon("logo-down.jpg");
    Icon bookCover = new ImageIcon("puzzlebook.jpg");
    JLabel columnLabel = new JLabel(acrossLogo);
    JLabel rowLabel = new JLabel(downLogo);
    JLabel coverLabel = new JLabel(bookCover);
    JButton button = new JButton("+");
    button.setFont(new Font("Monospaced", Font.PLAIN, 8));
    JScrollPane scrollPane = new JScrollPane(coverLabel);
    scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, button);
    scrollPane.setColumnHeaderView(columnLabel);
    scrollPane.setRowHeaderView(rowLabel);

    ActionListener actionListener = new JScrollPaneToTopAction(scrollPane);
    button.addActionListener(actionListener);

    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 200);//from w w  w.j  a  va 2 s  .  c o  m
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.java2s.com/style/download.png");

    final BufferedImage originalImage = ImageIO.read(url);
    int width = originalImage.getWidth();
    int height = originalImage.getHeight();
    final BufferedImage textImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = textImage.createGraphics();

    FontRenderContext frc = g.getFontRenderContext();
    Font font = new Font("Arial", Font.BOLD, 50);
    GlyphVector gv = font.createGlyphVector(frc, "java2s.com");

    int xOff = 0;
    int yOff = 50;

    Shape shape = gv.getOutline(xOff, yOff);
    g.setClip(shape);/*from www  .j  av a  2  s . c  om*/
    g.drawImage(originalImage, 0, 0, null);

    g.setStroke(new BasicStroke(2f));
    g.setColor(Color.BLACK);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.draw(shape);
    g.dispose();

    ImageIO.write(textImage, "png", new File("cat-text.png"));

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(textImage)));
        }
    });
}