need a java program for the following

Ads
 

need a java program for the following

Write a java program with three horizontal sliders. Name the sliders, the first one as red, the second one as green and the third one as blue. You design the program such that on varying the individual slider position, you set the color of the panel setBackground(new Color(positionOfSlider1, positionOfSlider2, positionOfSlider3))

View Answers

September 18, 2012 at 5:36 PM

Here is a swing example that displays three sliders on the frame and show RGB colors.

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.basic.*;

 class Slider extends JSlider{
    private String type;
    private RangeSlider ui;

    public Slider(int min, int max, String type){
        super(min, max);
        this.type = type;
    }
    public void updateUI(){
        ui = new RangeSlider(this);
        setUI(ui);
        updateLabelUIs();
    }
    public RangeSlider getUI(){
        return ui;
    }
    public String getType(){
        return type;
    }
}
class RangeSlider extends BasicSliderUI{
    public Color[] gradient = new Color[256];
    public RangeSlider(JSlider B){
        super(B);
    }
    public void updateGradient(int othercolor, int othercolor2){
        if ("Red".equals(((Slider) slider).getType())){
            for (int i = 0; i < gradient.length; i++){
                gradient[i] = new Color(i, othercolor, othercolor2);
            }
        }
        if ("Green".equals(((Slider) slider).getType())){
            for (int i = 0; i < gradient.length; i++){
                gradient[i] = new Color(othercolor, i, othercolor2);
            }
        }
        if ("Blue".equals(((Slider) slider).getType())){
            for (int i = 0; i < gradient.length; i++){
                gradient[i] = new Color(othercolor, othercolor2, i);
            }
        }
        slider.repaint();
    }
}
public class ColorSlider extends JFrame implements ChangeListener{
    Slider redSlider = new Slider(0, 255, "Red");
    Slider greenSlider = new Slider(0, 255, "Green");
    Slider blueSlider = new Slider(0, 255, "Blue");
    JPanel colourDisplay = new JPanel();

    public ColorSlider(){
        setVisible(true);
        setSize(300, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new GridLayout(4, 1));

        redSlider.addChangeListener(this);
        greenSlider.addChangeListener(this);
        blueSlider.addChangeListener(this);

        add(redSlider);
        add(greenSlider);
        add(blueSlider);
        add(colourDisplay);
    }
    public void stateChanged(ChangeEvent e){
        int red = redSlider.getValue();
        int green = greenSlider.getValue();
        int blue = blueSlider.getValue();

        redSlider.getUI().updateGradient(green, blue);
        greenSlider.getUI().updateGradient(red, blue);
        blueSlider.getUI().updateGradient(red, green);

        this.colourDisplay.setBackground(new Color(red, green, blue));
    }
    public static void main(String[] args){
        new ColorSlider();
    }
}

September 18, 2012 at 5:36 PM

Here is a swing example that displays three sliders on the frame and show RGB colors.

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.basic.*;

 class Slider extends JSlider{
    private String type;
    private RangeSlider ui;

    public Slider(int min, int max, String type){
        super(min, max);
        this.type = type;
    }
    public void updateUI(){
        ui = new RangeSlider(this);
        setUI(ui);
        updateLabelUIs();
    }
    public RangeSlider getUI(){
        return ui;
    }
    public String getType(){
        return type;
    }
}
class RangeSlider extends BasicSliderUI{
    public Color[] gradient = new Color[256];
    public RangeSlider(JSlider B){
        super(B);
    }
    public void updateGradient(int othercolor, int othercolor2){
        if ("Red".equals(((Slider) slider).getType())){
            for (int i = 0; i < gradient.length; i++){
                gradient[i] = new Color(i, othercolor, othercolor2);
            }
        }
        if ("Green".equals(((Slider) slider).getType())){
            for (int i = 0; i < gradient.length; i++){
                gradient[i] = new Color(othercolor, i, othercolor2);
            }
        }
        if ("Blue".equals(((Slider) slider).getType())){
            for (int i = 0; i < gradient.length; i++){
                gradient[i] = new Color(othercolor, othercolor2, i);
            }
        }
        slider.repaint();
    }
}
public class ColorSlider extends JFrame implements ChangeListener{
    Slider redSlider = new Slider(0, 255, "Red");
    Slider greenSlider = new Slider(0, 255, "Green");
    Slider blueSlider = new Slider(0, 255, "Blue");
    JPanel colourDisplay = new JPanel();

    public ColorSlider(){
        setVisible(true);
        setSize(300, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new GridLayout(4, 1));

        redSlider.addChangeListener(this);
        greenSlider.addChangeListener(this);
        blueSlider.addChangeListener(this);

        add(redSlider);
        add(greenSlider);
        add(blueSlider);
        add(colourDisplay);
    }
    public void stateChanged(ChangeEvent e){
        int red = redSlider.getValue();
        int green = greenSlider.getValue();
        int blue = blueSlider.getValue();

        redSlider.getUI().updateGradient(green, blue);
        greenSlider.getUI().updateGradient(red, blue);
        blueSlider.getUI().updateGradient(red, green);

        this.colourDisplay.setBackground(new Color(red, green, blue));
    }
    public static void main(String[] args){
        new ColorSlider();
    }
}

Ads









Related Tutorials/Questions & Answers:
need a java program for the following
need a java program for the following  Write a java program with three horizontal sliders. Name the sliders, the first one as red, the second one as green and the third one as blue. You design the program such that on varying
core java code for following program
core java code for following program  We are hosting the Olympic games. Write a program that will track all the details of this hosting.... The following are the guidelines while writing this program Only command line based
Advertisements
Need a java program
Need a java program  Write a code of java to print the fibonacci series.   for(int j=1;j<=5;j++) { int i,a,b,c; a=1;b=1;int n=5; System.out.print("fibonicci series is " + a +" " + b
Write a java program that prints out the following menu???
Write a java program that prints out the following menu???  Write a java program that prints out the following menu. Auser can interact with the menu using numbers (1,2,3,4,5,6). The menu will be printed again if the user enters
write a java program that implements the following classes:
write a java program that implements the following classes:  write a java program that implements the following classes: A) a) Point in the Cartesian... implements the following methods 1)getArea() method. 2)getVolume() method. B
please give me a java program for the following question
please give me a java program for the following question  Write a java program that displays multiple frames : Step 1: Design a frame with three buttons: ?Fruit?, ?Bird? and ?Animal? Step 2: On clicking ?Fruit? button
I need add my java program with a picture.
I need add my java program with a picture.  Good evng Frnds Friends i created 1 jar file in my desktop. I need add this program with 1 picture. Whenever the user double clicking on that picture The program must start instead
need to Program
need to Program   Can any one help me on below How to insert more then one records in EXCEL Sheet using JSP
I really need a tutor for Java program that has to do with multithreading and gui!
I really need a tutor for Java program that has to do with multithreading and gui!  I am looking for a Tutor to help me with a Java program, specially GUI and multithreading. If you can tutor, please email me
C program to print the following
C program to print the following  Hello, Some one help me with a "C Program" to print the following as output: 4444444 4333334..., that is why I choosed Java Beginners, but please give me a C program
java program...need help as soon as possible!!! - Java Beginners
java program...need help as soon as possible!!!  Modify the Lexer (15.... 4. Print each token with line number READLINE: program { int i int j program left: 0 right: 6 line: 1 { left: 8 right: 8: line: 1 int
write following program
write following program  Q1 What is the difference between single...-threading. Explain with examples Q2 Write a java applet that takes your name... the state transition diagram of a thread Q6 Write a java applet that displays 9
need help with program
need help with program  To write a program to read a set of words from a file and return the following 1)Each word in the file against its frequency 2) the frequency should be in the descending order of the frequencies
i need program or code for this program
i need program or code for this program  out should be in this form 1 2 3 4 5 6 3 5 7 9 11 8 12 16 20 20 28 36 48 64 112
need help with program
need help with program  To write a program where we can take two integer array as input,Find the missing number from array"B" has all the numbers from array"A" except one,and find the fastest way in doing
Need help with console program?
Need help with console program?  Write a console program that repeatedly prompts the user to enter data until they type done (any case, Upper, Lower, or Mixed). thanks in advance.   Here is an example that repeatedly
i need some help in understanding the following codes.thanks..
i need some help in understanding the following codes.thanks..  this code is to perform LocalColorHistogram.But i can't understand it public Vector<Double> getFeatureVector(int[] inImg, int height, int width, int[] maskImg
covert the following using java
covert the following using java  how to convert (for eg : 2.89) . this decimal to binary in java
need of frameworks in java?
need of frameworks in java?  What is need of frameworks in java
need of frameworks in java?
need of frameworks in java?  What is need of frameworks in java
need of frameworks in java?
need of frameworks in java?  What is need of frameworks in java
need of frameworks in java?
need of frameworks in java?  What is need of frameworks in java
Need help writing a console program
Need help writing a console program  I need help cant seems to figure it out! Write a program that consists of three classes. The first class will be the actual program. The second class will simply convert a string to lower
Java code for following
Java code for following  Create a function that returns day difference between two dates (inclusive), without using any function provided by the platform or external library. The function must work for all dates in the range of 1
program that uses while loops to perform the following steps :
program that uses while loops to perform the following steps :  Write a program that uses while loops to perform the following steps : a. Prompt... inclusive f. Output all the uppercase letters. (java coding: pls help me
Need Help in Java programming
Need Help in Java programming  Hello. Can someone please help me with writing the following program Java program that gives assignment details such as:assignment number,assignment name,due date,submission date,percentage marks
write a program in C to print following triangles on the screen
write a program in C to print following triangles on the screen  write a program in C to print following triangles on the screen
C Program to Print Following Output - Development process
C Program to Print Following Output  Hello Sir I wnat to print Followning output in C Language with for loop How I Can Print it? 5 5 4 5 4 3 5 4 3 2 5 4 3 2 1  Hi Friend, Try the following: #include
Write a program named InternetCharge_YourLastName that do the following:
Write a program named InternetCharge_YourLastName that do the following: ... per month unlimited access is provided. Write a program named InternetCharge_YourLastName that do the following: *display the menu as follow: 1. Package A 2
how to get following answer in turbo c program
how to get following answer in turbo c program  input 123456789 output 1 2 3 4 5 6 7 8 9
How to print the following output using c program
How to print the following output using c program  1) 4 3 4 2 3 4 1 2 3 4 2) A B C D E F G H I J

Ads