/*
 *  ==================================================================
 *  DemoRectangle.java : This applet contains draws five rectangles
 *  on a canvas. The first rectangle does not contain a fill pattern.
 *  Rectangles two through five are filled with gradient and texture
 *  patterns and simple color styles. The rectangles overlap each
 *  other -- you can adjust the color-composite slider to alter the
 *  alpha value for compositing colors.
 *
 *  Adapted from : Pantham S., Pure JFC Swing, 1999.
 *  Modified by : Mark Austin                              March, 2001
 *  ==================================================================
 */

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;

public class DemoRectangle extends JApplet {
    RectanglesCanvas canvas;
    JTextField textField;
    float alphaValue = 0.65f;

    public void init() {

        // 1. Get the content pane and assign layout

        Container container = getContentPane();

        // 2. Add the canvas with rectangles

        canvas = new RectanglesCanvas();
        container.add(canvas);

        // 3. Create the control panel with titled border

        JPanel panel = new JPanel();
        TitledBorder border = new TitledBorder("Adjust to Change Alpha Values");
        panel.setBorder(border);

        // 4. Create a label, slider and text field

        JLabel label = new JLabel("Color-Composite: ", JLabel.RIGHT);

        JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 65);
        slider.addChangeListener(new SliderListener());

        textField = new JTextField("0.65", 4);

        // 5. Add the label, slider and text field to the panel

        panel.add(label);
        panel.add(slider);
        panel.add(textField);

        // 6. Add the panel to the frame

        container.add(BorderLayout.SOUTH, panel);

    }

    // 7. Slider listener to obtain and store the slider values.
    // The slider values are converted to the alpha values, and
    // displayed in the text field. Then call repaint() on canvas. 

    class SliderListener implements ChangeListener {
        public void stateChanged(ChangeEvent e) {
            JSlider tempSlider = (JSlider) e.getSource();
            alphaValue = (float)(tempSlider.getValue()/100.0);
            textField.setText(Float.toString(alphaValue));
            canvas.repaint();
        }
    }

    // 10. Canvas to draw rectangles

    class RectanglesCanvas extends Canvas {
        Rectangle2D rec1, rec2, rec3, rec4, rec5;

        // 11. Constructor

        RectanglesCanvas() {

            // Create instances of 2D rectangles

            rec1 = new Rectangle2D.Float(25, 25, 75, 150);
            rec2 = new Rectangle2D.Float(125, 25, 100, 75);
            rec3 = new Rectangle2D.Float(75, 125, 125, 75);
            rec4 = new Rectangle2D.Float(225, 125, 125, 75);
            rec5 = new Rectangle2D.Float(150, 50, 125, 175);

            setBackground(Color.white);
        }

        public void paint(Graphics g) {

            // 12. Set up the 2D graphics context

            Graphics2D g2D = (Graphics2D) g;

            // 13. Create an instance of alpha composite and assign
            // to the graphics context.

            AlphaComposite ac = AlphaComposite.getInstance(
                            AlphaComposite.SRC_OVER, alphaValue);
            g2D.setComposite(ac);
                                                      
            // 14. Create a stroke object with the prescibed with
            // and assign it to the graphics context. 

            g2D.setStroke(new BasicStroke(5.0f));

            // 15. Draw rectangle1.

            g2D.draw(rec1);

            // 16. Create a gradient paint object and assign it
            // to the graphics context. Next, call the fill() method
            // to draw the filled rectangle2.

            GradientPaint gp = new GradientPaint(125f, 25f,
                                                 Color.yellow,
                                                 225f, 100f,
                                                 Color.blue);
            g2D.setPaint(gp);
            g2D.fill(rec2);

            // 17. Create a buffered image object, and create
            // the texture paint. Assign the texture paint to
            // the graphics context. Next, call the fill() method
            // to draw the filled rectangle.

            BufferedImage bi = new BufferedImage(5,5,
                                   BufferedImage.TYPE_INT_RGB);
            Graphics2D big = bi.createGraphics();
            big.setColor(Color.magenta);
            big.fillRect(0,0,5,5);
            big.setColor(Color.black);
            big.drawLine(0,0,5,5);
            Rectangle r = new Rectangle(0,0,5,5);

            // Create the texture using the buffered image and rectangle.

            TexturePaint tp = new TexturePaint(bi, r);

            g2D.setPaint(tp);
            g2D.fill(rec3);

            // 18. Finally, assign different colors to the graphics
            // context and draw the filled rectangles rectangle4 and
            // rectangle5.

            g2D.setColor(Color.green);
            g2D.fill(rec4);
            g2D.setColor(Color.red);
            g2D.fill(rec5);
        }
    }
}
