Solved

Displaying a drawing defined in data file.

Posted on 2003-11-22
10
240 Views
Last Modified: 2010-03-31
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.



0
Comment
Question by:k1ngp1n99
  • 5
  • 4
10 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 150 total points
ID: 9803502
I would write a class for each object type (with the same classname)

public interface IGUIItem
{
    public void loadFromString( String s ) ;
    public void paint( Graphics g ) ;
}

public class CIRCLE implements IGUIItem
{
    int x, y, rad ;

    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 :
                    rad = Integer.parseInt( tok ) ;
                    break ;
            }
        }
    }

    public void paint( Graphics g )
    {
        g.drawEllipse( x, y, rad, rad ) ;
    }
}

then the same for line, etc...

then:

ArrayList shapes = new ArrayList() ;

...

String line ; // a line in the file
int idx = line.indexOf( " " ) ;
if( idx > -1 )
{
   String classname = line.substring( 0, idx ).trim() ;
   line = line.substring( idx ).trim() ;
   IGUIItem item = Class.forName( classname ).newInstance() ;
   item.loadFromString( line ) ;
   shapes.add( item ) ;
}

then in your paintComponent method, just call paint on each of the items in the arraylist...

Tim
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9803504
IGUIItem item = Class.forName( classname ).newInstance() ;

should be

 IGUIItem item = (IGUIItem)Class.forName( classname ).newInstance() ;
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9803508
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
}
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 86

Expert Comment

by:CEHJ
ID: 9803518
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
{
}
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9804538
:-)  Yeah...probably :-)

I like reflection :-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9804558
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 ;
            }
        }
    }
}
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
ID: 9804613
That should be (since "CIRCLE" is the first token)

  public static class CIRCLE extends Ellipse2D.Double
  {
      public void loadFromString( String s )
      {
          StringTokenizer st = new StringTokenizer( s ) ;
          int i = 0 ;
          while( st.hasMoreTokens() )
          {
              String tok = st.nextToken() ;
              switch( i++ )
              {
                  case 1 :
                      x = Integer.parseInt( tok ) ;
                      break ;
                  case 2 :
                      y = Integer.parseInt( tok ) ;
                      break ;
                  case 3 :
                      width = Integer.parseInt( tok ) ;
                      height = width ;
                      break ;
              }
          }
      }
  }
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9804624
Ahhh...but I had

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

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

Author Comment

by:k1ngp1n99
ID: 9805664
there you go experts.
Thanks for the help.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9809881
>>Ahhh...but I had

Didn't notice ;-)
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question