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

graphics
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Graphics

Lecture 18 COP 3252 Summer 2017 June 6, 2017

slide-2
SLIDE 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

  • lder 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

slide-3
SLIDE 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.

slide-4
SLIDE 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

  • f 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

  • ne 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

slide-5
SLIDE 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

slide-6
SLIDE 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

slide-7
SLIDE 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

slide-8
SLIDE 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

slide-9
SLIDE 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().

slide-10
SLIDE 10

More on paint()

◮ These methods both take as a parameter a reference variable

  • f 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.

slide-11
SLIDE 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.

slide-12
SLIDE 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.

slide-13
SLIDE 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.

slide-14
SLIDE 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.

slide-15
SLIDE 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

slide-16
SLIDE 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

  • bject as a parameter.
slide-17
SLIDE 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.

slide-18
SLIDE 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