Solved

Strings into a splash page

Posted on 2004-08-07
13
357 Views
Last Modified: 2010-03-31
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
Comment
Question by:Drop_of_Rain
  • 3
  • 3
  • 3
  • +2
13 Comments
 
LVL 35

Assisted Solution

by:girionis
girionis earned 100 total points
ID: 11745923
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
 
LVL 7

Assisted Solution

by:JugglerW
JugglerW earned 200 total points
ID: 11747338
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
 
LVL 37

Accepted Solution

by:
zzynx earned 100 total points
ID: 11747611
- 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
 
LVL 30

Assisted Solution

by:GrandSchtroumpf
GrandSchtroumpf earned 100 total points
ID: 11748778
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
 

Author Comment

by:Drop_of_Rain
ID: 11749221
<< 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
 
LVL 7

Assisted Solution

by:JugglerW
JugglerW earned 200 total points
ID: 11750129
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 35

Expert Comment

by:girionis
ID: 11750294
>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
 
LVL 37

Expert Comment

by:zzynx
ID: 11751459
>>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
 

Author Comment

by:Drop_of_Rain
ID: 11752511
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
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 11752724
:°)
0
 
LVL 7

Expert Comment

by:JugglerW
ID: 11753288
Thanks :-)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11754106
Thanks
0
 
LVL 35

Expert Comment

by:girionis
ID: 11759900
Thank you for accepting, glad problem is solved :)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

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 had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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:
This video teaches viewers about errors in exception handling.

760 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

23 Experts available now in Live!

Get 1:1 Help Now