So I have the code below. The whole program just reads from a text file and prints certain characters if it detects specific characters from the text file (in this case I just made it mimics what the text file has).
My text file contains this:

.........www...
bbb..........ww
...bb....ww....

The program would print out exactly that.

The next step I'm trying to figure out now is how to make the program print out objects (circle, square, dot, etc.) instead of just letters? Like if it detects a dot from the text file, print a small circle.

How would I go about implementing this? I tried the paint method from Graphics2D but can't invoke/call it since that is call automatically. I'm very new to java graphics.
Any help would be greatly appreciated. Thanks.

import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
public class Read
{
    public static void main(String[] args)
    {
        try
        {
            char currentChar = '.';

            FileInputStream inputStream = new FileInputStream("test.txt");

            while (currentChar != (char)-1 )
            {
                currentChar = (char)inputStream.read(); // Cast to a char

                if (currentChar == 'b')
                {
                    System.out.print("b");
                }
                else if (currentChar == 'w')
                {
                    System.out.print("w");
                }
                else if (currentChar == '.')
                {
                    System.out.print(".");
                }
                else if (currentChar == ' ')
                {
                    System.out.print("\n");
                }

            }
        }
        catch (Exception e){//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
    }
}
 if (currentChar == 'b')
                 {
                     System.out.print("b");
                 }
                 else if (currentChar == 'w')
                 {
                     System.out.print("w");
                 }
                 else if (currentChar == '.')
                 {
                     System.out.print(".");
                 }
                 else if (currentChar == ' ')
                 {
                     System.out.print("\n");
                 }

what 's the use of that?
have you tried:

if ( currentChar == ' ')
  System.out.print("\n");
else
  System.out.print(currentChar); 

or something similar?

I think that's just a work-in-progress framework, so he can test that part of the code before moving on to doing more exciting things for each char (in which case I thing he is doing exactly the right thing).

a Switch statement might make it easier to read.

Ya, it's in-progress work and also for testing but that's what I'm aiming for. It's not just printing out what the text file contains (currentChar) but to print out something specific when it detects a specific character. In this case when the program detects 'b' it will print out "b" and so on.
But I got that part down already. Now I'm trying to figure out how to draw/print objects instead in place of letters.

switch(currentChar){
case 'b': printRectangle();
          break;
case 'c': printSquare();
          break;
case 'd': printCircle();
          break;
default: System.out.println("invalid option");
}

where are you trying to draw/print them? in a Panel? in the command line?

Yes, in a JPanel. But whichever works really. There are others like JFrame, JComponent, etc but I really don't know which suits what I'm doing best. I heard JPanel works great so I'm just using that.
Thanks for reply btw;)

@stultuske, ya, something like that but I want to have more freedom to call the paint method whenever i like and it will print out the object that I want.

@JamesCherrill, thanks, i'll take a look that at that.

I want to have more freedom to call the paint method whenever i like and it will print out the object that I want.

It doesn't work that way. The paint method may need to be called because: the window was just re-sized; the window was minimised/restored; the window had been covered by another window but now its been uncovered...
Swing knows about all those things, and calls your paint method whenever it's necessary.
If you decide that you want to change the shape in our window you call Swing's repaint() method, and that tells Swing to schedule a call to your paint method. You will need a variable that you set when the user replies to your menu so the paint method can use it to know which shape to paint.
It's all backwards from what you are thinking now, but it's how it has to work, and you'll soon get the hang of it.

So I've been playing around w/ swing JPanel and this is what I have so far:
But it's only printing out one circle. My intention is to have it print out circles in accordance w/ the text file that I have, which contains:

.........www...
bbb..........ww
...bb....ww....

So on the JPanel it should look like red circle, red circle, red circle, etc.
since "." should be drawn as a red circle, "w" should be drawn as a green circle etc.

import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

public class Temp extends JPanel
{
    private float x = 40.0f;
    private float y = 40.0f;

    public static void main(String args[])
    {
        JFrame frame = new JFrame("My");
        frame.add(new Temp());
        frame.pack();
        //frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }

    Shape circle = new Ellipse2D.Float(x, y, 10.0f, 10.0f);
    //Shape circle2 = new Ellipse2D.Float(90.0f, 40.0f, 10.0f, 10.0f);
    //Shape circle3 = new Ellipse2D.Float(140.0f, 40.0f, 10.0f, 10.0f);

    public void paint(Graphics g)
    {
        Graphics2D ga = (Graphics2D)g;
        try
        {
            FileInputStream file = new FileInputStream("test.txt");
            DataInputStream in = new DataInputStream(file);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));

            Temp rd = new Temp();

            char currentChar = ' ';

            while (currentChar != (char)-1)
            {
                currentChar = (char)file.read();

                if (currentChar == 'b')
                {
                    //System.out.print("b");  
                    ga.draw(circle);
                    ga.setPaint(Color.blue);
                    ga.fill(circle);
                    //System.out.println("For b" + x + y); //THIS IS TO KEEP TRACK OF X/Y COORDINATES
                }
                else if (currentChar == 'w')
                {
                    //System.out.print("w");
                    x = x + 50.0f;
                    ga.draw(circle);
                    ga.setPaint(Color.green);
                    ga.fill(circle);
                    //System.out.println("For w" + x + y);
                }
                else if (currentChar == '.')
                {
                    //System.out.print(".");
                    x = x + 50.0f;
                    ga.draw(circle);
                    ga.setPaint(Color.red);
                    ga.fill(circle);
                    //System.out.println("For ." + x + y);
                }
                else if (currentChar == ' ')
                {
                    //System.out.print("\n");
                    y = y + 20.0f;
                    //System.out.println("For space" + x + y);
                }
            }
            in.close();
        }

        catch(Exception e)
        {
            System.err.println("Error: " + e.getMessage());
        }

        /*ga.draw(circle);
        ga.setPaint(Color.blue);
        ga.fill(circle);
        ga.draw(circle2);
        ga.setPaint(Color.green);
        ga.fill(circle2);
        ga.draw(circle3);
        ga.setPaint(Color.red);
        ga.fill(circle3);*/
    }
}

Sorry for double post, but I think I have a better version that the above.
This one now draws multiple circles as I intended but it's not quite right yet.
First, the "b"s are not being drawn multiple times.
Second, the y coordinate is not working correctly for the second line.

In my code, I have it that whenver it detects a "space" (that's how I'm detecting end of the string atm) it will go to the next line. But it doesn't go to the next line correctly as you can see if you run the program.

b = blue circle
. = green circle
w = red circle

w's and .'s seem to be printing out correctly but b's don't.

/**
 * Write a description of class Read here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

public class Draw extends JPanel
{
    private int xx = 10;
    private int yy = 40;

    public static void main(String args[])
    {
        JFrame frame = new JFrame("My");
        frame.add(new Draw());
        frame.pack();
        //frame.setLocationRelativeTo(null);
        frame.setSize(600,600);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }

    public void paint(Graphics g)
    {
        super.paint(g);
        Graphics2D ga = (Graphics2D)g;
        try
        {
            FileInputStream file = new FileInputStream("test.txt");
            DataInputStream in = new DataInputStream(file);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));

            Draw rd = new Draw();

            char currentChar = ' ';

            while (currentChar != (char)-1)
            {
                currentChar = (char)file.read();

                if (currentChar == 'b')
                {
                    //System.out.print("b");  
                    //System.out.println("For b" + xx + yy);
                    ga.setPaint(Color.blue);
                    drawCircle(g, xx, yy, 10);
                }
                else if (currentChar == 'w')
                {
                    //System.out.print("w");
                    //System.out.println("For w" + xx + yy);

                    ga.setPaint(Color.red);
                    xx = xx + 40;
                    drawCircle(g, xx, yy, 10);
                }
                else if (currentChar == '.')
                {
                    //System.out.print(".");
                    //System.out.println("For ." + xx + yy);

                    ga.setPaint(Color.green);
                    xx = xx + 40;
                    drawCircle(g, xx, yy, 10);
                }
                else if (currentChar == ' ')
                {
                    //System.out.print("\n");
                    //System.out.println("For space" + xx + yy);

                    yy = yy + 30;
                    xx = 0;
                }
            }
            in.close();
        }

        catch(Exception e)
        {
            System.err.println("Error: " + e.getMessage());
        }

    }

    public void drawCircle(Graphics cg, int xCenter, int yCenter, int r) 
    {
        cg.drawOval(xCenter, yCenter, r, r);
    }
}

Nvm, got it:) Just messed around w/ x and y coordinates a little:P
Now my next goal is to animate it within JPanel:/

Btw, how do you fill the circles? lol

There's a graphics method that fills a circle - I'll let you find it ;)
Before you go to animation there's one thing still to do. Strip everything that's not painting out of your paint method. The reason is that that method will be called maybe 30 times per second when you're animating, so you don't want to open and read a file every time.
You use a string of characters to control what's painted. That's OK for now (although you will want to change that later!). Just have a variable to hold that string, read it from the file once when the app starts, then process it one char at a time in paint.

Ya, I got the fill method to work already:)
The way I'm thinking of animating is that based on the text file I have, I will have like a squaure. If it detects a "w" and there's nothing in the up, down, left direction have it move down. But if there's another "w" in the down direction but nothing on left direction, have it move left. Etc. etc.
I probably would have to use multidimensional array for that..

OK, that sounds like fun. But you'll still have to read the file outside the print method. Have you looked at how to control the speed/timing of the animation?

No, I haven't. This will be the first time I'm touching java animation lol
Btw, how do you make it so whenever I minimize/maximize the JPanel window, the painting does not disappear?

Hit another wall. How do you redirect the main method's output to JTextArea of JPanel?
I have like a main method that prints out a series of strings and I want that printout to display on JTextArea.

Btw, how do you make it so whenever I minimize/maximize the JPanel window, the painting does not disappear?

If you have taken the stuff out of paint like I said then min/max should work.

redirect the main method's output to JTextArea of JPanel

Not easy to redirect, but easy to replace the System.out.print(xxx); by myTextArea.append(xxx);

Ya, I tried to use append also but I just found out the real problem. When the console is in use, the GUI freezes. Has to do with threading or something. The GUI always wait for the console thread to finish before it becomes accessible again.

It's nothing to do with the console as such, it just depends which thread you are doing your console output on.
Swing uses a single thread (called the "Swing Thread", "Event Dispatch Thread" or "EDT") for everything it does, including screen updates, mouse/keyboard handling, and calling your paint and actionPeformed methods. If you have code in any methods like those that loops and runs for any appreciable time, then everything else in Swing's queue just gets held up until you have finished.
That's why earlier I asked about how you will approach controlling the animation speed/timing - doing it in a loop is going to cause these kinds of problems.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.