File #bzoqda21-4011 - JAVA - Sourcecode

Uploaded by an anonymous user - 20/12/2010 8:48 - 342 Views
Source code
  1. package colorslider;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import javax.swing.JPanel;
  5. import javax.swing.event.ChangeEvent;
  6. import javax.swing.event.ChangeListener;
  7.  
  8. /**
  9.  * ColorPanel.java
  10.  * Created by Stijn Strickx on Feb. 27 2008
  11.  * Copyright 2008 Stijn Strickx, All rights reserved
  12.  */
  13. class ColorPanel extends JPanel implements ChangeListener {
  14.     private Color color;
  15.     private Slider slide1;
  16.     private Slider slide2;
  17.     private Slider slide3;
  18.     private int value1;
  19.     private int value2;
  20.     private int value3;
  21.  
  22.  
  23.     public ColorPanel(Slider slide1, Slider slide2, Slider slide3) {
  24.         color = new Color((int)255,(int)255,(int)255);
  25.         setBackground(color);
  26.         setOpaque(true);
  27.         setPreferredSize(new Dimension(200,20));
  28.         setLocation(200,100);
  29.         this.slide1 = slide1;
  30.         this.slide2 = slide2;
  31.         this.slide3 = slide3;
  32.     }
  33.  
  34.     public void stateChanged(ChangeEvent c) {
  35.         value1 = slide1.getValue();
  36.         value2 = slide2.getValue();
  37.         value3 = slide3.getValue();
  38.         Color color2 = new Color(value1,value2,value3);
  39.         slide1.getNumberLabel().setText(valToString(value1));
  40.         slide2.getNumberLabel().setText(valToString(value2));
  41.         slide3.getNumberLabel().setText(valToString(value3));
  42.         setBackground(color2);
  43.     }
  44.     public String valToString(int value){
  45.         if((value<255)){
  46.             return ("" + value);
  47.         }
  48.         else{
  49.             return ("" + value);
  50.         }
  51.     }
  52. }
  53. ==================================================================================
  54.  
  55. package colorslider;
  56.  
  57. import javax.swing.BorderFactory;
  58. import javax.swing.JLabel;
  59. import javax.swing.JPanel;
  60. import javax.swing.JSlider;
  61. import javax.swing.border.TitledBorder;
  62.  
  63. /**
  64.  * Slider.java
  65.  * Created by Stijn Strickx on Feb. 27 2008
  66.  * Copyright 2008 Stijn Strickx, All rights reserved
  67.  */
  68. public class Slider extends JPanel {
  69.     private JSlider slider;
  70.     private JLabel number;
  71.  
  72.     public Slider(String name){
  73.         slider = new JSlider();
  74.         number = new JLabel("" + 255/50 );
  75.         slider.setMajorTickSpacing(5);
  76.         slider.setMinorTickSpacing(5);
  77.         add(slider);
  78.         add(number);
  79.         TitledBorder border = BorderFactory.createTitledBorder(name);
  80.         setBorder(border);
  81.     }
  82.  
  83.     void addChangeListener(ColorPanel colorSq) {
  84.         slider.addChangeListener(colorSq);
  85.     }
  86.     public JLabel getNumberLabel(){
  87.         return number;
  88.     }
  89.     public int getValue(){
  90.         return slider.getValue();
  91.     }
  92. }
  93. =============================================================================================
  94.  
  95. package colorslider;
  96. import java.awt.Container;
  97. import java.awt.FlowLayout;
  98. import java.awt.GridLayout;
  99. import javax.swing.JFrame;
  100. import javax.swing.JPanel;
  101.  
  102. /**
  103.  * ColorWindow.java
  104.  * Created by Stijn Strickx on Feb. 27 2008
  105.  * Copyright 2008 Stijn Strickx, All rights reserved
  106.  */
  107. public class ColorWindow {
  108.  
  109.     ColorPanel colorSq;
  110.     private JPanel slidePanel;
  111.  
  112.     public ColorWindow(){
  113.  
  114.         Slider slide1 = new Slider("Red");
  115.         Slider slide2 = new Slider("Green");
  116.         Slider slide3 = new Slider("Blue");
  117.         colorSq = new ColorPanel(slide1, slide2, slide3);
  118.  
  119.         slidePanel = new JPanel();
  120.         slideComplete(slide1);
  121.         slideComplete(slide2);
  122.         slideComplete(slide3);
  123.  
  124.         slidePanel.setLayout(new GridLayout (0,1));
  125.         JFrame window = new JFrame("Try Color Panel");
  126.         window.setSize(280, 220);
  127.         window.setResizable(false);
  128.  
  129.         Container cp = window.getContentPane();
  130.         cp.add(slidePanel);
  131.         cp.add(colorSq);
  132.         cp.setLayout(new FlowLayout());
  133.  
  134.         //window.pack();
  135.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  136.         window.setVisible(true);
  137.     }
  138.     public void slideComplete(Slider slide){
  139.         slide.addChangeListener(colorSq);
  140.         slidePanel.add(slide);
  141.     }
  142. }
  143. ============================================================================================
  144.  
  145. package colorslider;
  146.  
  147. /**
  148.  * Main.java
  149.  * Created by Stijn Strickx on Feb. 27 2008
  150.  * Copyright 2008 Stijn Strickx, All rights reserved
  151.  */
  152. public class tryMain {
  153.  
  154.     public static void main(String[] args){
  155.         new ColorWindow();
  156.     }
  157. }