Printing in Java Printing in Java 1.1 is implemented through the new PrintJob class and PrintGraphics interface When a PrintJob object is created, the user is prompted with a platform-dependent print dialog getGraphics() method this object returns a Graphics object that can be used for printing To print a component, you simply pass this Graphics object to the component's print() method Proprietary© 1997
Example of Printing from Example 8.1 void print() { // Obtain a PrintJob object. This posts a Print dialog. // printprefs (created below) stores user printing preferences. Toolkit toolkit = this.getToolkit(); PrintJob job = toolkit.getPrintJob(frame, "Scribble", printprefs); // If the user clicked Cancel in the print dialog, then do nothing. if (job == null) return; // Get a Graphics object for the first page of output. Graphics page = job.getGraphics(); // Check the size of the scribble component and of the page. Dimension size = this.getSize(); Dimension pagesize = job.getPageDimension(); Proprietary© 1997
Example of Printing from Example 8.1 (continued) // Center the output on the page. Otherwise it would be // scrunched up in the upper-left corner of the page. page.translate((pagesize.width - size.width)/2, (pagesize.height - size.height)/2); // Draw a border around the output area, so it looks neat. page.drawRect(-1, -1, size.width+1, size.height+1); // Set a clipping region so our scribbles don't go outside the border. // On-screen this clipping happens automatically, but not on paper. page.setClip(0, 0, size.width, size.height); // Print this Scribble component. By default this will just call paint(). // This method is named print(), too, but that is just coincidence. this.print(page); // Finish up printing. page.dispose(); // End the page--send it to the printer. job.end(); // End the print job. } Proprietary© 1997
More New AWT Features Cut-and-Paste - Data transfer via the cut-and-paste metaphor is supported in Java 1.1 by the classes and interfaces in the java.awt.datatransfer package Popup Menus and Menu Shortcuts Keyboard Focus Traversal SystemColor class to represent a color New font approach - use generic "serif," "sansserif," and "monospaced" rather than the specific font whenever possible Proprietary© 1997
Even More New AWT Features Now every component can have a its own mouse cursor, represented by the new Cursor object ScrollPane class Simpler components, containers, without the overhead of a native window (like Gadgets in X) Several changes to clipping and image manipulation (new methods to allow arbitrary image cropping, scaling, and flipping) Proprietary© 1997
Introduction to the AWT using the Java 1.0 Event Model import java.applet.*; import java.awt.*; /** A simple applet using the Java 1.0 event handling model */ public class ButtonTest extends Applet { private int lastx, lasty; // Remember last mouse coordinates. Button clear_button; // The Clear button. Graphics g; // A Graphics object for drawing. /** Initialize the button and the Graphics object. */ public void init( ) { clear_button = new Button("Clear"); this.add(clear_button); g = this.getGraphics(); } Proprietary© 1997
Introduction to the AWT using the Java 1.0 Event Model (cont’d) /** Respond to mouse clicks. */ public boolean mouseDown(Event e, int x, int y) { lastx = x; lasty = y; return true; } /** Respond to mouse drags. */ public boolean mouseDrag(Event e, int x, int y) { g.setColor(Color.black); g.drawLine(lastx, lasty, x, y); Proprietary© 1997
Introduction to the AWT using the Java 1.0 Event Model (cont’d) /** Respond to key presses. */ public boolean keyDown(Event e, int key) { if ((e.id == Event.KEY_PRESS) && (key == 'c')) { clear(); return true; } else return false; Proprietary© 1997
Introduction to the AWT using the Java 1.0 Event Model (cont’d) /** Respond to Button clicks. */ public boolean action(Event e, Object arg) { if (e.target == clear_button) { clear(); return true; } else return false; /** convenience method to erase the scribble */ public void clear() { g.setColor(this.getBackground()); g.fillRect(0, 0, bounds().width, bounds().height); Proprietary© 1997
Introduction to the AWT Peer graphical objects Java graphical objects Proprietary© 1997
Layouts Helps you place graphical components on the screen Layout managers FlowLayout GridLayout BorderLayout CardLayout GridBagLayout Proprietary© 1997
FlowLayout Flows from left to right Then to next row as needed Proprietary© 1997
GridLayout Components are placed in cells of a grid / matrix Proprietary© 1997
BorderLayout Components placed on the North, South, East, and West borders (rest in center) Proprietary© 1997
CardLayout Manages several components, displaying one of them at a time and hiding the rest All the components are given the same size Like a deck of cards Usually used for managing other containers Proprietary© 1997
GridBagLayout GridBagLayout, you can organize components in multiple rows and columns, stretch specific rows or columns when space is available, and anchor objects in different corners Proprietary© 1997
Containers A type of component that provides a rectangular area within which other components can be organized by a LayoutManager Subclass of Component Can be nested inside another Container Proprietary© 1997
Example of Nested Containers Panels within Panels Proprietary© 1997
Windows The Window object provides a top-level window on the screen, with no borders or menu bar Proprietary© 1997
Frames A Window with all the window manager's decorations (window title, borders, window minimize/maximize/close functionality) added It may also include a menu bar Proprietary© 1997
Dialogs / FileDialogs For user interaction Can be modal Proprietary© 1997
ScrollPane New in Java 1.1 A Container with built-in scrollbars that can be used to scroll its contents In the current implementation, a ScrollPane can hold only one Component and has no layout manager Proprietary© 1997
Drawing and Graphics Graphics class is an abstract class that provides the means to access different graphics devices Rarely create a Graphics object directly Its constructor is protected and is only called by the subclasses that extend Graphics You generally draw using the the current graphics context The sole parameter of the Component.paint( ) and Component.update( ) methods A Graphics object is always available when you override a component's paint( ) and update( ) methods Font, FontMetrics, Color, and SystemColor classes provide the ability to alter the displayed output Also possible to call getGraphics() Proprietary© 1997
The drawLine() Method public abstract void drawLine (int x1, int y1, int x2, int y2) Draws a line on the graphics context in the current color from (x1, y1) to (x2, y2) If (x1, y1) and (x2, y2) are the same point, you will draw a point (there is no method for drawing points). g.drawLine (5, 5, 50, 75); g.drawLine (5, 75, 5, 75); g.drawLine (50, 5, 50, 5); Proprietary© 1997
Examples of Drawing g.setColor (Color.black); g.drawRect (25, 10, 50, 75); g.fillRect (25, 110, 50, 75); g.drawRoundRect (100, 10, 50, 75, 60, 50); g.fillRoundRect (100, 110, 50, 75, 60, 50); Proprietary© 1997
An Image in Java A displayable object maintained in memory Support for reading GIF & JPEG Support for GIF89a animation Netscape Navigator, Internet Explorer, HotJava, and Sun's JDK also can use the XBM image format Loaded from the filesystem or network by the getImage() method (of either Component or Toolkit) Drawn with drawImage() from Graphics Manipulated by several objects within the java.awt.image package Proprietary© 1997
Java 1.1 Event Model Used by both the AWT and by Java Beans Every event is subclass of java.util.EventObject AWT events are subclasses of java.awt.AWTEvent Every event has a source object, which can be obtained with getSource() Type of event obtained using getID(), for example FocusEvent.FOCUS_GAINED and FocusEvent.FOCUS_LOST Proprietary© 1997
Java 1.1 Event Model Event subclasses contain whatever data values are pertinent to the particular event type For example, MouseEvent has getX(), getY(), and getClickCount() methods; it also inherits the getModifiers() and getWhen() methods, among others The 1.1 event handling model is based on the concept of an "event listener" Proprietary© 1997
Example of Java 1.1 Event Model import java.applet.*; import java.awt.*; import java.awt.event.*; public class Scribble extends Applet implements MouseListener, MouseMotionListener { private int last_x, last_y; public void init( ) { // Tell this applet what MouseListener and MouseMotionListener // objects to notify when mouse and mouse motion events occur // Since we implement the interfaces, our own methods are called this.addMouseListener(this); this.addMouseMotionListener(this); } // A method from the MouseListener interface. Invoked when the // user presses a mouse button. public void mousePressed(MouseEvent e) { last_x = e.getX( ); last_y = e.getY( ); Proprietary© 1997
Example of Java 1.1 Event Model // A method from the MouseMotionListener interface. Invoked // when the user drags the mouse with a button pressed. public void mouseDragged(MouseEvent e) { Graphics g = this.getGraphics( ); int x = e.getX(), y = e.getY( ); g.drawLine(last_x, last_y, x, y); last_x = x; last_y = y; } // The other, unused methods of the MouseListener interface. public void mouseReleased(MouseEvent e) {;} public void mouseClicked(MouseEvent e) {;} public void mouseEntered(MouseEvent e) {;} public void mouseExited(MouseEvent e) {;} // The other method of the MouseMotionListener interface. public void mouseMoved(MouseEvent e) {;} Proprietary© 1997