Home Answers Viewqa Swing-AWT need a java program for the following

 
 


adithya
need a java program for the following
2 Answer(s)      8 months ago
Posted in : Swing AWT

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();
    }
}









Related Pages:
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
need program - Java Beginners
need program  I need a java program to get the multiple student name and mark.display the student information by sorting the marks in ascending or desending order.  Hi Friend, Try the following code: import
need a java program - Java Beginners
need a java program  Draw a bar chart for the following data using frames Year: 2000 2001 2002 2003 2004 2005 TurnOver:7 8 4 5 9 12
Need the Following MobileApplication Related Code
Need the Following MobileApplication Related Code  Hi, I need java coding for the following requirements in Collections.. Mobile Subscriber name...: and one Daemon Thread class need's to run while doing the above operations and if you
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 java program as soon as possible
need help with java program as soon as possible  So my assignment is to write a program that used while loops to perform 6 different things. 1... number.   Hi Friend, Try the following code: import java.util.
need help creating a lift program - Java Beginners
need help creating a lift program  Classes, Methods, Constructors please i need help to create an elevator program Simulating an Elevator write an Elevator class containing various methods. Then, write a program
Need to develop Java Program - Development process
Need to develop Java Program  Develop Date class in Java similar to the one available in java.util package. Use JavaDoc comments.  Hi Friend, Try the following code: public class Date { private final int
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
need help with a program - Java Beginners
Java algorithm - need help with a program  Java algorithm - need help with a program
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
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
need help with a program - Java Beginners
need help with a program   Part I An algorithm describes how... by the user. The output of the program should be the length and width (entered.... First you would need to make up the "test data". For this algorithm, the test data
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 - Java Beginners
need help  Need help in programming in Java  Simple java program that will show the output within quotes using system.out.println... the following code is this: public class print1{ public static void main(String
Need help - Java Beginners
Need help   To Write a Java program that asks the users to enter a m..., respectively. The program stores this matrix into a two dimensional m*n array . Then, write a class with the following three methods: - Method 1 accepts the m * n
Need help in java programming
Need help in java programming  Hello. Can someone please help me with writig the following programm. Assignment 20% Presentation 10% Mini Test 10% Exam 60% Java program that accepts the following details: student
Desperately need Java program ASAP - Java Beginners
Desperately need Java program ASAP  I need the source code for a java application that asks the user to enter a 10 character telephone number... should display 555-438-3663. I need this program ASAP if possible. Thank you
New to JSP..need help to make & run a JSP program.
New to JSP..need help to make & run a JSP program.  Hi, I have installed Tomcat 5.5 on my system. Plz help me to make a simple JSP program..., if not then do the following: Go to the Control Panel>>System>>
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
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 develop Java Program - Development process
Need to develop Java Program  Develop multi-threaded echo server and a corresponding GUI client in Java
Need to develop Java Program - Development process
Need to develop Java Program  Design classes for Currency, Rupee, and Dollar. Write a program that randomly generates Rupee and Dollar objects and write them into a file using object serialization. Write another program to read
Need to develop Java Program - Development process
Need to develop Java Program  [Mini-Project] Develop a programmer's editor in Java that supports syntax-highlighting, compilation support, debugging support, etc
need help on writing a program. - Java Beginners
Writing first Java Program  Hi, I need help to write my first Java Program other then Hello World! Thanks!!  Hi Thanks for choosing roseIndia to get your query solved. check given link to see the solution of your Java
Need to develop Java Program - Development process
Need to develop Java Program  Develop a simple OPAC system for library using even-driven and concurrent programming paradigms of Java. Use JDBC to connect to a back-end database
Need to develop Java Program - Development process
Need to develop Java Program  Write a multi-threaded Java program to print all numbers below 100,000 that are both prime and fibonacci number (some examples are 2, 3, 5, 13, etc.). Design a thread that generates prime numbers
java program
java program   You need to keep record of under- graduate, PhD students, faculty and staffs of your institute. Write an abstract class Person with the following attributes and method 1. Attribute Name: String 2. Attribute Address
seriously need help....
program. Use each of the following Java statements at least once in your program...seriously need help....  Write a program that will prompt the user... for 30 days. Constraints Your program will be divided into four separate sub
program
program  explanation of program on extending thread class   Hi Friend, Please go through the following link: Java Threads Thanks
Please provide coding for following problem
Please provide coding for following problem  Please provide the coding for the following problem: You will write a java program that will read data... John Green 82 Jill White 97 The program will rad the data into three separate
need sourcecode - Java Beginners
need sourcecode  Hai, I need program for packetization for videos
need
need  hello anybody can write a javascript program for the capturing of mouse click anywhere in web page and use that click event in specified co-ordinates of x and y(i mean specified x and y cordinates)which can i mention
need
need  hello anybody can write a javascript program for the capturing of mouse click anywhere in web page and use that click event in specified co-ordinates of x and y(i mean specified x and y cordinates)which can i mention
really need help on how to write this program plz some 1 help out. - Java Beginners
a Java program that displays the following prompts: Enter...really need help on how to write this program plz some 1 help out.  i am confused here on what to write can some 1 help out here i dont quite
need project
need project  hi im new on this site so dnt knw actual procedure . but i need a project on banking system in java with sql database. which should contain following attributes create new account for new customer retrieve
doubt in the following code of java - Java Beginners
doubt in the following code of java  Hi frends, actually i want to display the following calculated matrix a[][] in the table format..... but the following program code is only helping when iam giving size of matrix as 3
How to Program in Java
This tutorial tells you how to program in Java? As a beginner you must know... is also used to create enterprise web applications also. To program in Java you... to write Java program. Here are the steps of programming Java: Step 1: Download
NEED A PROG
NEED A PROG   whats the program to add,delete, display elements... the following code: import java.util.*; class CollectionExample { public...(list.get(i)); } } } For more information, visit the following
need of java coding - JavaMail
need of java coding  Design a java interface for ADT stack. Develop it and implement the interface using link list. Provide necessary exception handling for that implementations. pls mail me this lab program..its urgent
do the following
do the following  write a program to enter the string and do the following 1- count totle number of vowel 2- replace vowel 3- delete the charactor from given value 4- riverce the string 5- convert second word in upercase 6
i need your help - Java Interview Questions
i need your help  Write a java program that: i. Allows user to enter 2 numbers from the keyboard ii. Uses a method to compare the two numbers... than the other.   Hi Friend, Try the following code: import
java program - Java Beginners
. It helps us to avoid name conflicts. As you can see in the following program... ); } } To get the program of product of two matrix,please visit the following link: http...java program   Pl. let me know about the keyword 'this' with at least
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
Java Question Anyone need some HELP !!
Java Question Anyone need some HELP !!  Create a file that contains... and poi-scratchpad-3.2-FINAL-20081019.jar files into the following path: \Java... as Quote.txt. Copy the file contents and paste them into a word-processing program
Need Help - Java Beginners
Need Help  Hello Sir, Am a beginner of Java. Also i did course... projects in Java as well as J2EE... Can u help me and guide to do a project.   Hi Friend, Please visit the following links: http
I need your help - Java Beginners
the file name is ApplicationDelivery.java Your program must follow proper Java... to describe the purpose of the program.   Hi Friend, Try the following...I need your help  For this one I need to create delivery class
need help pleaseee....i weak in java
meter reading, current meter reading and charges. Write a structured Java program to develop the above system by following the steps below: a) Prompt the user...need help pleaseee....i weak in java  QUESTION 1 You are required
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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.