Get Colour From TextField?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Get Colour From TextField?

  1. #1
    Join Date
    May 2006
    Posts
    28

    Get Colour From TextField?

    Hi all!

    Is this possible? to be able to grab the value written in a textField and put it into the g.setColor(Color.TextFieldColour);

    I've fiddled around with it a bit, but i cant get it to quite work...i would use a button or drop down box, but i already have a switch statement and im not sure if you can put one in another one...can you? as that may be easier...but it was just a question!

    Thanks!

    -Crawf

  2. #2
    Join Date
    Jan 2005
    Posts
    413
    How are you pulling the txtField value? Can you post the code you have so far?

  3. #3
    Join Date
    May 2006
    Posts
    28
    sure! I originally just had the actionPerformed get the text from the textField, but that didnt seem to work, so i put in a Button to actually tell it to fetch the text...but yeah, ive been fiddling around an i've got this far...but the new problem is, it doesnt work! do you have any ideas?

    Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class FlowerMain3 extends Applet implements ActionListener {
        double x, y;
        float alphaValue = 0.40f;
        TextField textColour;
        Button change;
        Color colourFallback = Color.BLACK; // or whatever you want instead.
        Color colourUsed = colourFallback;
        
        public void init(){
            textColour = new TextField(10);
            change = new Button("Change Colour");
            change.setActionCommand("change");
            add(textColour);
            add(change);
            textColour.addActionListener(this);
            change.addActionListener(this);
        }   
        
        public void paint(Graphics g) {
            int n = 10;
            double angle = 360 / n;
            int radius = 100;        
            for (int i = 0; i < n; i++) {
                x = Math.cos(Math.toRadians(angle * i)) * radius;
                y = Math.sin(Math.toRadians(angle * i)) * radius;
                Graphics2D alpha = (Graphics2D) g;
                g.setColor(colourUsed);
                alpha.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alphaValue));
                alpha.fillOval((int) x + radius+40, (int) y + radius+50, 2 * radius, 2 * radius);
            }
            for (int i = 0; i < n; i++) {
                x = Math.cos(Math.toRadians(angle * i)) * radius;
                y = Math.sin(Math.toRadians(angle * i)) * radius;
                g.setColor(Color.black);
                g.drawOval((int) x + radius+40, (int) y + radius+50, 2 * radius,2 * radius);
            }
        }
        
        public void actionPerformed(ActionEvent e){
            if(e.getActionCommand().equals("change")){
                Color c = Color.getColor(textColour.getText());
                if (c != null)
                    colourUsed = c;
                else
                    colourUsed = colourFallback;
            }
        }
    }
    Thanks for looking at it!

    -Crawf
    Last edited by crawf; 06-01-2006 at 05:15 PM.

  4. #4
    Join Date
    Mar 2004
    Posts
    635
    if you want the user to type in "blue" and then set the color to blue, you'll have manually map a value to that key.

Similar Threads

  1. textField With array Question
    By donce in forum Java
    Replies: 14
    Last Post: 04-19-2006, 09:54 PM
  2. Textboxes changing colour and others
    By spudmasher in forum ASP.NET
    Replies: 0
    Last Post: 09-01-2005, 12:54 AM
  3. button listener not working
    By DrunkinP in forum Java
    Replies: 0
    Last Post: 03-31-2005, 09:36 AM
  4. ANN: Glyfz, High Colour Toolbar Graphics
    By Marc Keeley in forum vb.announcements
    Replies: 0
    Last Post: 09-04-2002, 02:41 PM
  5. Changing Colour in MshFlexGrid
    By Amrita in forum VB Classic
    Replies: 4
    Last Post: 02-14-2001, 09:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center