2
   import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;
   import java.util.*;
   class Color_Demo extends Frame
   {
   Scanner sc=new Scanner(System.in);
   public Color_Demo()
   {

     setVisible(true);
     setSize(400, 300);
     setLayout(new FlowLayout());
     setBackground(Color.black);

}
public void paint(Graphics g)
{
     g.setColor(Color.magenta);
     g.drawString("Enter name",150,100);
     String name=sc.nextLine();

Can you tell me whats wrong? I wanted to take input in my container, But I don't know how I can..

I want to use the console typewriter effect with color , and take input from the user. Is there any way?

  • 1
    Don't do that. Don't read input in the `paint()` method (or any other related UI methods) as you'll block the event dispatch thread and thus all further event processing and ui updating. You'll probably want to use a JTextField for user input instead and listen for changes or key presses on that. – Thomas Oct 16 '18 at 13:17
  • 1
    Further and more basic advice: you seem to be quite new to programming so starting with ui/swing seems like taking 10 steps at a time. Start with console/terminal applications and then _slowly_ work your way towards building uis (and understanding how they work in the process). – Thomas Oct 16 '18 at 13:20
  • Using scanner suggests that you are writing a console application. On the other hand, using classes from Swing suggests that this is, in fact, a desktop application, with a window and such. Which one are you _actually_ writing? – M. Prokhorov Oct 16 '18 at 13:20
  • Ohh okay thanks, any suggestions on what I can do? – Anirudh Lakhotia Oct 16 '18 at 13:23
  • I wanted to print in color.. i got the program without the color, but I haven't learnt how to do that, so I googled it, and understood it partially.. – Anirudh Lakhotia Oct 16 '18 at 13:26
  • @Thomas could you help me with this? It's really important – Anirudh Lakhotia Oct 16 '18 at 13:33
  • @M.Prokhorov A Console Application.. i wanted the colored text to come in using the typewriter effect, and normally accept all the input values required – Anirudh Lakhotia Oct 16 '18 at 13:35
  • How should I help you? A swing UI would probably be too complex so the only advice I can give is: make it a console application (you can't do both in the way you think it works). The application will only send text to the console, the application displaying the console (there are several) is responsible for fonts and colors and can't easily be influenced by a Java application (at least not in a generic way - on windows you could try to send the "color xx" command to the console though) . – Thomas Oct 16 '18 at 13:38
  • @Thomas There's no way I could print in color using the typewriter effect? – Anirudh Lakhotia Oct 16 '18 at 13:41
  • As I said, you can try to send commands to the console itself. Have a look here: https://stackoverflow.com/questions/1448858/how-to-color-system-out-println-output (result of a quick google search for "Java commandline color") – Thomas Oct 16 '18 at 13:43
  • @Thomas System.out.println((char)27 + "[31m" + "ERROR MESSAGE IN RED"); doesn't work on Bluej... I printed it but it wasnt colored – Anirudh Lakhotia Oct 16 '18 at 13:54
  • 1
    That most likely means console does not support this format, as Thomas said - "the application displaying the console is responsible for fonts and colors and can't easily be influenced...". – M. Prokhorov Oct 16 '18 at 14:38
  • I'm making a GUI @Thomas I got the color printing part.. Could you just tell me how i can take input in the GUI like it is in C? Is there a way except for textfields and textareas? I wanted a terminal feel – Anirudh Lakhotia Oct 20 '18 at 09:20
  • A textarea would probably be the way to go, you just need to properly format it. There might be libraries out there that already provide a customized component for that but that would be out of scope for SO (if you haven't yet please visit the [help]) – Thomas Oct 22 '18 at 07:13

0 Answers0