Link to home
Start Free TrialLog in
Avatar of k1ngp1n99
k1ngp1n99

asked on

Displaying a drawing defined in data file.

Any websites that give good information and general tips would be appreciated. Basically i need to read data from a file with information
such as

CIRCLE 50 100 75      // draw a circle x=50 y=100 radius=75
LINE 25 50 100 100 // line with end points(25,50) and (100,100)

and display it onto a GUI.
methods i think i will need are BufferReader, StringTokenizer and parseInt(). The data file will be interpreted to create a shape object and placed in an ArrayList.



ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
IGUIItem item = Class.forName( classname ).newInstance() ;

should be

 IGUIItem item = (IGUIItem)Class.forName( classname ).newInstance() ;
You're on the right lines (no pun intended). Create an ArrayList of Shape. Split up the processing

String line = null;
while((line = in.readLine() != null) {
     parseShape(line);
}

...

void parseShape(String line) {
     if (line.indexOf(CIRCLE) > -1) { // CIRCLE is a constant
           parseEllipse(line);
     }
     else if (line.indexOf(LINE) > -1) { // LINE is a constant
           parseLine(line);
     }
}

void parseLine(String line) {
     Shape s = new Line2D.Double();
     // parse then call setLine
     shapes.add(s); // add to your ArrayList
}
Well - didn't expect to see anything else here ;-)

Tim's had a good idea - the reflection thing. Although I don't think the painting necessarily needs to be done by the class itself. The reflection would be better thus, IMHO:

public class CIRCLE extends Ellipse2D.Double
{
}
:-)  Yeah...probably :-)

I like reflection :-)
Extending CEHJ's idea:

public interface IGUILoader
{
    public void loadFromString( String s ) ;
}

public class CIRCLE extends Ellipse2D.Double implements IGUILoader
{
    public void loadFromString( String s )
    {
        StringTokenizer st = new StringTokenizer( s ) ;
        int i = 0 ;
        while( st.hasMoreTokens() )
        {
            String tok = st.nextToken() ;
            switch( i++ )
            {
                case 0 :
                    x = Integer.parseInt( tok ) ;
                    break ;
                case 1 :
                    y = Integer.parseInt( tok ) ;
                    break ;
                case 2 :
                    width = Integer.parseInt( tok ) ;
                    height = width ;
                    break ;
            }
        }
    }
}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ahhh...but I had

>   line = line.substring( idx ).trim() ;

in my original parse method (of the main class) ;-)
Avatar of k1ngp1n99
k1ngp1n99

ASKER

there you go experts.
Thanks for the help.
>>Ahhh...but I had

Didn't notice ;-)