270 likes | 687 Views
In this chapter, you will:. Learn about the paint() and repaint() methodsUse the drawString() method to draw stringsUse the setFont() and setColor() Graphic object methodsCreate Graphics and Graphics 2D objectsDraw lines, rectangles, ovals, arcs, and polygons. Copy an areaLearn more about fonts and their methodsDraw with Java 2D graphicsAdd sound, images, and simple animations to Swing applets.
E N D
1. Java Programming, Second Edition Chapter Ten
Graphics
2. In this chapter, you will: Learn about the paint() and repaint() methods
Use the drawString() method to draw strings
Use the setFont() and setColor() Graphic object methods
Create Graphics and Graphics 2D objects
Draw lines, rectangles, ovals, arcs, and polygons
3. Copy an area
Learn more about fonts and their methods
Draw with Java 2D graphics
Add sound, images, and simple animations to Swing applets
4. Learning about the paint() and repaint() Methods paint() method- Runs when Java displays your Swing applet
Is used within every Swing applet
public void paint (Graphics g)
repaint() method- Use when a window needs to be updated, such as when it contains new images
Java system calls the repaint() method when it needs to update a window
repaint() creates a Graphics object for you
5. Using the drawString() Method to Draw Methods drawString() method- Allows you to draw a String in a Swing applet window
Requires three arguments:
A String
An x-axis coordinate
A y-axis coordinate
6. Using the setFont() and setColor() Graphics Object Methods setFont() and setColor() Graphics object methods- Improve the appearance of Graphics objects
setFont() method requires a Font object
Font someFont = new Font(“TimesRoman”,Font.BOLD, 16);
7. Using the setFont() and setColor() Graphics Object Methods setColor() method- Color class contains 13 constants
Use any of these constants as an argument to the setColor() method
brush.setColor(Color.green);
9. The Swing Applets Background Color setBackground()
setBackground(Color.pink);
Change the background color of your Swing applet
Do not need a Graphics object to change the Swing applet’s background color
10. Creating Graphics and Graphics 2D Objects You can instantiate your own Graphics or Graphics 2D objects
11. Drawing Lines drawLine() method- To draw a straight line between any two points on the screen
Takes four arguments
The x- and y-coordinates of the line’s starting point
The x- and y-coordinates of the line’s ending point
pen.drawLine(10,10,100,200);
12. Drawing Rectangles drawRect() and fillRect() methods- To draw the outline of a rectangle or to draw a solid, or filled, rectangle
These methods require four arguments:
First two represent the x- and y-coordinates of the upper-left corner of the rectangle
Last two represent the width and height of the rectangle
drawRect(20,100,200,10);
13. Drawing Rectangles clearRect() method- Draws a rectangle
Requires four arguments
drawRect() and fillRect() methods use the current drawing color
clearRect() method uses the current background color to draw what appears to be an empty or “clear” rectangle
14. Drawing Rectangles drawRoundRect() method- Create rectangles with rounded corners
Requires six arguments:
x- and y-coordinates of the upper-left corner
The width and height
The arc width and height associated with the rounded corners
15. Drawing Ovals drawOval()
fillOval()
Both draw ovals using the same four arguments that rectangles use
17. Drawing Arcs Draw an arc using the Graphics drawArc() method
Provide six arguments:
x-coordinate of the upper-left corner of an imaginary rectangle that represents the bounds of the imaginary circle that contains the arc
y-coordinate of the same point
Width of the imaginary rectangle that represents the bounds of the imaginary circle that contains the arc
Height of the same imaginary rectangle
Beginning arc position
Arc angle
19. Drawing Arcs fillArc() method- Creates a solid arc
Arc is drawn and two straight lines are drawn from the arc end points to the center of the imaginary circle whose perimeter the arc occupies
solidarc.fillArc(10,50,100,100,20,320);
solidarc.fillArc(200,50,100,100,340,40);
21. Creating Three-Dimensional Rectangles draw3DRect() method- To draw a rectangle that appears to have “shadowing” on two of its edges
Requires a fifth argument in addition to the x- and y-coordinates and width and height
Fifth argument is a Boolean value
True if you want the raised rectangle effect
False if you want the lowered rectangle effect
fill3DRect() method for creating filled three-dimensional rectangles
22. Creating Polygons drawPolygon() method- To create a shape that is more complex than a rectangle
Requires three arguments: two integer arrays and a single integer
First integer array holds a series of x-coordinate positions
Second array holds a series of corresponding y-coordinate positions
Third integer argument is the number of pairs of points you want to connect
23. Copying an Area copyArea() method- Use to copy an area
Requires 6 parameters
x-coordinate and y-coordinate of the upper-left corner of the area to be copied
Width and height of the area to be copied
The horizontal and vertical displacement of the destination of the copy
24. Learning More about Fonts and Their Methods getAllFonts()- Used to discover the fonts that are available on your system
Part of the GraphicsEnvironment class in the java.awt package
getScreenResolution() and getScreenSize()-Used to get the resolution and screen size of your system
getScreenResolution()- returns the number of pixels as an int type
25. Drawing with Java 2D Graphics Java 2D creates higher-quality two-dimensional graphics, images, and text
Includes fill patterns such as gradients
Strokes that define the width and style of a drawing stroke
Anti-aliasing, a graphics technique for producing smoother on-screen graphics
26. Specifying the Rendering Attributes Fill patterns- Control how a drawing will be filled in
Gradient fill- A gradual shift of color
Acyclic- color shift occurs once
Cyclic- color shift occurs repeatedly
setStroke()- Changes the width of the drawing line
27. Adding Sound, Images, and Simple Animation to Swing Applets Java 2 supports sound
Use the play() method to retrieve and play sound
play() with one argument- the argument is a URL
play() with two arguments- loads and plays the audio file
28. Adding Sound, Images, and Simple Animation to Swing Applets Images formats supported by Java include:
Graphics Interchange Format (GIF)
Maximum of 256 colors
Join Photographic Experts Group (JPEG)
Stores mostly photographs
Portable Network Graphics (PNG)
Stores images in a lossless form