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:
and next is the global variables:// 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.
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...// 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"; }
Here is the exit listener that closes down all of the processes.
and finally here is my sub routine that scrolls the text: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); } }
Simples!!!!!!!!111111111import 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; } } }
Have fun :-)
Shaun.