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.

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

Introduction to Java 2 Programming
CS18000: Problem Solving and Object-Oriented Programming.
Graphic User Interfaces Layout Managers Event Handling.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Inner Classes in Java CMSC 432 Shon Vick. 2 Conceptual Structure package p 1 class A 1 class A 2... class A n... package n class A 1 class A 2... class.
Advanced Java Class GUI – part 1. Intro to GUI GUI = Graphical User Interface -- “Gooey” Just because it’s “gooey” does not mean you may write messy code.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
9/21/99www.cs.vt.edu/wwtut/1 Introduction To Abstract Windowing Toolkit (AWT) Part 3 – AWT Marc Abrams Virginia Tech CS Dept courses.cs.vt.edu/wwwtut/
Java Programming Chapter 10 Graphical User Interfaces.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 12.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Abstract Window Toolkit (AWT) The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include:  A rich set.
Cs884(Prasad)java12AWT1 Abstract Windowing Toolkit Support for Graphical User Interface (Event-driven programming)
Applets Session 8. Java Simplified / Session 8 / 2 of 31 Review The Abstract Windowing Toolkit (AWT) is a set of classes that allow us to create a graphical.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
Session 11 Border Layout, using Panels, Introduction to PinBallGame.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Swing Differences between Swing and AWT Naming Conventions All Swing components begin with a capital J -- JPanel, JButton, JScrollBar, JApplet, etc..
CSE 219 Computer Science III Image Manipulation. HW 1 Has been posted on Blackboard Making a Game of Life with limited.
Layout Managers Arranges and lays out the GUI components on a container.
GUI Graphical User Interface. Outline Introduction Packages Components Layout Manager Events and Listeners Examples.
1 Windows program example import java.awt.*; import java.awt.event.*; public class wpexample extends Frame { public wpexample(String title) { super(title);
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Object Oriented Programming.  Interface  Event Handling.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Mouse Listeners Moving the mouse will also generate events like the Timer –To have your program respond, you must implement either or both of MouseListener.
Basics of GUI Programming Chapter 11 and Chapter 22.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
CS Lecture 04 Mice Lynda Thomas
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.
Graphical User Interface (GUI)
Getting Started with GUI Programming Chapter 10 CSCI 1302.
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Events and Event Handling
Chapter 9: Graphical User Interfaces
Applets.
INFSY 547: WEB-Based Technologies
Tim McKenna Layout Mangers in Java Tim McKenna
Programming in Java Event Handling
GUI Programming III: Events
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
AWT.
Timer class and inner classes
Introduction to Computing Using Java
Border Layout, using Panels, Introduction to PinBallGame
Introduction to Graphical Interfaces in Java: AWT and Swing
Advanced GUIs and Graphics
Graphical User Interface
TA: Nouf Al-Harbi NoufNaief.net :::
Presentation transcript:

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