Example: Card Game Create a class called “Card” Has thirteen static variables for the possible values of a playing card Has four static variables, one for each suit Has two instance attributes: value and suit Create a class called “Deck” Has an array of 52 cards Initialize the deck to one card of each value and of each suit Write a main function that plays “war”: Draw two cards from a deck and compare their value to see which one wins
Re-using Code Recall the purpose of classes and objects Model the real-world Encapsulate the attributes and behaviors of concepts Writing classes in a “black-box” fashion allows programmers to share and re- use code for many purposes! Now that we have Card and Deck, we can use them for basically any card game that uses a standard deck of cards The game logic will be different, but in some driver class! This (huge) benefit of object-oriented programming is what motivates the maintenance of the open source software libraries
Graphical Programming (Obviously) hugely important development in history of computers Allow much more logical and accessible presentation and manipulation of data Requires much different programming and problem-solving techniques Program efficiency becomes much more important Many more approaches to similar problems Much more hardware dependent
Graphics Basics A screen is broken down into small light-emitting diodes, forming “pixels” To display an image or text, the right ones need to be lit up or turned off For colors, there are three different hues of pixels that are adjusted to different intensities
Screen layout Screens are rendered using an x-y axis, where the origin is in the top- left Similar to how text-only terminals operated (see the terminal in Eclipse) Java will abstract away many of the finer details, giving more logical access to the viewport of your program The OS will also take care of placement, dragging, etc. of your graphics window
Getting starting with Graphics No user-interaction (buttons, text input, etc.) yet Only “output” graphics: shapes and text Requires some initial setup to prepare the graphics and window Starter file with comments: http://faculty.salisbury.edu/~jtanderson/teaching/cosc117/files/SimpleAnimationStarter.j ava Download and change the three occurrences of “SimpleAnimationStarter” to whatever class name you need
Creating a graphic Similar to the Scanner, we will get an graphics context from Java: Graphics g; The Graphics class has many methods that let us choose to display primitive graphical elements: Text rendering Rectangles Circles Full list: https://docs.oracle.com/javase/8/docs/api/java/awt/Graphics.html
Drawing Lines If we want to draw a line, we need to specify its end points Two points in 2d – four coodinates E.g. g.drawLine(xOne, yOne, xTwo, yTwo); We can loop it, and orient the lines any way we want!
Drawing rectangles g.drawRect(int x, int y, int width, int height) Draws the outline of a rectangle with top-left at (x,y) and width x height dimensions g.fillRect(…) Fills the rectangle with the current color We can change color with g.setColor(Color c) We can access different color constants like Color.BLUE, Color.GREEN, Color.RED, etc.
Drawing circles and ovals g.fillOval(int x, int y, int height, int width) Same idea as rectangle, but coodinates are the corner of the bounding rectangle Can also use g.drawOval(…)
Examples: Draw a set of lines on the screen Draw a set of nested rectangles Make the nested rectangles animated to move “inward” Draw random circles Draw a moving circle
Animate Objects Create a class called “Circle” centerX and centerY coordinates Radius size velocityX and velocityY Make one circle starting somewhere random, and update its position every frame, based on the velocity