Solved

Displaying a drawing defined in data file.

Posted on 2003-11-22
10
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

617 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