Colour selector in Java Assignment Help

Assignment Help: >> Some Examples of Event Driven Programming >> Colour selector in Java

Colour selector in Java:

The second example demonstrates the use of some of the control elements we discussed in the earlier part of this unit. It uses three sliders to adjust the red, green and blue components of a colour and displays the resulting colour. Four panels are used: one displays the current red colour, another the current blue colour, another the current green colour and the final canvas displays the colour made by combining the red, green and blue components.

The display for this application is shown in Figure.

1289_colour selctor in java.png

Figure: The ColourAdjust frame with the top panel showing the combination of the three colours selected using the sliders below

The topmost panel contains the mixed colour; below this are the three sliders with their respective values and colours. The first lines of the application are shown below:

public class ColourAdjustTest
{
public static void main (String [] args)
{
ColourAdjust world = new ColourAdjust( );
world.setVisible(true);
}
}

import java.awt. *;

import javax.swing. *;

import java.awt.event. *;
public class ColourAdjust extends JFrame

private JPanel bottom; 

private int greenValue;

private int redValue;

private int blueValue; 

private ColourPanel greenCanvas;

private ColourPanel redCanvas;

private ColourPanel blueCanvas;

private ColourPanel colCanvas;

private JTextField greenText;

private JTextField blueText;

private JTextField redText;

private JScrollBar redSlider;

private JScrollBar blueSlider;

private JScrollBar greenSlider;

This declares the various sliders, text fields and panels necessary for the operation. The integer variables, greenValue, redValue and blueValue, are the colour values that are used to construct the composite colour.

public ColourAdjust ( )
{
setTitle("Colour Adjust");

setSize(300, 300); bottom = new JPanel( );

colCanvas = new ColourPanel( );

redCanvas = new ColourPanel( );
blueCanvas = new ColourPanel( );

greenCanvas = new ColourPanel( );

bottom.setLayout(new GridLayout(4, 3));

redSlider = new JScrollBar(JScrollBar.HORIZONTAL, 0, 1, 0, 256);

blueSlider = new JScrollBar(JScrollBar.HORIZONTAL, 0, 1, 0, 256);

greenSlider = new JScrollBar(JScrollBar.HORIZONTAL, 0, 1, 0, 256);

redText = new JTextField("0000", 4);

blueText = new JTextField("0000", 4);

greenText = new JTextField("0000", 4);

bottom.add(redSlider);

bottom.add(redText);

bottom.add(redCanvas);

bottom.add(blueSlider);

bottom.add(blueText);

bottom.add(blueCanvas);

bottom.add(greenSlider);

bottom.add(greenText);

bottom.add(greenCanvas);

getContentPane( ).setLayout(new GridLayout(2, 1));

getContentPane( ).add(colCanvas);

getContentPane( ).add(bottom);

redSlider.addAdjustmentListener(new ColSlide( ));

greenSlider.addAdjustmentListener(new ColSlide( ));

blueSlider.addAdjustmentListener(new ColSlide( )); 

}

The code that handles a slider being modified is shown below. Again, we have used an inner class. It involves overriding the method adjustmentValueChanged of the interface AdjustmentListener. The method getAdjustable returns the slider that has changed.

public class ColSlide implements AdjustmentListener
{
public void adjustmentValueChanged (AdjustmentEvent e)
{
if ((e.getAdjustable( )) == redSlider)
{
redValue = redSlider.getValue( );

redText.setText("" + redValue);

redCanvas.changeColour(redValue, 0, 0);

redCanvas.repaint( );
}
if ((e.getAdjustable( )) == blueSlider)
{
blueValue = blueSlider.getValue( );

blueText.setText("" + blueValue);

blueCanvas.changeColour(0, 0, blueValue);

blueCanvas.repaint( );
}
if ((e.getAdjustable( )) == greenSlider)
{
greenValue = greenSlider.getValue( );

greenText.setText("" + greenValue);

greenCanvas.changeColour(0, greenValue, 0);

greenCanvas.repaint( );
}
colCanvas.changeColour(redValue, greenValue, blueValue);
colCanvas.repaint( );
} // ends adjustmentValueChanged
} // ends inner class ColSlide
} // ends ColourAdjust

The class defining a ColourPanel object is shown below:

import java.awt.Graphics;

import javax.swing. *;

import java.awt. *;

public class ColourPanel extends JPanel
{
private Color currColour;

public Color getColour ( )
{
return currColour;
}

public void changeColour (int r, int g, int b)

{
currColour = new Color (r, g, b);
}

public void paintComponent (Graphics g)
{
Dimension d = getSize ( );

g.setColor(currColour);

g.fillRect(0, 0, d.width, d.height);
}

public ColourPanel( )
{
currColour = new Color(0, 0, 0);
}
}

There are two things to note. First, it uses the constructor of Color that takes three integer values representing the red, green and blue components of a colour. Second, the paintComponent method associated with ColourPanel objects finds the size of Activity 7.6 the panel by applying the getSize method that is associated with the Component class. This method delivers a Dimension object that has two public instance variables, width and height. These are used to draw a filled rectangle on a panel using the current colour of the canvas.

 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your Colour selector in Java homework and assignments? Live Colour selector in Java experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Colour selector in Java homework help, java assignment help and Colour selector in Java projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd