Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: My assignment - drawing to screen in a JFrame, including scrolly text.

  1. #1
    Member
    Join Date
    Dec 2009
    Location
    UK
    Posts
    58
    My Mood
    Sleepy
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default My assignment - drawing to screen in a JFrame, including scrolly text.

    My assignment was to draw a picture using a JFrame. I also wanted to do Scrolly Text.

    The most logical way to do it was to have something that most of you will never have heard of - Global Variables. I had a bit of a headache with this. Anyways, here's the main class:
    // This is the main programme to my assignment.
    // It requires the GlobalVars.java, ScrollyText.java and ExitListener.java.
    // The ScrollyText.java handles the scrolly text routine, sorry 'Method'.
    // I've also added an ExitListener routine to end all JFrame processes
    // when the JFrame is closed.
    // What this does is that it handles the JFrame by dynamically updating it
    // with a pause for the scrolly text. It draws the image first and the scrolly
    // text second.
    // Also calls some global variables from the GlobalVars file too, just
    // for the fun of it.
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferStrategy;
    import javax.swing.*;
    public class BeachHeadMain extends Canvas
    {
        // These are not global, hence the 'private' bit
        private BufferStrategy strategy;
        private final int ScreenWidth=699;
        private final int ScreenHeight=699;
        // This sets up the Scrolly as follows:
        // GlobalVars.ScrollY gives the Y position of the text
        ScrollyText st = new ScrollyText(GlobalVars.ScrollY,ScreenWidth,
        GlobalVars.ScrollText);
        int i=0, roll=4;
        public BeachHeadMain()
        {
            JFrame beachframe = new JFrame(GlobalVars.Title$);
            beachframe.addWindowListener(new ExitListener());
            // From this point on I make sure I take over drawing this frame,
            // because JFrame doesn't update dynamically, like an applet
            JPanel panel = (JPanel) beachframe.getContentPane();
            panel.setPreferredSize(new Dimension(ScreenWidth,ScreenHeight));
            panel.setLayout(null);
            // Setup canvas size and put it into the content of the frame
            setBounds(0,0,ScreenWidth,ScreenHeight);
            panel.add(this);
            // Now take complete control by tell AWT not to bother repainting the canvas since I'm
            // going to do that myself in accelerated mode
            setIgnoreRepaint(true);
            // Make the frame visible 
            beachframe.pack();
            beachframe.setResizable(false);
            beachframe.setVisible(true);
            // Double buffering enabled for the smooth scrolling
            createBufferStrategy(2);
            strategy = getBufferStrategy();
        }
        // And now here is the loop to write the scrolly text to the screen
        public void loop()
        {
            while(true) 
            {
                // Get hold of a graphics context for the accelerated
                // surface and blank it out
                Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
                // This will set the background to white
                g.setColor(new Color(255,255,255));
                g.fillRect(0,0,ScreenWidth,ScreenHeight);
                // And now I want to draw the screen first, and then the scrolly text second
                DrawScreen(g);
                DrawScrolly(g);
                // Finally, I've completed drawing so clear up the graphics
                // and flip the buffer over
                g.dispose();
                strategy.show();
                // Sleep will slow the scrolly text down
                try{Thread.sleep(10);
                }
                catch(Exception e)
                {
                }
            }
        }
        // This is the bit that draws the screen
        public void DrawScreen(Graphics2D g)
        {
            // This bit displays 'DOKEYSOFT' and does a grey-scale
            // colour roll in steps of 16 from black to white, simples!
            if (i<255)
            {
            g.setColor (new Color (i,i,i));
            g.drawString (GlobalVars.d$,588,686);
            g.drawString (GlobalVars.d$,589,686);
            g.drawString (GlobalVars.d$,588,687);
            g.drawString (GlobalVars.d$,589,687);
            i=i+roll;
            }
            if (i>=255)
            {
                i=GlobalVars.zero;
            }
            // This bit is where the magic happens in the code. It uses the
            // imported commands to draw the pixels and stuff into the window.
            // Good, isn't it?
            // Okay, here is my counter, controverially called C, oh and it's an
            // Integer too :-)
            int C=GlobalVars.zero;
            // Okay, let's get down to business and draw the rectangles first.
            // Blocky is good!
            // The data block is read until -999 is reached, which is the end
            // of the data block. This is the most logical way to do this in my
            // opinion. This way, I can draw what I like just by adding a new
            // data table into the GlobalVars file :-)
            while (GlobalVars.Rect[C]!=-999)
            {
                // Set the colour first, which will be the first three integers in the data block.
                g.setColor (new Color(GlobalVars.Rect[C],GlobalVars.Rect[C+1],GlobalVars.Rect[C+2]));
                // Now draw the damn rectangle according to the rest of the data block.
                g.fillRect (GlobalVars.Rect[C+3],GlobalVars.Rect[C+4],GlobalVars.Rect[C+5],GlobalVars.Rect[C+6]);
                // As we've used seven integers from the data table, counting from zero initially, we'll
                // add seven to our counter, which is done like this:
                C=C+7;
            }
        }
        // Now I update the scrolly text after the screen has been drawn
        public void DrawScrolly(Graphics2D g)
        {
            st.draw(g);
        }
        // Now lets execute this bad boy
        public static void main (String [] args)
        {
            BeachHeadMain bhm = new BeachHeadMain();
            System.out.println (GlobalVars.a$);
            System.out.println (GlobalVars.b$);
            System.out.println (GlobalVars.c$);
            // Start the loop (scrolling animation)
            bhm.loop();
        }
    }
    // With special thanks to Mike Dailly, Lee Fogarty and 'Sack' for helping me work out
    // the Java syntax and commands, especially how to implement global variables, which
    // helped keep my sanity during this assignment. Archi Medes helped me with the Exit
    // event listener thing, which I thought I had sorted out, but I keep forgetting
    // that every routine needs to be seperate from the main program just because
    // Java is rubbish.
    // All events dipicted within this assignment are purely ficticious, any resemblence
    // to any event being past, present or future is purely accidental.
    // No children or animals were harmed during this assignment. Thanks for reading.
    and next is the global variables:
    // This file is where all of my variables will be.
    // This might seem a bit of a backwards way of doing things,
    // but it makes the most sense to me, and that's important.
    // Any variables herein will require the GlobalVars. prefix 
    // elsewhere in this 'program'.
    public class GlobalVars
    {
        // Here is the array - or Data table in English - which reads as follows:
        // x-axis, y-axis, width, height. This is for the rectangle data.
        public static int [] Rect=
        {
            // Main screen (as in C64 screen area)
            // Colour black
            0,0,0,
            // X, Y, Width and Height
            0,0,699,566,
            // Loading strips in Yellow
            255,255,0,
            0,76,36,35,
            255,255,0,
            0,144,36,35,
            255,255,0,
            0,212,36,35,
            255,255,0,
            0,280,36,35,
            255,255,0,
            0,348,36,35,
            255,255,0,
            0,416,36,35,
            255,255,0,
            0,484,699,35,
            255,255,0,
            664,76,36,35,
            255,255,0,
            664,144,36,35,
            255,255,0,
            664,212,36,35,
            255,255,0,
            664,280,36,35,
            255,255,0,
            664,348,36,35,
            255,255,0,
            664,416,36,35,
            // Stars in text
            // Colour white
            255,255,255,
            194,40,4,2,
            255,255,255,
            202,40,4,2,
            255,255,255,
            210,40,4,2,
            255,255,255,
            218,40,4,2,
            255,255,255,
            226,40,4,2,
            255,255,255,
            234,40,4,2,
            255,255,255,
            450,40,4,2,
            255,255,255,
            458,40,4,2,
            255,255,255,
            466,40,4,2,
            255,255,255,
            474,40,4,2,
            255,255,255,
            482,40,4,2,
            255,255,255,
            490,40,4,2,
            255,255,255,
            196,42,8,2,
            255,255,255,
            212,42,8,2,
            255,255,255,
            228,42,8,2,
            255,255,255,
            452,42,8,2,
            255,255,255,
            468,42,8,2,
            255,255,255,
            484,42,8,2,
            255,255,255,
            192,44,48,2,
            255,255,255,
            448,44,48,2,
            255,255,255,
            194,48,4,2,
            255,255,255,
            202,48,4,2,
            255,255,255,
            210,48,4,2,
            255,255,255,
            218,48,4,2,
            255,255,255,
            226,48,4,2,
            255,255,255,
            234,48,4,2,
            255,255,255,
            450,48,4,2,
            255,255,255,
            458,48,4,2,
            255,255,255,
            466,48,4,2,
            255,255,255,
            474,48,4,2,
            255,255,255,
            482,48,4,2,
            255,255,255,
            490,48,4,2,
            255,255,255,
            196,46,8,2,
            255,255,255,
            212,46,8,2,
            255,255,255,
            228,46,8,2,
            255,255,255,
            452,46,8,2,
            255,255,255,
            468,46,8,2,
            255,255,255,
            484,46,8,2,
            // Char: N
            255,255,255,
            258,40,4,14,
            255,255,255,
            266,40,4,14,
            255,255,255,
            262,42,2,6,
            255,255,255,
            264,44,2,6,
            // Char: O
            255,255,255,
            274,42,4,10,
            255,255,255,
            282,42,4,10,
            255,255,255,
            276,40,8,2,
            255,255,255,
            276,52,8,2,
            // Char: W
            255,255,255,
            290,40,4,14,
            255,255,255,
            300,40,4,14,
            255,255,255,
            294,49,2,3,
            255,255,255,
            298,49,2,3,
            255,255,255,
            296,47,2,4,
            // Char: L
            255,255,255,
            322,40,4,14,
            255,255,255,
            322,52,12,2,
            // Char: O
            255,255,255,
            338,42,4,10,
            255,255,255,
            346,42,4,10,
            255,255,255,
            340,40,8,2,
            255,255,255,
            340,52,8,2,
            // Char: A
            255,255,255,
            358,40,4,2,
            255,255,255,
            356,42,8,2,
            255,255,255,
            354,44,4,10,
            255,255,255,
            362,44,4,10,
            255,255,255,
            358,46,4,2,
            // Char:D
            255,255,255,
            370,40,4,14,
            255,255,255,
            374,40,4,2,
            255,255,255,
            374,52,4,2,
            255,255,255,
            376,42,4,2,
            255,255,255,
            376,50,4,2,
            255,255,255,
            378,44,4,6,
            // Char:I
            255,255,255,
            388,40,8,2,
            255,255,255,
            390,42,4,10,
            255,255,255,
            388,52,8,2,
            // Char:N
            255,255,255,
            402,40,4,14,
            255,255,255,
            410,40,4,14,
            255,255,255,
            406,42,2,6,
            255,255,255,
            408,44,2,6,
            // Char: G
            255,255,255,
            420,40,8,2,
            255,255,255,
            418,42,4,10,
            255,255,255,
            426,42,4,2,
            255,255,255,
            424,46,2,2,
            255,255,255,
            426,46,4,6,
            255,255,255,
            420,52,8,2,
            // End of Data:
            -999,-999
        };
        public static final int zero=0;
        public static int FontSize=24;
        public static int ScrollY=632;
        public final int ScreenWidth=699;
        public final int ScreenHeight=699;
        public static String ScrollText=".... Kicking it in 2010, in an old skool stylee - "+
        "This be me first programming assignment for my Foundation Degree in Computer Enterprise"+
        " coded in Java with a hand-built data table with a loop reading it to display the above"+
        " image! I wouldn't recommend doing that, but it seemed the most logical way"+
        " to do it as far as I'm concerned because I can change the picture without"+
        " changing the program. Good, innit though?"+
        "            Greetz and a Happy New Year to you all. The above picture"+
        " is a loading screen from one of the first Commodore 64 games"+
        " I played all of those years ago way back in 1985 or '86 when"+
        " we got the famous 8-bit monolith for Christmas... oh the memories!"+
        " Okay, so Commodore purists will probably wonder how you can write to the borders"+
        " whilst loading from tape, and point out that the colours are wrong and so on, but"+
        " to be honest, I don't care. To all those damn purist: you try building a data table"+
        " from hand, working out all of the ex and why co-ordinates for the position of"+
        " each pixel and then come to me. Unless you're a certain Mr 'been doing it for"+
        " years' Kelk, you'll probably lose your sanity pretty quickly.     "+
        " I was going to add a SID player, but I didn't get the time"+
        " to do it, such are the joys of working on an assignment with a tight deadline."+
        " Anyway, I should send out some greetings and so on, so without further ado, I say"+
        " a big HELLOES to:- Peter S, Filcho, Andy the programming tutor, Mike Dailly, Jason 'TMR' Kelk,"+
        " Maciej and to all of my fellow students, Lisa A, me Mum and everyone I know - including the"+
        " members of my rather dubious FaceBook fanclub (!!!!!!), the Commodore Computer"+
        " Club (UK) (www.commodorecomputerclub.co.uk) and anyone else I've forgotten about."+
        " My advice for the new year is to play more Commodore games and stuff. Byez!!!!!!!!1ONE";
        public static String a$=" This is my interpretation of the Beach Head II loading screen";
        public static String b$="Taken from the Commodore 64 version (c) 1985 by Access Software";
        public static String c$="                   -= by Shaun Bebbington =-                   ";
        public static String d$="DONKEYSOFT";
        public static String Title$=" Beach Head II loading screen interpretation, by Shaun Bebbington MMX";
    }
    I've not included the whole data array because it was in excess of 2100 lines of data (by itself), that I worked out from hand using MS Paint to get the X and Y co-ords of each pixel and manually enter them after the Red, Green and Blue values. Essentially though, you can enter your own data table as long as it reads Red value, Green value, Blue value, X-axis, Y-axis, width and height of rectangle (1,1 smallest). You can also change the strings to make your own scrolly text etc...

    Here is the exit listener that closes down all of the processes.
    import java.awt.*;
    import java.awt.event.*;
    // This is my event which will end all processes within these programs
    // when the JFrame is closed by the traditions method.
    public class ExitListener extends WindowAdapter
    {
        public void windowClosing(WindowEvent event)
        {
            System.exit(0);
        }
    }
    and finally here is my sub routine that scrolls the text:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class ScrollyText
    {
        String scrolly;
        Font mfont;
        int Width,X,Yoff,textWidth;
        public ScrollyText(int y,int width,String text)
        {
            Yoff=y;
            X=Width=width;
            scrolly=text;
            mfont=new Font("Ariel Black",Font.BOLD,GlobalVars.FontSize);
        }
        public void draw(Graphics2D g)
        {
            g.setColor(new Color(0,0,0));
            g.setFont(mfont);
            g.drawString(scrolly, X, Yoff);
            FontMetrics fm=g.getFontMetrics(mfont);
            textWidth=fm.stringWidth(scrolly);
            X=X-1;
            if(X<-(textWidth))
            {
                X=Width;
            }
        }
    }
    Simples!!!!!!!!111111111

    Have fun :-)

    Shaun.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: My assignment - drawing to screen in a JFrame, including scrolly text.

    Instead of hard-coding the RGB values, they can be opened up directly from the file: see Java Images.

  3. #3
    Member
    Join Date
    Dec 2009
    Location
    UK
    Posts
    58
    My Mood
    Sleepy
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: My assignment - drawing to screen in a JFrame, including scrolly text.

    Quote Originally Posted by helloworld922 View Post
    Instead of hard-coding the RGB values, they can be opened up directly from the file: see Java Images.
    Yeah, or I had a method which only required me to pass through a GIF image that would give me the hex dump, and I could then add that to my data table and write the program that would read the GIF in the same way. I don't think that was the point of the assignment though.

    The benefits of the method I used, although it was rather mundane, is that I can change the picture without actually changing the program. I could also easily manipulate it, like do a zoomer by adding 1 to the width and height in each update whilst taking 1 away from the x and y asix to keep it centered by setting a few conditions or mouse events or whatever. Unfortunatley, I got so fed up with the code I handed it in as is once it was finished and working. However, I might do some old-skool demo effects now it's done.

    Regards,

    Shaun.

  4. #4
    Junior Member
    Join Date
    Jul 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My assignment - drawing to screen in a JFrame, including scrolly text.

    I had a query, I have a requirement in OBIEE reporting tool in which the user wants that suppose he has set of images stored in his local machine , initially he wants to setup say 1.jpg as background image in the login screen later on he wants to update the background image by just changing the file name and location on his machine.

    I have created login screen in java swings since it is not a web application but confused whether to use FILE IO or anything else for setting and updating the background image.

    So need your feedback or help wrt code in the same on sidhu220409@gmail.com.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My assignment - drawing to screen in a JFrame, including scrolly text.

    Can you describe your problem a bit more?
    Are you able to read the image file into your program into an image object? See ImageIO class
    Can you display that image as the background?

Similar Threads

  1. Drawing image on JPanel from another frame
    By jeryslo in forum AWT / Java Swing
    Replies: 3
    Last Post: December 8th, 2009, 04:01 PM
  2. clear screen.
    By chronoz13 in forum Java Theory & Questions
    Replies: 10
    Last Post: December 5th, 2009, 07:27 AM
  3. Help - Return to menu screen
    By throzen in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 29th, 2009, 01:44 PM
  4. Content positioning on the screen
    By Drakenmul in forum AWT / Java Swing
    Replies: 1
    Last Post: July 27th, 2009, 09:02 AM
  5. Drawing "Hello world" on screen
    By shikh_albelad in forum Java SE APIs
    Replies: 4
    Last Post: June 11th, 2009, 03:21 AM