Showing posts with label Frames. Show all posts

Color Transparency

Friday, 19 June 2015
Posted by Unknown
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class TransparencyDemo extends JPanel 
{
    public static void main(String[] args) 
    {
        JFrame window = new JFrame("TransparencyDemo");
        TransparencyDemo content = new TransparencyDemo();
        window.setContentPane(content);
        window.pack(); 
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        window.setLocation( (screenSize.width - window.getWidth())/2,
                (screenSize.height - window.getHeight())/2 );
        window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        window.setVisible(true);
    }
    private JSlider triangleTransparency;
    private JSlider ovalTransparency;
    private JSlider rectangleTransparency;
    private JSlider textTransparency;

    private Font textFont = new Font("Serif",Font.BOLD,42);

    private JPanel display = new JPanel() {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Color triangleColor = new Color(255,0,0,triangleTransparency.getValue());
            Color ovalColor = new Color(0,255,0,ovalTransparency.getValue());
            Color rectangleColor = new Color(0,0,255,rectangleTransparency.getValue());
            Color textColor = new Color(0,0,0,textTransparency.getValue());
            g.setColor(triangleColor);
            g.fillPolygon(new int[] { scaleX(0.25), scaleX(0.7), scaleX(0.1) },
                    new int[] { scaleY(0.1), scaleY(0.7), scaleY(0.7) },
                    3);
            g.setColor(ovalColor);
            g.fillOval(scaleX(0.3),scaleY(0.45),getWidth()/2,getHeight()/2);
            g.setColor(rectangleColor);
            g.fillRect(scaleX(0.4),scaleY(0.15),getWidth()/2,getHeight()/2);
            FontMetrics metrics = g.getFontMetrics(textFont);
            int lineWidth1 = metrics.stringWidth("Transparency");
            int lineWidth2 = metrics.stringWidth("Demo");
            int textHeight = metrics.getHeight() + metrics.getAscent();
            int topSkip = (getHeight() - textHeight) / 2;
            int leftSkip1 = (getWidth() - lineWidth1) / 2;
            int leftSkip2 = (getWidth() - lineWidth2) / 2;
            g.setColor(textColor);
            g.setFont(textFont);
            g.drawString("Transparency", leftSkip1, topSkip + metrics.getAscent());
            g.drawString("Demo", leftSkip2, topSkip + metrics.getAscent() + metrics.getHeight());
        }
        private int scaleX(double x) {
            return (int)(x * getWidth());
        }
        private int scaleY(double y) {
            return (int)(y * getHeight());
        }
    };
   
    private ChangeListener listener = new ChangeListener() {
        public void stateChanged(ChangeEvent arg0) {
            display.repaint();
        }
    };

    public TransparencyDemo() {
        setLayout(new BorderLayout(3,3));
        setBorder(BorderFactory.createLineBorder(Color.GRAY,2));
        setBackground(Color.GRAY);
        display.setBackground(Color.WHITE);
        display.setPreferredSize(new Dimension(400,300));
        add(display,BorderLayout.CENTER);
        JPanel bottom = new JPanel();
        bottom.setLayout(new GridLayout(4,2,3,3));
        add(bottom,BorderLayout.SOUTH);
        bottom.add( new JLabel("  Triangle Transparency:") );
        triangleTransparency = new JSlider(0,255);
        triangleTransparency.addChangeListener(listener);
        bottom.add( triangleTransparency );
        bottom.add( new JLabel("  Oval Transparency:") );
        ovalTransparency = new JSlider(0,255);
        ovalTransparency.addChangeListener(listener);
        bottom.add( ovalTransparency );
        bottom.add( new JLabel("  Rectangle Transparency:") );
        rectangleTransparency = new JSlider(0,255);
        rectangleTransparency.addChangeListener(listener);
        bottom.add( rectangleTransparency );
        bottom.add( new JLabel("  Text Transparency:") );
        textTransparency = new JSlider(0,255);
        textTransparency.addChangeListener(listener);
        bottom.add( textTransparency );
    }
}

Output:

GUI Example

Thursday, 11 June 2015
Posted by Unknown
Tag :
import javax.swing.*;    // make the Swing GUI classes available
import java.awt.*;       // used for Color and GridLayout classes
import java.awt.event.*; // used for ActionEvent and  ActionListener classes

/**
 * This simple program demonstrates several GUI components that are available in the
 * Swing GUI library.  The program shows a window containing a button, a text input
 * box, a combo box (pop-up menu), and a text area.  The text area is used for
 * a "transcript" that records interactions of the user with the other components.
 */
public class GUIDemo extends JPanel implements ActionListener {

    /**
     * This main routine allows this class to be run as an application.  The main
     * routine simply creates a window containing a panel of type GUIDemo.
     */
    public static void main(String[] args) {
        JFrame window = new JFrame("GUI Demo");
        window.setContentPane( new GUIDemo() );
        window.pack();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setResizable(false);
        window.setLocation(150,100);
        window.setVisible(true);
    }

    //-----------------------------------------------------------------------------------

    private JTextArea transcript; // a message will be posted to this text area
                                  // each time an event is generated by some
                                  // some user action
   
    private JComboBox<String> combobox; // The pop-up menu.

    /**
     * This constructor adds several GUI components to the panel and sets
     * itself up to listen for action events from some of them.
     */
    public GUIDemo() {

        setBorder(BorderFactory.createLineBorder(Color.GRAY, 3));
        setBackground(Color.WHITE);

        setLayout(new GridLayout(1, 2, 3, 3));
            // I will put the transcript area in the right half of the
            // panel. The left half will be occupied by a grid of
            // four lines. Each line contains a component and
            // a label for that component.

        transcript = new JTextArea();
        transcript.setEditable(false);
        transcript.setMargin(new Insets(4, 4, 4, 4));
        JPanel left = new JPanel();
        left.setLayout(new GridLayout(4, 2, 3, 10));
        left.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
        add(left);
        add(new JScrollPane(transcript));

        JLabel lab = new JLabel("Push Button:   ", JLabel.RIGHT);
        left.add(lab);
        JButton b = new JButton("Click Me!");
        b.addActionListener(this);
        left.add(b);

        lab = new JLabel("Checkbox:   ", JLabel.RIGHT);
        left.add(lab);
        JCheckBox c = new JCheckBox("Click me!");
        c.addActionListener(this);
        left.add(c);

        lab = new JLabel("Text Field:   ", JLabel.RIGHT);
        left.add(lab);
        JTextField t = new JTextField("Type here!");
        t.addActionListener(this);
        left.add(t);

        lab = new JLabel("Pop-up Menu:   ", JLabel.RIGHT);
        left.add(lab);
        combobox = new JComboBox<String>();
        combobox.addItem("First Option");
        combobox.addItem("Second Option");
        combobox.addItem("Third Option");
        combobox.addItem("Fourth Option");
        combobox.addActionListener(this);
        left.add(combobox);

    }

    private void post(String message) { // add a line to the transcript
        transcript.append(message + "\n\n");
    }

    /**
     * Respond to an ActionEvent from one of the GUI components in the panel.
     * In each case, a message about the event is posted to the transcript.
     * (This method is part of the ActionListener interface.)
     */
    public void actionPerformed(ActionEvent evt) {
        Object target = evt.getSource(); // which component produced this event?
        if (target instanceof JButton) {
            post("Button was clicked.");
        } else if (target instanceof JTextField) {
            post("Pressed return in TextField\nwith contents:\n    "
                    + evt.getActionCommand());
        } else if (target instanceof JCheckBox) {
            if (((JCheckBox) target).isSelected())
                post("Checkbox was turned on.");
            else
                post("Checkbox was turned off.");
        } else if (target == combobox) {
            Object item = combobox.getSelectedItem();
            post("Item \"" + item + "\" selected\nfrom pop-up menu.");
        }
    }

} // end class GUIDemo

Output:

Welcome to My Blog

Translate

Popular Post

Total Pageviews

- Copyright © Learning Java Program - Powered by Blogger -