Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

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
[450 byte] By [crawf] at [2007-11-11 8:48:50]
# 1 Re: Get Colour From TextField?
How are you pulling the txtField value? Can you post the code you have so far?
JPnyc at 2007-11-11 22:34:21 >
# 2 Re: Get Colour From TextField?
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?

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
crawf at 2007-11-11 22:35:21 >
# 3 Re: Get Colour From TextField?
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.
Phaelax at 2007-11-11 22:36:25 >