?
Solved

java color chooser

Posted on 2005-03-07
23
Medium Priority
?
705 Views
Last Modified: 2013-11-23
I am not very good at this but trying to teach myself, going badly, need help, i have started the question out of the book i am using and i am getting nowhere it told me to break it up into 2 classes i am using jbuilder, but i cannot get it right, could you maybe point me in the right direction, here is my code so far

package javaproject;
/*
       A RGBChooserComponent is a component that allows the user to select
       an RGB color.  It shows three sliders that the user can manipulate
       to set the red, green, and blue, components of the color.  A color patch
       shows the selected color, and there are three labels that show the numerical
       values of all the components.  Values are in the range 0 to 255.
       The initial color is black.  The getColor() method can be used to retrieve
       the color that the user has set.  The setColor() can be used to set
       the color that is displayed in the component.
    */
import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 import java.awt.Graphics.*;

public class ColourChooser extends Frame implements ActionListener, AdjustmentListener{

   public static void main(String[] args) {  // <<<<<<< You must have a main like this
       }

    JPanel row1 = new JPanel();

    JPanel row2 = new JPanel();
    Scrollbar slider1 = new Scrollbar(Scrollbar.HORIZONTAL,0,0,1,255);
    Scrollbar slider2 = new Scrollbar(Scrollbar.HORIZONTAL,0,0,1,255);
    Scrollbar slider3 = new Scrollbar(Scrollbar.HORIZONTAL,0,0,1,255);

    JPanel row3 = new JPanel();
    JLabel red = new JLabel("      Red");
    JLabel green = new JLabel("     Green"); // Variable declarations of textfields and scrollbars.
    JLabel blue = new JLabel("     Blue");

    JPanel row4 = new JPanel();
    JTextField txt1 = new JTextField(10);
    JTextField txt2 = new JTextField(10);
    JTextField txt3 = new JTextField(10);

    // Variable declaration of DrawCanvas i.e.
    DrawCanvas c;

    public ColourChooser() {
super("Ger Power");
      setSize(600,130);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      GridLayout layout = new GridLayout(1,5);
      Container c = getContentPane();
      c.setLayout(layout);
      c.setVisible(true);


  FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER);
  row1.setLayout(layout1);
  c.setBackground(Color.white);
  c.setSize(90,90);
  row1.add(c);
  c.add(row1);

  GridLayout layout2 = new GridLayout (3,1);
      row2.setLayout(layout2);
      row2.add(slider1);
      row2.add(slider2);
      row2.add(slider3);
        c.add(row2);

  GridLayout layout3 = new GridLayout (3,1);
      row3.setLayout(layout3);
      row3.add(red);
      row3.add(green);
      row3.add(blue);
        c.add(row3);

  GridLayout layout4 = new GridLayout (3,1);
      row4.setLayout(layout4);
      row4.add(txt1);
      row4.add(txt2);
      row4.add(txt3);
      c.add(row4);

  }
      




  }
 
      // In the constructor, some of the steps you should perform are:

      //    - define and set the layout that you want to use
      //    - define the scrollbars, labels and textfields that you will use
      //    - add the scrollbars, labels and textfields to the layout
      //    - add the action and adjustment listeners
      //    - create an instance of DrawCanvas, i.e.
      //             c = new DrawCanvas();
      //    - add the instance (i.e. c) to your layout

    // end of the constructor
 
   
    public void actionPerformed(ActionEvent e){
     
      if (e.getSource().equals(this.txt1))
               {
                    int r = new Integer(this.txt1.getText()).intValue();
                    this.slider1.setValue(r);
               }
               if (e.getSource().equals(this.txt2))
               {
                    int g = new Integer(this.txt2.getText()).intValue();
                    this.slider2.setValue(g);
               }
               if (e.getSource().equals(this.txt3))
               {
                    int b = new Integer(this.txt3.getText()).intValue();
                    this.slider3.setValue(b);
               }
               this.updateColor();
          }

                // In this method, you need to retrieve the red, green and blue
      // values entered into the textfields.

      // You also need to set the value in the corresponding scrollbar to be
      // the same value as the textfield value.

      // Call the setCanvasColour method (in the DrawCanvas class) to set the
      // colour of the canvas to the new values i.e.

      c.setCanvasColour(redValue, greenValue, blueValue);

   


    public void adjustmentValueChanged(AdjustmentEvent e){

          int r = slider1.getValue();
          int g = slider2.getValue();
          int b = slider3.getValue();
          redLabel.setText(" R = " + r);
          greenLabel.setText(" G = " + g);
          blueLabel.setText(" B = " + b);
          colorPatch.setBackground(new Color(r,g,b));

          c.setCanvasColour(redValue, greenValue, blueValue);

      // In this method, you need to retrieve the red, green and blue
      // values selected by the scrollbars.

      // You also need to set the value in the corresponding textfield to be
      // the same value as the scrollbar value.

      // Call the setCanvasColour method (in the DrawCanvas class) to set the
      // colour of the canvas to the new values i.e.



   }


package javaproject;

public class DrawCanvas extends Canvas
{
     int red, green, blue;
    Color c;
    Image i; // Variable declarations

     public DrawCanvas()
     {
        r = red;
        g = green;
        b = _blue;
        c = new Color(r, g, b);
        i = null;
     }

      public int getRed()
    {
        return red;
    }

    public int getGreen()
    {
        return green;
    }

    public int getBlue()
    {
        return blue;
    }


     public void paint(Graphics g)
     {
        Graphics g2;

        if ( i == null ||
             ( !( i.getWidth(this)  == size().width  &&
                  i.getHeight(this) == size().height ) ) )
        {
            i = createImage(size().width, size().height);
        }

        g2 = i.getGraphics();
        g2.setColor(c);
        g2.fillRect(0, 0, size().width, size().height);
        g2.dispose();

        g.drawImage(i, 0, 0, null);  // set the colour of the canvas here
     }

     public void setCanvasColor(int red, int green, int blue)
     {
        if (r != red || g != green || b != blue)
        {
            r = red;
            g = green;
            b = blue;
            c = new Color(r, g, b);
            this.repaint();
        }// "repaint" the canvas here (this will call the paint method for you)
     }

}
 
0
Comment
Question by:powerger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
  • 6
23 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 280 total points
ID: 13476606
0
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 220 total points
ID: 13476650
Right...so now we know this is a University assignment at least...

We had this as well: http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21327287.html

*sigh*

;-)

Tim
0
 

Author Comment

by:powerger
ID: 13477648
thanks tim, really struggling with the subject, i actually hate it missed the first year, but its a part of the course and i have to complete the module, wont be carrying on doing java thats for sure.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:powerger
ID: 13478327
Tim,

sorry to be a nuisance, but in the txt fields if i type in a number from 0-255 the color of the palette does not change, i dont understand the fix you have with that link, is it hard to get that working
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13484084
I don't think that comment deserves any points, does it Tim?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13484288
No :-(

Not sure why you accepted my answer powerger?

Read the link zzynx sent as that shows the fully working examples...
0
 

Author Comment

by:powerger
ID: 13504182
guys, i have my program working perfect just one small thing that wont work, in the txt field when i put in a value greater than 255 i want it to default back to zero, i tried using an if - else statement but it keeps throwing up errors.  any idea's?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13504196
Could you first answer this Q of Tim:
>> why you accepted my answer powerger?
0
 

Author Comment

by:powerger
ID: 13504209
i am new to the site, i accepted the first answer that came to me, didnt realise how it worked sorry
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13504390
If you want to reopen this question, post a zero-point question in http://www.experts-exchange.com/Community_Support/

Subject: Moderator Please Reopen
Body: Please reopen this question:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21340497.html

After it has been reopened, you can reaccept.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13504396
>> After it has been reopened, you can reaccept.

or split ;-)
0
 

Author Comment

by:powerger
ID: 13507505
right guys i have sent the request to reopen the question, do you want me to share the points or give them all to zzynx.  Can ye help me with setting the value back to zero if the amount entered is greater than 255
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13507578
>> do you want me to share the points or give them all to zzynx
Tim decides.

>> Can ye help me with setting the value back to zero if the amount entered is greater than 255
Yes. What's your code at the moment?
0
 

Author Comment

by:powerger
ID: 13507813
i opened another question for that, so if you want the 50 pts Tim it is under title "reset the values in textfields in java"

package javaproject;
/*
       A RGBChooserComponent is a component that allows the user to select
       an RGB color.  It shows three sliders that the user can manipulate
       to set the red, green, and blue, components of the color.  A color patch
       shows the selected color, and there are three labels that show the numerical
       values of all the components.  Values are in the range 0 to 255.
       The initial color is black.  The getColor() method can be used to retrieve
       the color that the user has set.  The setColor() can be used to set
       the color that is displayed in the component.
    */
   import javax.swing.* ;
   import java.awt.* ;
   import java.awt.event.* ;

      public class ColorChooser
    extends JFrame
       implements ActionListener, AdjustmentListener
   {

     // Variable declarations of textfields, panals and scrollbars.
     JTextField rtxtfld, gtxtfld, btxtfld ;
     Container contentpane ;
     JLabel redlabel, greenlabel, bluelabel ;
     private JPanel colorPatch;
                  // Color patch for displaying the color.
     Scrollbar scrollbar1, scrollbar2, scrollbar3 ;
     GridLayout colorlayout ;
     int redValue, greenValue, blueValue ; //variable declarations
      int i = 0;
     // Variable declaration of DrawCanvas
     DrawCanvas c ;

     public ColorChooser()
     {
       //define and set the layout used
       contentpane = getContentPane() ;
       setTitle( "Ger Power" ) ; //title when compiled
       colorPatch = new JPanel();
       colorPatch.setBackground( Color.black ) ; //color of patch is black
       setBackground(Color.GRAY);
       setSize( 1000, 300 ) ;
       getContentPane().setLayout(new GridLayout(4,10,10,10));
           JPanel scrolls = new JPanel();  //declare 3 panals for scrollbars
                                           //labels and textfields
           JPanel labels = new JPanel();

           JPanel textfields = new JPanel();

           scrolls.setBackground(Color.gray); //set the background colors
           labels.setBackground(Color.gray);
           textfields.setBackground(Color.gray);
           getContentPane().add(scrolls); // add the panals in

           getContentPane().add(labels);

           getContentPane().add(textfields);


       setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) ;
       colorlayout = new GridLayout( 1, 10 ) ;
       contentpane.setLayout(colorlayout) ;

       // - define the scrollbars, labels and textfields used
       rtxtfld = new JTextField( "" + redValue ) ;
       rtxtfld.addActionListener( this ); //adds action listerner
       gtxtfld = new JTextField( "" + greenValue ) ;
       gtxtfld.addActionListener( this );
       btxtfld = new JTextField( "" + blueValue ) ;
       btxtfld.addActionListener( this );

       redlabel = new JLabel( "     Red            " ) ;
       greenlabel = new JLabel( "     Green          " ) ;
       bluelabel = new JLabel( "     Blue           " ) ;

    redlabel.setBackground(Color.white);   //set up the labels background & txt color
    redlabel.setForeground(Color.red);
    redlabel.setOpaque(true);
    greenlabel.setBackground(Color.white);
    greenlabel.setForeground(new Color(0,150,0));
    greenlabel.setOpaque(true);
    bluelabel.setBackground(Color.white);
    bluelabel.setForeground(Color.blue);
    bluelabel.setOpaque(true);
       //creates horizontal scrollbars with step increments of 10 and a range
       //0-256
       scrollbar1 = new Scrollbar( Scrollbar.HORIZONTAL,  0, 10, 0, 255 ) ;
       scrollbar2 = new Scrollbar( Scrollbar.HORIZONTAL,  0, 10, 0, 255 ) ;
       scrollbar3 = new Scrollbar( Scrollbar.HORIZONTAL,  0, 10, 0, 255 ) ;
       // - Setting value of DrawCanvas to null - If this wass'nt here
       // - when the program executed it threw NullPointerExceptions
       c = new DrawCanvas() ;

       //  - add the scrollbars, labels and textfields to the layout & panals

       scrolls.setLayout(new GridLayout(3,4,5,5));
       scrolls.add(scrollbar1);
       scrolls.add(scrollbar2);
       scrolls.add(scrollbar3);

       labels.setLayout(new GridLayout(3,4,5,5));
       labels.add(redlabel);
       labels.add(greenlabel);
       labels.add(bluelabel);

       textfields.setLayout(new GridLayout(3,4,5,5));
       textfields.add(rtxtfld);
       textfields.add(gtxtfld);
       textfields.add(btxtfld);

       //    - add the instance (i.e. c) to your layout
       contentpane.add( c ) ;

       //    - add the action and adjustment listeners
       scrollbar1.addAdjustmentListener( this ) ;
       scrollbar2.addAdjustmentListener( this ) ;
       scrollbar3.addAdjustmentListener( this ) ;
       pack() ;
       update() ;
       show() ;
     } // end of the constructor

     public static void main( String args[] )
     {
       new ColorChooser() ;
     }

     public void update()
     {
             // This is called when the user has changed the value on
              // one of the scrollbars.  All the scrollbars are checked,
              // the labels are set to display the correct values,
              // and the color patch is set to correspond to the new color.

       rtxtfld.setText( "" + scrollbar1.getValue() );
       gtxtfld.setText( "" + scrollbar2.getValue() );
       btxtfld.setText( "" + scrollbar3.getValue() );
       c.setCanvasColor( scrollbar1.getValue(), scrollbar2.getValue(),
                         scrollbar3.getValue() ) ;
     }

     public void actionPerformed( ActionEvent e )
     {
       try
       {
         scrollbar1.setValue( Integer.parseInt( rtxtfld.getText() ) ) ;
         scrollbar2.setValue( Integer.parseInt( gtxtfld.getText() ) ) ;
         scrollbar3.setValue( Integer.parseInt( btxtfld.getText() ) ) ;
         update() ;
       }
       catch( NumberFormatException ex )
       //this just calls the update color routine above whenever the text fields change
       {
       }
     }



     public void adjustmentValueChanged( AdjustmentEvent e )
     {
       update() ;

       c.setCanvasColor(scrollbar1.getValue(),scrollbar2.getValue(),scrollbar3.getValue());
     }
   }
.........................................................................................................................................................
package javaproject;

import javax.swing.JPanel;
import java.awt.Color;

//this is an inner class, just like the main class extends applet, this extends
//canvas. a canvas is not unlike a panel, except instead of adding controls
//and components to it, we can add graphics and colors
class DrawCanvas extends JPanel
  {
    // Variable declarations
    int greenValue, redValue, blueValue ;

    public DrawCanvas()
    {
      // set the size of the canvas
      setSize( 500, 200 ) ;
    }


    public void setCanvasColor( int red, int green, int blue )
    {
      // "repaint" the canvas(this will call the paint method)
      setBackground( new Color( red, green, blue ) ) ;
      repaint() ;
    }
  }
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13507867
>> I opened another question for that
Then why do you post the code here?
0
 

Author Comment

by:powerger
ID: 13507948
i have posted it on the 2 questions, as i was replying to you on this question.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13508191
I don't mind what you do with this question...

If zzynx helped and I didn't, give him the points, if neither of those links helped, delete it

if both our links helped, then a split seems fair ;-)

Tim
0
 

Author Comment

by:powerger
ID: 13508233
thats fine Tim, both of you were a great help so i think a split is the fairest
0
 

Author Comment

by:powerger
ID: 13508621
zzynx did you come up with anything, they still have'nt reopened the question
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13511427
Thanks modulo! :-)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13516428
Thanks powerger
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13516477
I love a happy ending :-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question