1 / 12

Graphics in Java

Graphics in Java. Graphical information appears onscreen inside frame objects actually frame windows with title bars To use frames, import the JFrame class from the javax.swing package. Displaying Java graphics. To display graphics, frames need to be constructed,

aine
Download Presentation

Graphics in Java

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Graphics in Java • Graphical information appears onscreen inside frame objects • actually frame windows with title bars • To use frames, import the JFrame class from the javax.swing package.

  2. Displaying Java graphics • To display graphics, frames need to • be constructed, • be given a nonzero size, and • be made visible • Frames often should • have titles • halt their creating program when they are closed • Methods and constructors to do all this are described on p. 61 of the text.

  3. Coordinates and frames • Frames use the coordinate system that we saw in Section 2.4. • This system is like a 2-dimensional array of pixels. • But note that the point at position (x,y) is not in row x and column y. • instead it's in row y and column x.

  4. Java components • Objects that appear in frames (component objects) • must be added explicitly to frames, and • must know how to draw themselves. • Component objects are those whose class extends the JComponent class.

  5. Drawing Java components • Component objects need a public method voidpaintComponent that takes a Graphicsargument. • To draw a component object, cast the Graphicsargument to a Graphics2Dobject, then send messages to this object. • The component object needs to know how to draw its own components. • the methods to do this needn't have any special name.

  6. The Graphics2D class • Methods for the Graphics2D class include • draw • setColor • fill, and • drawString • The drawand fill methods take Shape objects as arguments.

  7. Shapes in Java • Instances of the Rectangle, Ellipse2D.Double, and Line2D.Double class are Shape objects. • One Ellipse2D.Double constructor has four arguments like those of Rectangle. • They specify a bounding box (or framing rectangle, or bounding rectangle) for the ellipse • One constructor for Line2D.Double has 4 arguments -- the x and y coordinates of one endpoint, then those of the other.

  8. Drawing text strings in Java • The drawString method takes as arguments the string and the coordinates of the lower-left corner of its desired position • cf. Figure 23 on p. 67 of the text.

  9. Colors in Java • The setColor method takes a Color object as argument and uses this color for subsequent drawing. • An instance of the Color class can be initialized to one of the constants of that class • cf. the table on p. 68 • Or a 3-argument constructor can be used for initialization • it takes the color’s red, green, and blue components as integers from 0 through 255

  10. Important details for Java graphics • The Graphics, Graphics2D, Color, Ellipse2D.Double, and Line2D.Double classes should be properly imported • cf. the example on p. 69 of the text • Note that to import the latter two classes, you don’t need to specify the full class name. • To add a component to a frame object, pass the add message to the frame with the component as argument.

  11. Java applets • One common application of Java graphics is to applets – programs that run inside a web browser • cf. page 6 of the text

  12. Drawing complicated images • For all but the simplest drawings, it’s a good idea to have a class for each object in the drawing. • If objects have several pieces of the same type, it’s likely worth having classes for them. • cf. Section 3.8

More Related