HSA2 is a library created so that students can start using graphics right away when learning Java.
The syntax is generally identical to Swing syntax so that students can transition to Swing graphis quite easily next year.
This a template for a program that only does drawing on the screen. Animation etc, will come later.
This is a template for you to copy and use in your programs. You copy the file to a new file name, rename a few things below, and then add your code and change these comments.
Change "GraphicsTemplate" to whatever you are calling your program. Don't call it GraphicsTemplate.java.
You can also change the size and title of the Graphics Console
package YOUR_PACKAGE_NAME_HERE; // change this to match the name of the package (folder) that code is in import hsa2.GraphicsConsole; // this imports the code from the hsa2 new package import java.awt.Color; // this imports code for using colors and fonts import java.awt.Font; public class GraphicsTemplate //there are three places where this name needs to be changed when you make a new class. (1) { public static void main(String[] args) { new GraphicsTemplate(); // this is the name of your class (2) } //Global variables here: GraphicsConsole gc = new GraphicsConsole(800, 600, "HSA2 Graphics"); GraphicsTemplate(){ //constructor. Name of class. (3) //all drawing goes here gc.setBackgroundColor(Color.YELLOW); gc.clear(); gc.drawLine(50,50,300,100); //will draw in black since we haven't changed the foreground colour gc.setColor(Color.blue); gc.fillOval(400, 400, 50, 100); //..... } } //end of class
This a template for a program that will do any sort of graphics animation. The only future improvement is to draw onto a buffered image, then you can make it faster and set the framerate.
This is a template for you to copy and use in your programs. You copy the file to a new file name, rename a few things below, and then add your code and change these comments.
/* • Notice some constants. These make it easy to change numbers in the code without having to do search and replace. Get used to using constants for these sort of things. I can change my screen size and EVERYTHING still works. • Notice some new global variables: lives, isPlaying (this will be used to end the game) • A game loop has been added. This is a standard way of doing things. • The sleep() function is ESSENTIAL. This allows the computer to do other things. It can be used to vary the speed of the game ball.xspeed and ball.yspeed are how many pixels the ball moves each time. If this number is larger, the ball moves faster. However, it also becomes more jerky because it actually jumps this number of pixels. So keep the xspeed small and vary the game speed by changing SLEEPTIME. It is in milliseconds • SYNCHRONIZED has been added to drawGraphics(). This reduces flickering during animation a lot (and is one reason why all drawing needs to be in only one place) • Interestingly, both a capital or lowercase Q will end the game. • Always check to make sure that Eclipse hasn't added in some weird import. It tends to do that when you cut and paste code. */ package YOUR_PACKAGE_NAME_HERE; // change this to match the name of the package (folder) that code is in import hsa2.GraphicsConsole; import java.awt.Color; public class AnimationMain {// change this to whatever you are calling your program. public static void main(String[] args) { new AnimationMain(); } /***** Constants *****/ static final int SLEEPTIME = 10; static final int SCRW = 800; static final int SCRH = 600; /***** Global (instance) Variables ******/ GraphicsConsole gc = new GraphicsConsole (SCRW, SCRH); boolean isPlaying = true; //is the game still on? int lives = 3; //how many lives you have //put any other objects that you're using here /****** Main program********/ (technically called a Constructor) AnimationMain() { initialize(); // ~~~~~MAIN GAME LOOP~~~~~ while (gc.getKeyCode() != 'Q' && isPlaying) { //press Q to quit //do all of the moving and collisions that you need to do //also check for specific keystrokes here (e.g. firing, teleporting, etc) fireLaser(); moveBall(); movePaddle(); checkCollisions(); drawGraphics(); gc.sleep(SLEEPTIME); if (lives <= 0) isPlaying = false; }// ~~~~~END MAIN GAME LOOP~~~~~ //Ending the game //gc.drawString("GAME OVER", 30, 30); //gc.sleep(2000); //the line below does the same as the two above. gc.showDialog("Thank you for playing!", "Game Over"); gc.close(); //close the window and end the program } /****** END Main program********/ /****** Methods for game *******/ void initialize() { //do all setup for gc (graphics console) here //and create all game objects here } /* Method: drawGraphics() * ALL graphics drawing must be done here. * Do not draw on gc anywhere else in your program (if you want it to work properly) */ void drawGraphics() { synchronized(gc) { gc.clear(); //clear screen and then redraw everything gc.setColor(Color.WHITE); gc.drawString("LIVES = " + lives, 30, 70); //etc. etc. } } }
The first way as seen in detail above
import hsa2.GraphicsConsole; public class AnimationMain { public static void main(String[] args) { new AnimationMain(); } GraphicsConsole gc = new GraphicsConsole (SCRW, SCRH); AnimationMain() { initialize(); /****** Main game loop ********/ while (gc.getKeyCode() != 'Q' && isPlaying) { moveBall(); drawGraphics(); gc.sleep(SLEEPTIME); } }
Here's another way of doing this
import hsa2.GraphicsConsole; public class AnimationMain { public static void main(String[] args) { new AnimationMain().run; } GraphicsConsole gc = new GraphicsConsole (SCRW, SCRH); AnimationMain() { put initialization here } void run(){ /****** Main game loop ********/ while (gc.getKeyCode() != 'Q' && isPlaying) { moveBall(); drawGraphics(); gc.sleep(SLEEPTIME); } }
This would be for faster animation