Presentation is loading. Please wait.

Presentation is loading. Please wait.

Example: Card Game Create a class called “Card”

Similar presentations


Presentation on theme: "Example: Card Game Create a class called “Card”"— Presentation transcript:

1 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

2 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

3 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

4 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

5 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

6 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: ava Download and change the three occurrences of “SimpleAnimationStarter” to whatever class name you need

7 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:

8 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!

9 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.

10 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(…)

11 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

12 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


Download ppt "Example: Card Game Create a class called “Card”"

Similar presentations


Ads by Google