Download presentation
Presentation is loading. Please wait.
1
Basic Graphics Drawing Shapes 1
2
Learning Goals By the end of the lesson you should know:
Using the Helper Library to: Use predefined colours Create custom colours Where to write our graphics code The screen resolution How to draw a line How to draw a rectangle How to draw a filled rectangle
3
The Helper Library The Helper library built into the Java Game Engine allows you to do a number of things: Generate random values within a given range Take advantage of a number of predefined colours Use custom colours Using the Helper Library Like other libraries such as Math, we simply need to use the library’s name followed by the desired value or command E.g. Helper.BLACK
4
Predefined Colours There are 13 predefined colours you can take advantage of: WHITE RED GREEN BLUE ORANGE YELLOW GRAY BLACK PINK CYAN DARK_GRAY LIGHT_GRAY MAGENTA To use these colours you need to follow the Helper. prefix with the colour you need: Helper.WHITE or Helper.BLACK
5
Custom Colours In order to create custom colours it is best practice to create a variable to store that new colour. The data type for your variable will be Color From here we will take advantage of one of two Helper commands: Helper.GetColor(int red, int green, int blue) Helper.GetColor(int red, int green, int blue, int transparency) The red, green and blue values are numbers between 0 to Think of it like mixing paint, these three colours can produce any colour you want. HINT: Go to colorpicker.com to get exact values for any colour you want Transparency is also a value from 0 to 255, in this case however the number represents how transparent (see-through) the colour is is completely opaque and 0 and invisible. When creating your colours, it is best to make these variables global, but they do NOT need to be static. E,g. Color skyColor = Helper.GetColor(0,0,200);
6
Where to Draw? public void Draw(GameContainer gc, Graphics2D gfx) {
For our purposes we will be using the Game project on the Moodle which has the Java Game Engine built in and ready to go. Recall that all of our drawing commands will go in the Draw command of our Game Loop. public void Draw(GameContainer gc, Graphics2D gfx) { //Add your draw code here }
7
The screen X (0,0) You will be drawing to the game window which you specify as a certain size in the Main code of the Game Project Assume you use the default values of 800 pixels wide by 600 pixels high Also note that the (0,0) origin is NOT in the bottom left of the screen, it is at the top left of the screen. This implies coordinate (800,600) is located at the bottom right of the screen. This means that x in creases as you move right and y increases as your move DOWN Y (800,600)
8
Draw a Line Draw.Line(gfx,50,125,300,200,3,Helper.RED,0.5f)
Draw.Line(gfx, x1, y1, x2, y2, lineWidth, Color, transparency) gfx represents your game window, leave this alone x1, y1 is the coordinate of the first end point of the line x2, y2 is the coordinate of the second end point of the line lineWidth is the number of pixels wide the line is Color is the colour of the line, e.g. Helper.RED transparency is how see-through the line is, a float number from 0 to 1 where 0 is invisible Example: Draw a red line from (50,125) to (300,200) that is half transparent and 3 pixels wide Draw.Line(gfx,50,125,300,200,3,Helper.RED,0.5f)
9
Draw a Rectangle Draw.Rect(gfx, x, y, width, height, lineWidth, color, transparency) gfx represents your game window, leave this alone x,y is the coordinate for the rectangle’s top left corner width, height are the dimensions of the rectangle color is the border’s colour transparency is how see-through the line is, a float number from 0 to 1 where 0 is invisible Example: Draw a 200x400 White opaque rectangle at (25,100) Draw.Rect(gfx,25,100,200,400,5,Helper.WHITE, 1f);
10
Draw a Filled Rectangle
Draw.FillRect(gfx, x, y, width, height, color, transparency) gfx represents your game window, leave this alone x,y is the coordinate for the rectangle’s top left corner width, height are the dimensions of the rectangle color is colour the rectangle is filled in with transparency is how see-through the line is, a float number from 0 to 1 where 0 is invisible Example: Draw a 300x100 ¾ opaque yellow filled rectangle at (40,150) Draw.FillRect(gfx,40,150,300,100,Helper.YELLOW,0.75f);
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.