getting it to work

i have this short play program that allows the user enter a color and then shows it on JOptionPane with a text the user typed but i want the user to be able to simply type in a valid color and then the text displays in that color but it doesn't do that so i used RGB scheme (which i don't like) can anyone help? this is the program (Sorry, i'm new to java)

import java.io.*;

import java.awt.*;

public class eg{

public static void main(String args[]) throws IOException{

BufferedReader d=new BufferedReader(new InputStreamReader(System.in));System.out.println();System.out.println("Type anything and it will be reprinted nicely in a new window!");System.out.println();String h=d.readLine();System.out.println();System.out.println("Choose a Color by entering three numerical values between 0 and 255");System.out.println();System.out.println("Color 1: ");String x =d.readLine();System.out.println("Color 2: ");String y=d.readLine();System.out.println("Color 3: ");String z=d.readLine();int x1=Integer.parseInt(x);int y1=Integer.parseInt(y);int z1=Integer.parseInt(z);Color p =new Color(x1, y1, z1);Graphics g;

Frame f = new Frame("Fun Time!");f.setSize(800,100);f.setVisible(true);g=f.getGraphics();Font B= new Font("Copperplate Gothic Light", Font.BOLD,18);g.setFont(B);g.setColor(p);g.drawString(h,10,60);}}

[1354 byte] By [deafgoata] at [2007-11-26 15:35:35]
# 1

> i have this short play program that allows the user

> enter a color and then shows it on JOptionPane with a

> text the user typed but i want the user to be able to

> simply type in a valid color and then the text

> displays in that color but it doesn't do that so i

> used RGB scheme (which i don't like) can anyone help?

> this is the program (Sorry, i'm new to java)

>

> import java.io.*;

> import java.awt.*;

> ...

Try reposting that in multiple lines AND with code tags*

* see: http://forum.java.sun.com/help.jspa?sec=formatting

prometheuzza at 2007-7-8 21:53:09 > top of Java-index,Java Essentials,New To Java...
# 2

have this short play program that allows the user enter a color and then shows it on JOptionPane with a text the user typed but i want the user to be able to simply type in a valid color and then the text displays in that color but it doesn't do that so i used RGB scheme (which i don't like) can anyone help? this is the program (Sorry, i'm new to java)

hope this is better.

import java.io.*;

import java.awt.*;

public class eg{

public static void main(String args[]) throws IOException{

BufferedReader d=new BufferedReader(new InputStreamReader(System.in));

System.out.println();

System.out.println("Type anything and it will be reprinted nicely in a new window!");

System.out.println();

String h=d.readLine();

System.out.println();

System.out.println("Choose a Color by entering three numerical values between 0 and 255");

System.out.println();

System.out.println("Color 1: ");

String x =d.readLine();

System.out.println("Color 2: ");

String y=d.readLine();

System.out.println("Color 3: ");

String z=d.readLine();

int x1=Integer.parseInt(x);

int y1=Integer.parseInt(y);

int z1=Integer.parseInt(z);

Color p =new Color(x1, y1, z1);

Graphics g;

Frame f = new Frame("Fun Time!");

f.setSize(800,100);

f.setVisible(true);

g=f.getGraphics();

Font B= new Font("Copperplate Gothic Light", Font.BOLD,18);

g.setFont(B);

g.setColor(p);

g.drawString(h,10,60);

}

}

deafgoata at 2007-7-8 21:53:09 > top of Java-index,Java Essentials,New To Java...
# 3

> have this short play program that allows the user

> enter a color and then shows it on JOptionPane with a

> text the user typed but i want the user to be able to

> simply type in a valid color and then the text

> displays in that color but it doesn't do that so i

> used RGB scheme (which i don't like) can anyone help?

> this is the program (Sorry, i'm new to java)

> hope this is better.

You could let the user just enter a color, "red" for example, and the use the static Color.RED.

Something like this:

BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Type a color: ");

String strColor = buffer.readLine();

Color myColor;

if(strColor.equalsIgnoreCase("red")) {

myColor = Color.RED;

}

else if(strColor.equalsIgnoreCase("blue")) {

myColor = Color.BLUE;

}

else {

myColor = Color.BLACK;

}

// ...

Have a look at the Color API docs to see which colors you can use:

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Color.html

Good luck.

prometheuzza at 2007-7-8 21:53:09 > top of Java-index,Java Essentials,New To Java...
# 4
it works but i'm looking for something that allows the user any color as an arguement and then, that arguement is passed to the Color. class which uses it to print
deafgoata at 2007-7-8 21:53:09 > top of Java-index,Java Essentials,New To Java...
# 5

> it works but i'm looking for something that allows

> the user any color as an arguement and then, that

> arguement is passed to the Color. class which uses it

> to print

Do you know how much colors there are?

Did you look at the API docs I posted? There are 13 static colors you can use.

You could also use this (only possible for the 13 predefined colors!):

http://www.exampledepot.com/egs/java.awt/color_Str2Clr.html

Or use a JColorChooser:

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JColorChooser.html

[url=http://www.google.com/custom?domains=exampledepot.com&q=JColorChooser+&sa=Google+Search&sitesearch=exampledepot.com&client=pub-6001183370374757&forid=1&ie=ISO-8859-1&oe=ISO-8859-1&cof=GALT%3A%23008000%3BGL%3A1%3BDIV%3A%23336699%3BVLC%3A663399%3BAH%3Acenter%3BBGC%3AFFFFFF%3BLBGC%3A336699%3BALC%3A0000FF%3BLC%3A0000FF%3BT%3A000000%3BGFNT%3A0000FF%3BGIMP%3A0000FF%3BFORID%3A1%3B&hl=en]JColorChooser examples[/url]

prometheuzza at 2007-7-8 21:53:09 > top of Java-index,Java Essentials,New To Java...
# 6

ok, so i assume the initial one i posted with the .parseInt is better with its range. thanks. i also noticed that when i try to exit the program by clicking 'X', it doesn't respond and if another window is laid on it, the message disappears :-( is there any way i can rectify this? I also wrote another program that uses .swing but when the user chooses 'cancel' it also doesn't respond... definitely not user friendly.

deafgoata at 2007-7-8 21:53:09 > top of Java-index,Java Essentials,New To Java...
# 7

> ok, so i assume the initial one i posted with the

> .parseInt is better with its range. thanks. i also

> noticed that when i try to exit the program by

> clicking 'X', it doesn't respond and if another

> window is laid on it, the message disappears :-( is

> there any way i can rectify this? I also wrote

> another program that uses .swing but when the user

> chooses 'cancel' it also doesn't respond...

> definitely not user friendly.

Have a look at the API:

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFrame.html

JFrame has a setDefaultCloseOperation(int) method which you can use. Read the details of that method to see how you can use it.

Good luck.

prometheuzza at 2007-7-8 21:53:09 > top of Java-index,Java Essentials,New To Java...