Solved

Displaying a drawing defined in data file.

Posted on 2003-11-22
10
230 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

914 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

15 Experts available now in Live!

Get 1:1 Help Now