Solved

Displaying a drawing defined in data file.

Posted on 2003-11-22
10
258 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
Independent Software Vendors: 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android development question 2 108
Java: anonymous class 4 54
Bot application - advice 3 80
Retrieve multiple results between delimiters in a String in Java 13 24
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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

734 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