All pastes #388235 Raw Edit

StyXblind

public text v1 · immutable
·published 2007-03-09 22:40 UTC
rendered paste body
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

/**
 *  This program is a Java application showing a single slider and 
drawing
 *  a window frame with a  blind adjustable using the slider.
 * 
 */
public class WindowBlind extends JFrame
    implements ChangeListener {

    /**
     *  This is the slider that appears in the display.
     */
    private JSlider slider;    
    
    /**
     *  This is the panel that is drawn on.
     */
    private JPanel panel;

    /**
     *  The main method is the main action for the WindowBlind program.
     *
     * @param  args  The launch arguments (though none are required).
     */
    public static void main (String[] args) {
        
        WindowBlind frame = new WindowBlind();
        frame.setSize(400,400);
        frame.createGUI();
        frame.setVisible(true);
        
    } // end of main

    /**
     *  The createGUI method is called by the main method
     *  to set up the graphical user interface.
     */
    private void createGUI() {
        
        // Set up main window characteristics
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container window = getContentPane();
        window.setLayout(new FlowLayout() );

        // Create the adjustable slider
        slider = new JSlider(JSlider.VERTICAL,0,100,0);
        window.add(slider);
        slider.addChangeListener(this);
        

        // Create the panel for drawing on
        panel = new JPanel();
        panel.setPreferredSize(new Dimension(300, 300));
        panel.setBackground(Color.white);
        window.add(panel);
        
    } // end of createGUI 

    /**
     *  The stateChanged method is called automatically
     *  when the slider is adjusted.
     *
     * @param  event  This brings information about the slider
     *                adjustment event. This information is
     *                ignored in this application as we know which 
slider
     *                must have been adjusted!
     */
    public void stateChanged(ChangeEvent e) {
        
        Graphics paper = panel.getGraphics();
        int sliderValue = slider.getValue();
        
        
         // First erase the drawing on the paper
        paper.setColor(Color.white);
        paper.fillRect(0, 0, 300, 300);
        
        
       
        
         drawWindow(paper, Color.green, 30, 80, sliderValue);
        drawWindow(paper, Color.red, 120, 80, sliderValue);
       

        
    } // end of stateChanged
   
       private void drawWindow(Graphics paper, Color c,
                                    int x, int y, int level) {
        paper.setColor(c);
        paper.fillRect(x, y, 60, level); 
        paper.setColor(Color.black);
        paper.drawRect(x, y, 60, 100);
    }
} // end of WindowBlind