Solved

Displaying a drawing defined in data file.

Posted on 2003-11-22
10
220 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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)
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now