Java programming second edition
This presentation is the property of its rightful owner.
Sponsored Links
1 / 28

Java Programming, Second Edition PowerPoint PPT Presentation


  • 63 Views
  • Uploaded on
  • Presentation posted in: General

Java Programming, Second Edition. Chapter Ten Graphics. 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

Download Presentation

Java Programming, Second Edition

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Java programming second edition

Java Programming, Second Edition

Chapter Ten

Graphics


In this chapter you will

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


Java programming second edition

  • Copy an area

  • Learn more about fonts and their methods

  • Draw with Java 2D graphics

  • Add sound, images, and simple animations to Swing applets


Learning about the paint and repaint methods

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


Using the drawstring method to draw methods

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


Using the setfont and setcolor graphics object methods

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);


Using the setfont and setcolor graphics object methods1

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);


The swing applets background color

The Swing Applets Background Color

setBackground()

  • setBackground(Color.pink);

  • Change the background color of your Swing applet

  • You do not need a Graphics object to change the Swing applet’s background color


Creating graphics and graphics 2d objects

Creating Graphics and Graphics 2D Objects

  • You can instantiate your own Graphics or Graphics 2D objects


Drawing lines

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);


Drawing rectangles

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);


Drawing rectangles1

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


Drawing rectangles2

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


Drawing ovals

Drawing Ovals

  • drawOval()

  • fillOval()

  • Both draw ovals using the same four arguments that rectangles use


Drawing arcs

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


Drawing arcs1

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);


Creating three dimensional rectangles

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


Creating polygons

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


Copying an area

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


Learning more about fonts and their methods

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


Drawing with java 2d graphics

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


Specifying the rendering attributes

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


Adding sound images and simple animation to swing applets

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


Adding sound images and simple animation to swing applets1

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


  • Login