• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

Strings into a splash page

In the main in this class, random text is generated to scroll up the image. I want this for scrolling the credits for the program. What would it be best way to get the strings into the class by using a properties file, string list, ?, to accomplish this.


import java.awt.event.*;
import javax.imageio.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;

public class Splash extends JPanel implements Runnable, ActionListener
{
    protected JWindow window = null;
    protected String[] text = null;
    protected Image img = null;
    protected Dimension size = null;
    protected JButton but = null;
    protected boolean go = false;
    protected int offset = 0;
    protected int topInset = 20;
    protected int leftInset = 20;
    protected int bottomInset = 20;
    protected int rightInset = 20;
   
    /**
     * create a new Splash instance
     *
     * @param text the text lines to Scroll
     * @param imgurl the url or the filename of the image
     * @param closeButton whether or not to display a close button
     */
    public Splash( String[] text, String imgurl, boolean closeButton )
    {
        super( null, true );
        this.text = text;
        try {
            window = new JWindow();
            window.setContentPane( this );
            window.setFocusable( true );
            if( imgurl.indexOf( ':' ) < 0 )
                imgurl = "file:" + imgurl;
            img = ImageIO.read( new URL( imgurl ) );
            size = new Dimension( img.getWidth( null ), img.getHeight( null ) );
            if( closeButton )
            {
                but = new JButton( "Close" );
                Dimension bsize = but.getMinimumSize();
                but.setBounds( size.width - bsize.width - 10,
                     size.height - bsize.height - 10, bsize.width, bsize.height );
                add( but );
                but.addActionListener( this );
            }
        }
        catch( Exception ex ) {
            ex.printStackTrace();
        }
    }

    /**
     * set the insets of the scrolling area
     *
     * @param top the top inset >= 0
     * @param left the left inset >= 0
     * @param bottom the bottom inset >= 0
     * @param right the right inset >= 0
     */
    public void setInsets( int top, int left, int bottom, int right )
    {
        topInset = top;
        leftInset = left;
        bottomInset = bottom;
        rightInset = right;
    }

    /**
     * open the window and start the animation
     */
    public void show()
    {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        window.setBounds( ( screen.width - size.width ) / 2,
                   ( screen.height - size.height ) / 2,
                   size.width, size.height );
        window.show();
        go = true;
        offset = 0;
        new Thread( this ).start();
    }

    /**
     * stop the animation, and close the window
     */
    public void hide()
    {
        go = false;
        window.hide();
    }

    public void actionPerformed(ActionEvent e)
    {
        hide();
    }

    public void run()
    {
        while( go )
        {
            try {
                synchronized( this ) {
                    wait( 50 );
                    offset++;
                }
                window.toFront();
                repaint();
            } catch( Exception ex ) {
                ex.printStackTrace();
            }
        }
    }

    public void paintComponent( Graphics g )
    {
        g.drawImage( img, 0, 0, null );
        String[] text = this.text;
        Rectangle bounds = getBounds();
        bounds.setBounds( leftInset, topInset, bounds.width - leftInset - rightInset,
                                bounds.height - topInset - bottomInset );
        g.setClip( bounds );
        int offset = this.offset;
        int lineHeight = g.getFontMetrics().getHeight();
        int viewHeight = bounds.height + text.length * lineHeight;
        for( int idx = 0; idx < text.length; idx++ )
        {
            int offs = bounds.y + bounds.height + lineHeight * idx - ( offset % viewHeight );
            g.drawString( text[ idx ], bounds.x, offs );
        }
        g.setClip( getBounds() );
    }

    public static void main( String[] args )
    {
        try {
            // generate some random text
            String[] text = new String[ 20 ];
            for( int idx = 0; idx < text.length; idx++ )
                for( text[ idx ] = ""; text[ idx ].length() < 20; )
                    text[ idx ] += "Anh Dgdb -".charAt( (int)(  Math.random() * 10 ) );

            // the background image
            String imgurl = "http://www.blueprint21.de/flashdata/thai-rice.jpg";

            // initialize with the random text and some image url
            Splash splash = new Splash( text, imgurl, true );

            // set the font for the text
            splash.setFont( new Font( "sans-serif", Font.ITALIC, 12 ) );

            // set the text color
            splash.setForeground( Color.black );

            // set the insets of area where the scolling test is displayed
            // top (157), left (50), bottom (5), right (10)
            splash.setInsets( 157, 50, 5, 10 );
           

            // open the splash window
            splash.show();

            // do the initialization or whatever
            for( int idx = 0; idx < 30 && splash.isShowing(); idx++ )
                Thread.sleep( 5000 );

            // close the splash window when the application is ready
            splash.hide();            
        } catch( Exception ex ) {
            ex.printStackTrace();
        }
        System.exit( 0 );
    }
}
0
Drop_of_Rain
Asked:
Drop_of_Rain
  • 3
  • 3
  • 3
  • +2
5 Solutions
 
girionisCommented:
I'd use a ResourceBundle and separate properties with values, e.g.

programmer=a programmer
designer=blah blah

and so on:

http://javaalmanac.com/egs/java.text/LocMsg.html
http://java.sun.com/docs/books/tutorial/i18n/resbundle/propfile.html
0
 
JugglerWCommented:
Would read lines from a normnal text file. Try this. To start you must create a text file named lines.txt  with the lines to show in your working directory:


import java.awt.event.*;
import javax.imageio.*;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.awt.*;
import javax.swing.*;

public class Splash extends JPanel implements Runnable, ActionListener
{
    protected JWindow window = null;
    protected String[] text = null;
    protected Image img = null;
    protected Dimension size = null;
    protected JButton but = null;
    protected boolean go = false;
    protected int offset = 0;
    protected int topInset = 20;
    protected int leftInset = 20;
    protected int bottomInset = 20;
    protected int rightInset = 20;
   
    /**
     * create a new Splash instance
     *
     * @param text the text lines to Scroll
     * @param imgurl the url or the filename of the image
     * @param closeButton whether or not to display a close button
     */
    public Splash( String[] text, String imgurl, boolean closeButton )
    {
        super( null, true );
        this.text = text;
        try {
            window = new JWindow();
            window.setContentPane( this );
            window.setFocusable( true );
            if( imgurl.indexOf( ':' ) < 0 )
                imgurl = "file:" + imgurl;
            img = ImageIO.read( new URL( imgurl ) );
            size = new Dimension( img.getWidth( null ), img.getHeight( null ) );
            if( closeButton )
            {
                but = new JButton( "Close" );
                Dimension bsize = but.getMinimumSize();
                but.setBounds( size.width - bsize.width - 10,
                     size.height - bsize.height - 10, bsize.width, bsize.height );
                add( but );
                but.addActionListener( this );
            }
        }
        catch( Exception ex ) {
            ex.printStackTrace();
        }
    }

    /**
     * set the insets of the scrolling area
     *
     * @param top the top inset >= 0
     * @param left the left inset >= 0
     * @param bottom the bottom inset >= 0
     * @param right the right inset >= 0
     */
    public void setInsets( int top, int left, int bottom, int right )
    {
        topInset = top;
        leftInset = left;
        bottomInset = bottom;
        rightInset = right;
    }

    /**
     * open the window and start the animation
     */
    public void show()
    {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        window.setBounds( ( screen.width - size.width ) / 2,
                   ( screen.height - size.height ) / 2,
                   size.width, size.height );
        window.show();
        go = true;
        offset = 0;
        new Thread( this ).start();
    }

    /**
     * stop the animation, and close the window
     */
    public void hide()
    {
        go = false;
        window.hide();
    }

    public void actionPerformed(ActionEvent e)
    {
        hide();
    }

    public void run()
    {
        while( go )
        {
            try {
                synchronized( this ) {
                    wait( 50 );
                    offset++;
                }
                window.toFront();
                repaint();
            } catch( Exception ex ) {
                ex.printStackTrace();
            }
        }
    }

    public void paintComponent( Graphics g )
    {
        g.drawImage( img, 0, 0, null );
        String[] text = this.text;
        Rectangle bounds = getBounds();
        bounds.setBounds( leftInset, topInset, bounds.width - leftInset - rightInset,
                                bounds.height - topInset - bottomInset );
        g.setClip( bounds );
        int offset = this.offset;
        int lineHeight = g.getFontMetrics().getHeight();
        int viewHeight = bounds.height + text.length * lineHeight;
        for( int idx = 0; idx < text.length; idx++ )
        {
            int offs = bounds.y + bounds.height + lineHeight * idx - ( offset % viewHeight );
            g.drawString( text[ idx ], bounds.x, offs );
        }
        g.setClip( getBounds() );
    }

    // Read the lines of a file in a string[]
    public static String[] readFileintoStringArray( String fileName )
    {
        ArrayList list = new ArrayList();
        String[]  result;
       
        try
        {
            BufferedReader reader = new BufferedReader( new FileReader( fileName ) );
            String         line;
            while ( ( line = reader.readLine() ) != null )
            {
                list.add( line );
            }
            result = new String[ list.size() ];
            for ( int idx = 0 ; idx < list.size(); idx++ )
                result[ idx ] = (String) list.get( idx );
        }
        catch (FileNotFoundException e)
        {
            System.out.println( "File " + fileName + " was not found!" );
            result = new String[0];
        } catch (IOException e)
        {
            System.out.println( "Error reading file " + fileName );
            e.printStackTrace();
            result = new String[0];
        }
       
        return result;
    }
   
    public static void main( String[] args )
    {
        try {

            // Read from lines.txt
            String[] text = readFileintoStringArray( "lines.txt" );
           
            // the background image
            String imgurl = "http://www.blueprint21.de/flashdata/thai-rice.jpg";

            // initialize with the random text and some image url
            Splash splash = new Splash( text, imgurl, true );

            // set the font for the text
            splash.setFont( new Font( "sans-serif", Font.ITALIC, 12 ) );

            // set the text color
            splash.setForeground( Color.black );

            // set the insets of area where the scolling test is displayed
            // top (157), left (50), bottom (5), right (10)
            splash.setInsets( 157, 50, 5, 10 );
           

            // open the splash window
            splash.show();

            // do the initialization or whatever
            for( int idx = 0; idx < 30 && splash.isShowing(); idx++ )
                Thread.sleep( 5000 );

            // close the splash window when the application is ready
            splash.hide();            
        } catch( Exception ex ) {
            ex.printStackTrace();
        }
        System.exit( 0 );
    }
}

0
 
zzynxSoftware engineerCommented:
- Using a simple text file containing the lines you want to display as JugglerW suggested seems OK to me. Contra: yet another file
- Since you have already a properties files you could add it over there. (keys e.g. credits_line1, credits_line2, .... credits_line20)
- I would dare to keep it even more simpler: hard code it.

e.g.
            String[] text = new String[ 10 ];
            text[0] = " Training Arts Program v1.0.1 ";
            text[1] = " ";
            text[2] = " The following people helped in the design: ";
            text[3] = "        Johnny Cash";
            text[4] = "        Peter Tosh ";
            text[5] = " ";
            text[6] = " These people helped in the the development:";
            text[7] = "        John McEnroe";
            text[8] = "        Jerry Lee Lewis";
            text[9] = "        My mother";
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
GrandSchtroumpfCommented:
ResourceBundle is good if you need to localize your text.
Since you obviously don't need to localize people's names, i would use a plain properties file.
But you should use a ResourceBundle to save all the texts that need to be localized such as job titles and all the labels and messages that your program uses.
0
 
Drop_of_RainAuthor Commented:
<< But you should use a ResourceBundle to save all the texts that need to be localized such as job titles
       and all the labels and messages that your program uses.

zzynx  What do you think about using a ResourceBundle for the program? How hard would that be to change? But being that the button labels can be changed by the buttonManager that might not work unless it can ce writen over.

I think that it would be OK to hard code it because all the people will be known in advance, and in other versions it can be updated.
0
 
JugglerWCommented:
What do you win with ResourceBundles i your splash screen? If you don't want another text file do it like zzynx propose and hard code the texts in your Splash.java file. If you need i18m you could do there easily (Replace main method in you Splash.java by this:

    public static final int GERMAN  = 0;
    public static final int ENGLISH = 1;

    public static String[][] localizedText = new String[][]
    {
        // German
        {
            "Produzent",
            "Drop of Rain",
            "",
            "Autor",
            "JugglerW"
        },
               
        // English
        {
            "Producer",
            "Drop of Rain",
            "",
            "Author",
            "JugglerW"
        }
    };
   
    public static void main( String[] args )
    {
        try {
            int language = GERMAN;
            // int language = ENGLISH;
           
            String[] text = localizedText[ language ];
           
            // the background image
            String imgurl = "http://www.blueprint21.de/flashdata/thai-rice.jpg";

            // initialize with the random text and some image url
            Splash splash = new Splash( text, imgurl, true );

            // set the font for the text
            splash.setFont( new Font( "sans-serif", Font.ITALIC, 12 ) );

            // set the text color
            splash.setForeground( Color.black );

            // set the insets of area where the scolling test is displayed
            // top (157), left (50), bottom (5), right (10)
            splash.setInsets( 157, 50, 5, 10 );
           

            // open the splash window
            splash.setVisible( true );

            // do the initialization or whatever
            for( int idx = 0; idx < 30 && splash.isShowing(); idx++ )
                Thread.sleep( 5000 );

            // close the splash window when the application is ready
            splash.setVisible( false );            
        } catch( Exception ex ) {
            ex.printStackTrace();
        }
        System.exit( 0 );
    }
0
 
girionisCommented:
>ResourceBundle is good if you need to localize your text.
>Since you obviously don't need to localize people's names, i would use a plain properties file.

ResourceBundle can be used for properties as well, since the concept behind localization and properties is *exactly* the same. Besides using a ResourceBundle is a piece of cake.
0
 
zzynxSoftware engineerCommented:
>>What do you think about using a ResourceBundle for the program?
I think you don't need that, since you don't need internationalization/localization, do you?

>>How hard would that be to change?
That's not hard at all. As girionis indicated, a resource bundle is in fact a collection of properties files
where the same keys are translated different in each language.

>>But being that the button labels can be changed by the buttonManager that might not work unless it can ce writen over.
Well, in case of localization you should exclude those button texts: each user should just enter the button texts in his own language.

I would (still) go for the hard coded way.
0
 
Drop_of_RainAuthor Commented:
I raised the points from 350 to 500 because of such good feedback Thanks for all the support everyone, you guys are great.
Christopher
0
 
GrandSchtroumpfCommented:
:°)
0
 
JugglerWCommented:
Thanks :-)
0
 
zzynxSoftware engineerCommented:
Thanks
0
 
girionisCommented:
Thank you for accepting, glad problem is solved :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now