skamarthi16
skamarthi16

Reputation: 1

Taking user input for foreground color and applying it to JTextArea? Why is String foregroundcolor not working?

import java.applet.Applet;
import java.awt.*;
import java.util.Scanner;
import javax.swing.*;

public class font_chooser extends JApplet {

    public static void main(String[] args) {

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Object[] possibilities = ge.getAvailableFontFamilyNames();
        Object[] colors = { "red", "yellow", "blue", "orange", "pink", "cyan", "magenta", "black", "white", "gray" };
        String font = (String) JOptionPane.showInputDialog(null, "Choose a Font", "Font Chooser",
                +JOptionPane.PLAIN_MESSAGE, null, possibilities, "");

I set the variable foregroundcolor to the user input

        String foregroundcolor = (String) JOptionPane.showInputDialog(null, "Chose Font Color", "Color",
                JOptionPane.PLAIN_MESSAGE, null, colors, "");

I am printing this pangram if the user chooses font and color correctly

        if ((font != null) && (font.length() > 0) && (colors != null)) {
            JTextArea textArea = new JTextArea("The quick brown fox jumped over the lazy dog’s back."
                    + "\n Pack my box with five dozen liquor jugs" + "\n Jackdaws love my big sphinx of quartz."
                    + "\n Mr. Jock, TV quiz PhD, bags few lynx." + "\n abcdefghijklmnopqrstuvwxyz"
                    + "\n ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "\n 01234567890"
                    + "\n €†™´¸¢©¤°÷½¼¾>¡¿«‘’<¯µ ·¬ªº¶±£\"»®§­¹²³ß×™¥" + "\n ÀÁÂÃÄÅÆÇÈÉ ÊËÌÍÎÏÐÑÒÓÔ ÕÖØÙÚÛÜÝÞÿ"
                    + "\n àáâãäåæçèé êëìíîïðñòóô õöøùúûüýþÿ" + "\n !\"#$%&'()*+,-./:;<=>?@[\\^_z{|}~"
                    + "\n uvw wW gq9 2z 5s il17|!j oO08 `'\" ;:,. m nn rn {[()]}u");
            textArea.setFont(new Font((String) font, Font.ITALIC, 16));

I apply the textArea foreground to the userinput, and this does not seem to be working correctly, thoughts?

            textArea.setForeground(Color.getColor(foregroundcolor));
            JOptionPane.showMessageDialog(null, textArea);
            // JOptionPane.showMessageDialog(null, "You chose " + font);
        }
    }

}

Upvotes: 0

Views: 375

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

You're misinterpreting the Color.getColor(...) method. It takes a String, but it's a String representation of the Color int, not a String representation of the Color String. Per the Color API:

The argument is treated as the name of a system property to be obtained. The string value of this property is then interpreted as an integer which is then converted to a Color object.

Consider using a Map<String, Color> to help you set Color.

For example (though ugly as it uses parallel arrays:

import java.awt.*;
import java.util.HashMap;
import java.util.Map;

import javax.swing.*;

public class TestColors {
    public static void main(String[] args) {

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Object[] possibilities = ge.getAvailableFontFamilyNames();
        String[] colors = { "red", "yellow", "blue", "orange", "pink", "cyan", "magenta", "black", "white", "gray" };
        Color[] realColors = { Color.RED, Color.YELLOW, Color.BLUE, Color.ORANGE, Color.PINK, Color.CYAN, Color.MAGENTA,
                Color.BLACK, Color.WHITE, Color.GRAY };
        Map<String, Color> colorMap = new HashMap<>();
        for (int i = 0; i < colors.length; i++) {
            colorMap.put(colors[i], realColors[i]);
        }
        String font = (String) JOptionPane.showInputDialog(null, "Choose a Font", "Font Chooser",
                +JOptionPane.PLAIN_MESSAGE, null, possibilities, "");

        String foregroundcolor = (String) JOptionPane.showInputDialog(null, "Chose Font Color", "Color",
                JOptionPane.PLAIN_MESSAGE, null, colors, "");
        System.out.println(foregroundcolor);

        if ((font != null) && (font.length() > 0) && (colors != null)) {
            JTextArea textArea = new JTextArea("The quick brown fox jumped over the lazy dog’s back."
                    + "\n Pack my box with five dozen liquor jugs" + "\n Jackdaws love my big sphinx of quartz."
                    + "\n Mr. Jock, TV quiz PhD, bags few lynx." + "\n abcdefghijklmnopqrstuvwxyz"
                    + "\n ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "\n 01234567890"
                    + "\n €†™´¸¢©¤°÷½¼¾>¡¿«‘’<¯µ ·¬ªº¶±£\"»®§­¹²³ß×™¥" + "\n ÀÁÂÃÄÅÆÇÈÉ ÊËÌÍÎÏÐÑÒÓÔ ÕÖØÙÚÛÜÝÞÿ"
                    + "\n àáâãäåæçèé êëìíîïðñòóô õöøùúûüýþÿ" + "\n !\"#$%&'()*+,-./:;<=>?@[\\^_z{|}~"
                    + "\n uvw wW gq9 2z 5s il17|!j oO08 `'\" ;:,. m nn rn {[()]}u");
            textArea.setFont(new Font((String) font, Font.ITALIC, 16));

            textArea.setForeground(colorMap.get(foregroundcolor));
            JOptionPane.showMessageDialog(null, new JScrollPane(textArea));
            // JOptionPane.showMessageDialog(null, "You chose " + font);
        }
    }
}

Note that

  1. your class should not extend JApplet if it is not to be used as an applet.
  2. Nothing should use applets nowadays.

A little cleaner:

import java.awt.*;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.swing.*;

public class TestColors {
    public static void main(String[] args) {

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Object[] possibilities = ge.getAvailableFontFamilyNames();
        Map<String, Color> myColorMap = new LinkedHashMap<>();
        myColorMap.put("Red", Color.RED);
        myColorMap.put("Orange", Color.ORANGE);
        myColorMap.put("Yellow", Color.YELLOW);
        myColorMap.put("Green", Color.GREEN);
        myColorMap.put("Blue", Color.BLUE);
        myColorMap.put("Magenta", Color.MAGENTA);
        myColorMap.put("Black", Color.BLACK);
        myColorMap.put("Gray", Color.GRAY);

        String[] myColors = myColorMap.keySet().toArray(new String[] {});
        String font = (String) JOptionPane.showInputDialog(null, "Choose a Font", "Font Chooser",
                +JOptionPane.PLAIN_MESSAGE, null, possibilities, "");

        String foregroundcolor = (String) JOptionPane.showInputDialog(null, "Chose Font Color", "Color",
                JOptionPane.PLAIN_MESSAGE, null, myColors, "");

        if ((font != null) && (font.length() > 0) && (foregroundcolor != null)) {
            JTextArea textArea = new JTextArea("The quick brown fox jumped over the lazy dog’s back."
                    + "\n Pack my box with five dozen liquor jugs" + "\n Jackdaws love my big sphinx of quartz."
                    + "\n Mr. Jock, TV quiz PhD, bags few lynx." + "\n abcdefghijklmnopqrstuvwxyz"
                    + "\n ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "\n 01234567890"
                    + "\n €†™´¸¢©¤°÷½¼¾>¡¿«‘’<¯µ ·¬ªº¶±£\"»®§­¹²³ß×™¥" + "\n ÀÁÂÃÄÅÆÇÈÉ ÊËÌÍÎÏÐÑÒÓÔ ÕÖØÙÚÛÜÝÞÿ"
                    + "\n àáâãäåæçèé êëìíîïðñòóô õöøùúûüýþÿ" + "\n !\"#$%&'()*+,-./:;<=>?@[\\^_z{|}~"
                    + "\n uvw wW gq9 2z 5s il17|!j oO08 `'\" ;:,. m nn rn {[()]}u");
            textArea.setFont(new Font((String) font, Font.ITALIC, 16));

            textArea.setForeground(myColorMap.get(foregroundcolor));
            JOptionPane.showMessageDialog(null, new JScrollPane(textArea));
        }
    }
}

Upvotes: 2

Related Questions