Pages

Kamis, 26 April 2012

Project Java Swing Makeover (Font Browser)


/*
 * FontBrowser.java
 *
 * Created on 14 Maret 2008, 20:23
 */
package sample;

import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author  usu
 */
public class FontBrowser extends javax.swing.JFrame {

    private DefaultListModel modelFont;
    private DefaultListModel modelStyle;
    private DefaultListModel modelSize;

    /** Creates new form FontBrowser */
    public FontBrowser() {
        modelFont = new DefaultListModel();
        for (String f : GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames()) {
            modelFont.addElement(f);
        }

        modelStyle = new DefaultListModel();
        for (String s : ListRender.getStyleFont()) {
            modelStyle.addElement(s);
        }

        modelSize = new DefaultListModel();
        for (int i = 1; i <= 100; i++) {
            modelSize.addElement(i);
        }

        initComponents();

        listFont.setModel(modelFont);
        listFont.setCellRenderer(new ListRender.RenderFont());

        listStyle.setModel(modelStyle);
        listStyle.setCellRenderer(new ListRender.RenderStyle());

        listSize.setModel(modelSize);
        listSize.setCellRenderer(new ListRender.RenderSize());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                        
    private void initComponents() {

        listFont = new javax.swing.JList();
        listStyle = new javax.swing.JList();
        listSize = new javax.swing.JList();
        viewPort21 = new sample.ViewPort2();
        viewPort22 = new sample.ViewPort2();
        viewPort23 = new sample.ViewPort2();
        panelEfect1 = new pelajaran3.PanelEfect();
        jScrollPane1 = new javax.swing.JScrollPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        jScrollPane3 = new javax.swing.JScrollPane();
        labelPreview = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        listFont.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listFontValueChanged(evt);
            }
        });

        listStyle.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listStyleValueChanged(evt);
            }
        });

        listSize.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listSizeValueChanged(evt);
            }
        });

        viewPort21.setView(listFont);

        viewPort22.setView(listStyle);

        viewPort23.setView(listSize);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Font Browser");

        jScrollPane1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jScrollPane1.setOpaque(false);
        jScrollPane1.setViewport(viewPort21);

        jScrollPane2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jScrollPane2.setOpaque(false);
        jScrollPane2.setViewport(viewPort22);

        jScrollPane3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jScrollPane3.setOpaque(false);
        jScrollPane3.setViewport(viewPort23);

        labelPreview.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelPreview.setText("Font Browser By USU");
        labelPreview.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Font Browser By Usu");

        javax.swing.GroupLayout panelEfect1Layout = new javax.swing.GroupLayout(panelEfect1);
        panelEfect1.setLayout(panelEfect1Layout);
        panelEfect1Layout.setHorizontalGroup(
            panelEfect1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelEfect1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelEfect1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelPreview, javax.swing.GroupLayout.DEFAULT_SIZE, 513, Short.MAX_VALUE)
                    .addGroup(panelEfect1Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel2))
                .addContainerGap())
        );
        panelEfect1Layout.setVerticalGroup(
            panelEfect1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelEfect1Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelEfect1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelPreview, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        getContentPane().add(panelEfect1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>                      

private void listFontValueChanged(javax.swing.event.ListSelectionEvent evt) {                                    
// TODO add your handling code here:
    if (listFont.getSelectedIndex() < 0) {
        return;
    }
    String family = listFont.getSelectedValue().toString();
    Font f = new Font(family, labelPreview.getFont().getStyle(), labelPreview.getFont().getSize());
    labelPreview.setFont(f);
}                                  

private void listStyleValueChanged(javax.swing.event.ListSelectionEvent evt) {                                    
// TODO add your handling code here:
    if (listStyle.getSelectedIndex() < 0) {
        return;
    }
    String style = listStyle.getSelectedValue().toString();
    if (style.equals("BOLD")) {
        labelPreview.setFont(labelPreview.getFont().deriveFont(Font.BOLD));
    } else if (style.equals("PLAIN")) {
        labelPreview.setFont(labelPreview.getFont().deriveFont(Font.PLAIN));
    } else if (style.equals("ITALIC")) {
        labelPreview.setFont(labelPreview.getFont().deriveFont(Font.ITALIC));
    } else {
        labelPreview.setFont(labelPreview.getFont().deriveFont(Font.BOLD + Font.ITALIC));
    }
}                                    

private void listSizeValueChanged(javax.swing.event.ListSelectionEvent evt) {                                    
// TODO add your handling code here:
    if (listSize.getSelectedIndex() < 0) {
        return;
    }
    float size = Float.valueOf(listSize.getSelectedValue().toString()).floatValue();
    labelPreview.setFont(labelPreview.getFont().deriveFont(size));
}                                  

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(FontBrowser.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InstantiationException ex) {
                    Logger.getLogger(FontBrowser.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(FontBrowser.class.getName()).log(Level.SEVERE, null, ex);
                } catch (UnsupportedLookAndFeelException ex) {
                    Logger.getLogger(FontBrowser.class.getName()).log(Level.SEVERE, null, ex);
                }
                new FontBrowser().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                  
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel labelPreview;
    private javax.swing.JList listFont;
    private javax.swing.JList listSize;
    private javax.swing.JList listStyle;
    private pelajaran3.PanelEfect panelEfect1;
    private sample.ViewPort2 viewPort21;
    private sample.ViewPort2 viewPort22;
    private sample.ViewPort2 viewPort23;
    // End of variables declaration                
}


0 komentar:

Posting Komentar

Twitter Delicious Facebook Digg Stumbleupon Favorites More