graphics
play

Graphics Lecture 18 COP 3252 Summer 2017 June 6, 2017 Graphics - PowerPoint PPT Presentation

Graphics Lecture 18 COP 3252 Summer 2017 June 6, 2017 Graphics classes In the original version of Java, graphics components were in the AWT library (Abstract Windows Toolkit) Was okay for developing simple GUI applications For


  1. Graphics Lecture 18 COP 3252 Summer 2017 June 6, 2017

  2. Graphics classes ◮ In the original version of Java, graphics components were in the AWT library (Abstract Windows Toolkit) ◮ Was okay for developing simple GUI applications ◮ For different platforms, AWT components mapped to platform-specific components. ◮ Prone to platform specific bugs. ◮ Primary package: java.awt . See other awt package APIs on docs.oracle.com/javase/8/docs/api/ ◮ When Java 2 was released, a library known as the Swing components were introduced with the idea of replacing the older AWT user-interface components (like Button, TextField, TextArea, etc). ◮ less dependent on target platform ◮ a more robust and flexible library ◮ Primary package: javax.swing ◮ Since release of Java 2, the Swing components are recommended for building graphic user interfaces for later browsers

  3. Java Graphics API (Using Swing Components) ◮ Component : A base class for all non-menu graphic user interface classes. ◮ Container : a base class for container classes. A container is used to group smaller components. The most important containers are: ◮ JApplet - for holding Applets ◮ JFrame - for holding GUI components in applications. A window that is on the outer level (not inside another window) ◮ JPanel - invisible container holding user-interface components. Can be nested, and can be used as canvases for drawing graphics. ◮ JDialog - for creating dialog boxes (usually temporary popup messages or dialogs for receiving additional info.

  4. JComponent Base class for all of the lightweight Swing components, which are graphical items places on the canvases or containers. Its subclasses are the basic elements for constructing GUIs. Here are just a few of the more common elements: ◮ JButton - for creating push buttons ◮ JCheckBox - for creating toggle checkboxes ◮ JMenu - for pop-up menus ◮ JRadioButton - for radio buttons (made into a group, only one can be selected) ◮ JLabel - a display area for a short string or image ◮ JList - a component allowing the user to select from a list ◮ JOptionPane - a component allowing the user to pop up an easy dialog box as an information message or for user input ◮ JTextField - component allowing an editable line of text ◮ JTextArea - multi-line area for displaying text

  5. Helper classes Helper classes - used by components and containers to control drawing and placing of objects. Some important helper classes (from package java.awt ): ◮ Graphics - abstract class. Provides graphical context for drawing ◮ Color - used for specifying colors in components and drawings ◮ Font - specify fonts used in Graphics drawings ◮ FontMetrics - abstract class. Encapsulates information and properties about the rendering of a font on screen ◮ Dimension - encapsulates width and height of a component in an object

  6. Events ◮ Event: A signal that something has happened in a program. Examples: Button clicks, mouse movements, menu selections ◮ GUI programs generally driven by events, rather than a specific procedural order ◮ vents are handled with event objects. These are triggered by actions on source objects (components or objects on which the event is generated), and they must implement corresponding event listener interfaces. The listener listens for the event, and invokes an event handler when event occurs. ◮ java.util.EventObject : Base class for event classes in Java

  7. Some examples of event types: These are just a few examples, not a comprehensive list. ◮ ActionEvent - clicking a button, pressing return on a text field ◮ ItemEvent - clicking a check box, selecting an item ◮ WindowEvent - Closing a window, opening a window ◮ ContainerEvent - component added to a container ◮ ComponentEvent - resizing a component, hiding a component ◮ TextEvent - changing a text value ◮ MouseEvent - clicking the mouse, dragging the mouse ◮ KeyEvent - pressing a key on the keyboard

  8. paint() methods ◮ Lightweight Swing components that extend class JComponent have a method called paintComponent , with this prototype: public void paintComponent(Graphics g) ◮ Another similar method is the paint method in class Component (and thus all its children) with this prototype: public void paint(Graphics g) ◮ The JComponent version of paint() actually delegates its work to three methods: paintComponent, paintBorder, and paintChildren

  9. paint() methods ◮ The idea behind paint() is that they are invoked for any component automatically whenever that component needs to be drawn or re-drawn. Some examples of triggering events: ◮ When the component first is placed on the application. ◮ When the component is resized. ◮ When the component is covered by some other application, then uncovered and comes to the forefront again. ◮ Since this is triggered by such events, the programmer seldom needs to call paint() or paintComponent() explicitly. ◮ The programmer can call repaint() (also a Component method) to force the paint operation, if the need arises (i.e. some situation not covered by the automatic calls to paint() .

  10. More on paint() ◮ These methods both take as a parameter a reference variable of type Graphics – which is an abstract class. ◮ The object will be a subtype that handles the drawing context for the given platform. ◮ For Swing components, it is usually sufficient to just define paintComponent() for drawing aspects, unless you want to control the other parts (border, children) as well. ◮ So, what can we DO in the paint() or paintComponent() methods? Pretty much anything that’s available in the Graphics class, and then some.

  11. class Graphics and other useful helper classes The Graphics Class ◮ Helps manage drawing on the screen for GUI applications and applets. ◮ Keeps track of state information like current font, current color, the Component object being drawn on, and more. ◮ Has methods for drawing various kinds of shapes (lines, ovals, polygons, rectangles, etc) as well as strings. ◮ Also has methods for setting the font, the color, the current clipping area, the paint mode, and other status information.

  12. The Color Class ◮ Used for specifying colors in components and drawings. ◮ Colors stored and specified with RGB (Red Green Blue) values. ◮ RGB values can be specified with ints (0-255) or floats (0.0-1.0). ◮ Color constants exist for common colors (Color.BLUE, Color.GREEN, etc). ◮ To find out or set the current drawing color, use the Graphics methods getColor() and setColor() . Example: g.setColor(Color.MAGENTA); g.setColor(new Color(255, 128, 3)); //RGB values ◮ JColorChooser - a javax.swing component that enables application users to choose colors.

  13. The Font Class ◮ Specify fonts used in Graphics drawings. ◮ Physical fonts are actual fonts on a system – these depend on platform and what fonts are installed on a system. ◮ Logical fonts are the 5 font families supported in Java: Serif, Sans Serif, Monospaced, Dialog, and DialogInput. When using logical fonts, an appropriate font on the given system will be chosen. ◮ Font constructor takes three parameters: font name, font style, font size ◮ Font name can be physical or logical. ◮ Font styles are plain, italics, or bold. ◮ Font size measured in points .

  14. Font Examples ◮ To set or find out the current drawing font, use the Graphics methods getFont() and setFont() . Example: Font f = g.getFont(); // retrieve current font g.setFont(new Font("Serif", Font.ITALICS, 12)); ◮ Other methods available in class Font to set or retrieve properties for a Font object.

  15. The FontMetrics class ◮ Abstract class. Encapsulates information and properties about the rendering of a font on screen. ◮ Helps track more specific font information like height, descent, ascent, and leading (interline spacing). ◮ Graphics class has a couple of methods named getFontMetrics(): FontMetrics m1, m2; m1 = g.getFontMetrics(); //current font info m2 = g.getFontMetrics(f1); //info about font f1

  16. The Polygon Class ◮ Helper class for representing information about Polygons . ◮ Stores a list of (x,y) coordinate pairs, representing vertices of a polygon. ◮ Several Graphics class methods are for drawing polygons - drawPolygon(), drawPolyLine, fillPolygon . ◮ There are versions of these last two that take a Polygon object as a parameter.

  17. Java2D The Java2D API provides advanced graphics capabilities, for more detailed and complex two-dimensional drawing. ◮ Allows more complex drawing, like lines of varying thickness, filling shapes with colors and patterns, drawing dashed lines, composite overlapping text and graphics, gradients and textures, and more. ◮ Need to use an instance of class Graphics2D , which is a subclass of class Graphics. ◮ Must cast the Graphics object in the paintComponent() method into a Graphics2D reference when using: Graphics2D g2d = (Graphics2D) g; ◮ For more details, look up the class on the Oracle Java Documentation website.

  18. Java2D Packages Java2D involves a variety of packages: ◮ java.awt ◮ java.awt.image ◮ java.awt.color ◮ java.awt.font ◮ java.awt.geom ◮ java.awt.print ◮ java.awt.image.renderable

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend