Java Programming, Second edition

slimwhimperSoftware and s/w Development

Nov 3, 2013 (3 years and 8 months ago)

75 views

Graphics Programming

In this class, we will cover:


The difference between AWT and Swing


Creating a frame


Frame positioning


Displaying information in a frame


Using Graphics and Graphics 2D objects


The repaint() method


Using the drawString() method to draw strings


Using Color


Displaying text with multiple fonts

Introduction to

Graphics Programming


So far, user interaction has been limited


application parameters and JOptionPane


Users expect more varied interactions


Today we will discuss how to:


resize windows on the screen


display text in a panel


adding color to windows and objects within the window


displaying text with multiple fonts


Next class we will go further and talk about:


displaying 2D and 3D objects


copying an area


displaying images

Introduction to

Graphics Programming


After that we will discuss how to:


process events such as keystrokes and mouse clicks


add interface elements like menus and buttons

AWT and Swing


Java 1.0 was introduced with a class library called
Abstract Window Toolkit (AWT)


used for basic user interface


delegated creation and behavior of interface elements to
the native GUI tookit on the target platform


Windows vs. Solaris vs. Macintosh, etc


Downside to AWT:


Worked well for simple applications but difficult to write high
-
quality portable graphics


Limited graphics programming to the lowest common
denominator.


Different platforms had different bugs

AWT and Swing


In 1996 Sun worked with Netscape to create
Swing


In Swing user interface elements are painted onto blank
windows


Swing is not a complete replacement of AWT. Instead
it works with AWT.


Swing simply gives you more capable user interface
components.


However, even though AWT components are still
available, you will rarely use them.

Swing


Reasons to choose Swing:


much richer and more convenient set of user interface
elements


depends far less on the underlying platform so it is less
vulnerable to platform
-
specific bugs


gives a consistent user experience across platforms


fullfill’s Java’s promise of “Write Once, Run
Anywhere”


Easier to use than AWT

How to Create Graphics in Java


Here are the basic steps you need to take to
include graphics in your program:


Create a frame


Create a panel


Override the paintComponent() method in your panel

Creating a Frame


A frame is the top
-
level window (a window that is
not contained in another window)


Frames are containers.


Use the JFrame class


This is Swing’s version of frame.


Note: Swing component classes start with a ‘J’.


Swing components are placed in the javax.swing
package.

Creating a Frame


see


http://www2.bc.edu/~bernier/MC697/LectureNotes/Simple
FrameTest.java

Frame Positioning


Most methods for working the size and position of a frame
come from the various superclasses of JFrame. Some
important methods include:


the
dispose()

method: closes the window and reclaims system
resources.


the
setIconImage()

method: takes an Image object to use as the
icon when the window is minimized


the
setTitle()

method: changes the text in the title bar.


the
setResizable()

method: takes a boolean to determine if a frame
will be resizeable by the user.


the
setLocation()

method: repositions a frame


the
setBounds()

method: both repositions and resizes a frame.


the
setExtendedState(Frame.MAXIMIZED_BOTH):

maximizes the size of a frame

Frame Positioning


Note: If you don’t explicitly size the frame, it will
default to being 0 by 0 pixels, which is invisible.


In a professional application, you should check the
resolution of the user’s screen to determine the
appropriate frame size.

Displaying Information in a Panel


It is possible to draw messages and objects
directly onto a frame, but this is not considered
good programming practice.


In Java frames are really designed to be containers for
components such as menu bars and other user interface
elements.


You normally draw on another component called a
panel
.


You add a panel to your frame.


Swing programmers are most concerned with the
content pane

Displaying Information in a Panel


The content pane can be obtained this way:

Container contentPane = frame.getContentPane();



Once we have the content pane, we can add a
panel to it.


Panels:


implemented by extending the
JPanel

class


have a surface onto which you can draw


are themselves containers so they can hold other user
interface components like menus, buttons, etc.

Displaying Information in a Panel


To draw on a panel you need to:


define a class that extends
JPanel


override the
paintComponent

mehtod in that class



The
paintComponent

method takes in one
parameter of type
Graphics
.

Displaying Information in a Panel


The
Graphics

object:


remembers a collection of settings for drawing images
and text (e.g. fonts, current color, etc.)


all drawing in Java must go through a
Graphics

object


has methods to draw patterns, images, and text


These operations are limited though (e.g. can’t vary line
thickness or rotate shapes, etc.)


So, Sun created the
Graphics2D

class which is much
more powerful

Displaying Information in a Panel


The
Graphics2D

object:


is a subclass of the
Graphics

object


to draw shapes using the
Graphics2D

object, simply
cast the
Graphics

object in your
paintComponent

method to a
Graphics2D

object


public void paintComponent(Graphics g) {


Graphics2D g2 = (Graphics2D)g;


….

}


Displaying Information in a Panel


So, now that we understand the
Graphics

obect,
let’s go back to the
paintComponent

method


the first line in your override of this method needs to be

super.paintComponent(g)

so that the superclass method gets executed


never call the
paintComponent

method yourself in your
programs


Every time a window needs to be redrawn (e.g. user resizes a
window), an event handler notifies the component causing the
paintComponent

method to be executed.


To force a repainting of the screen, use the
repaint

method.

Using the drawString() Method to Draw Text


drawString
() method


allows you to draw a String in a Swing window


is a method of the Graphics class


Requires three arguments:


A String


An x
-
axis coordinate


A y
-
axis coordinate


Example:


g.drawString(“hello”, 75, 100) draws the string 75
pixels to the right and 100 pixels down starting from
the upper left corner of the component.

Drawing Text Using Multiple Fonts


Specify a font by its font face name (font family
name with optional suffix)


e.g. Helvetica Bold


To find out which fonts are available on a
computer, call the
getAvailableFontFamilyNames

method of the
GraphicsEnvironment

class.


see pg. 262 in your books for example code


To draw characters in a font, you must first crate
an object of the class
Font
.


e.g. Font helvb14 = new Font(“Helvetica”,
Font.BOLD, 14);

Centering Text


To center text, you need to first get the width and
height of the string in pixels.


These dimensions depend on three factors:


the font used


the string


the device on which the font is drawn (e.g. the
computer screen)


To get the object that represents the font
characteristics of the screen device, call the
getFontRenderContext

method of the
Graphics2D

class. It returns an object of the
FontRenderContext

Centering Text


The
Graphics2D

class is a subclass of the
Graphics

class.


You can instantiate your own Graphics or Graphics 2D
objects


The following code returns the rectangle that
encloses the string:


FontRenderContext context = g2.getFontRenderContext();

Rectangle2D bounds = f.getStringBounds(message, context);



See pg. 265 to see the code to use all of this
information to center a string in its surrounding
panel.

Example of Drawing

Text Strings with Fonts


see



http://www2.bc.edu/~bernier/MC697/LectureNotes/DrawTextTest.java